Input: aaed2000_kbd - convert to use polldev library
[sfrench/cifs-2.6.git] / drivers / mmc / core / sysfs.c
1 /*
2  *  linux/drivers/mmc/core/sysfs.c
3  *
4  *  Copyright (C) 2003 Russell King, All Rights Reserved.
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  *  MMC sysfs/driver model support.
11  */
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/idr.h>
16 #include <linux/workqueue.h>
17
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/host.h>
20
21 #include "sysfs.h"
22
23 #define dev_to_mmc_card(d)      container_of(d, struct mmc_card, dev)
24 #define to_mmc_driver(d)        container_of(d, struct mmc_driver, drv)
25 #define cls_dev_to_mmc_host(d)  container_of(d, struct mmc_host, class_dev)
26
27 #define MMC_ATTR(name, fmt, args...)                                    \
28 static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
29 {                                                                       \
30         struct mmc_card *card = dev_to_mmc_card(dev);                   \
31         return sprintf(buf, fmt, args);                                 \
32 }
33
34 MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
35         card->raw_cid[2], card->raw_cid[3]);
36 MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
37         card->raw_csd[2], card->raw_csd[3]);
38 MMC_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
39 MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
40 MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
41 MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
42 MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid);
43 MMC_ATTR(name, "%s\n", card->cid.prod_name);
44 MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid);
45 MMC_ATTR(serial, "0x%08x\n", card->cid.serial);
46
47 #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
48
49 static struct device_attribute mmc_dev_attrs[] = {
50         MMC_ATTR_RO(cid),
51         MMC_ATTR_RO(csd),
52         MMC_ATTR_RO(date),
53         MMC_ATTR_RO(fwrev),
54         MMC_ATTR_RO(hwrev),
55         MMC_ATTR_RO(manfid),
56         MMC_ATTR_RO(name),
57         MMC_ATTR_RO(oemid),
58         MMC_ATTR_RO(serial),
59         __ATTR_NULL
60 };
61
62 static struct device_attribute mmc_dev_attr_scr = MMC_ATTR_RO(scr);
63
64
65 static void mmc_release_card(struct device *dev)
66 {
67         struct mmc_card *card = dev_to_mmc_card(dev);
68
69         kfree(card);
70 }
71
72 /*
73  * This currently matches any MMC driver to any MMC card - drivers
74  * themselves make the decision whether to drive this card in their
75  * probe method.
76  */
77 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
78 {
79         return 1;
80 }
81
82 static int
83 mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
84                 int buf_size)
85 {
86         struct mmc_card *card = dev_to_mmc_card(dev);
87         char ccc[13];
88         int retval = 0, i = 0, length = 0;
89
90 #define add_env(fmt,val) do {                                   \
91         retval = add_uevent_var(envp, num_envp, &i,             \
92                                 buf, buf_size, &length,         \
93                                 fmt, val);                      \
94         if (retval)                                             \
95                 return retval;                                  \
96 } while (0);
97
98         for (i = 0; i < 12; i++)
99                 ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
100         ccc[12] = '\0';
101
102         add_env("MMC_CCC=%s", ccc);
103         add_env("MMC_MANFID=%06x", card->cid.manfid);
104         add_env("MMC_NAME=%s", mmc_card_name(card));
105         add_env("MMC_OEMID=%04x", card->cid.oemid);
106 #undef add_env
107         envp[i] = NULL;
108
109         return 0;
110 }
111
112 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
113 {
114         struct mmc_driver *drv = to_mmc_driver(dev->driver);
115         struct mmc_card *card = dev_to_mmc_card(dev);
116         int ret = 0;
117
118         if (dev->driver && drv->suspend)
119                 ret = drv->suspend(card, state);
120         return ret;
121 }
122
123 static int mmc_bus_resume(struct device *dev)
124 {
125         struct mmc_driver *drv = to_mmc_driver(dev->driver);
126         struct mmc_card *card = dev_to_mmc_card(dev);
127         int ret = 0;
128
129         if (dev->driver && drv->resume)
130                 ret = drv->resume(card);
131         return ret;
132 }
133
134 static int mmc_bus_probe(struct device *dev)
135 {
136         struct mmc_driver *drv = to_mmc_driver(dev->driver);
137         struct mmc_card *card = dev_to_mmc_card(dev);
138
139         return drv->probe(card);
140 }
141
142 static int mmc_bus_remove(struct device *dev)
143 {
144         struct mmc_driver *drv = to_mmc_driver(dev->driver);
145         struct mmc_card *card = dev_to_mmc_card(dev);
146
147         drv->remove(card);
148
149         return 0;
150 }
151
152 static struct bus_type mmc_bus_type = {
153         .name           = "mmc",
154         .dev_attrs      = mmc_dev_attrs,
155         .match          = mmc_bus_match,
156         .uevent         = mmc_bus_uevent,
157         .probe          = mmc_bus_probe,
158         .remove         = mmc_bus_remove,
159         .suspend        = mmc_bus_suspend,
160         .resume         = mmc_bus_resume,
161 };
162
163 /**
164  *      mmc_register_driver - register a media driver
165  *      @drv: MMC media driver
166  */
167 int mmc_register_driver(struct mmc_driver *drv)
168 {
169         drv->drv.bus = &mmc_bus_type;
170         return driver_register(&drv->drv);
171 }
172
173 EXPORT_SYMBOL(mmc_register_driver);
174
175 /**
176  *      mmc_unregister_driver - unregister a media driver
177  *      @drv: MMC media driver
178  */
179 void mmc_unregister_driver(struct mmc_driver *drv)
180 {
181         drv->drv.bus = &mmc_bus_type;
182         driver_unregister(&drv->drv);
183 }
184
185 EXPORT_SYMBOL(mmc_unregister_driver);
186
187
188 /*
189  * Internal function.  Initialise a MMC card structure.
190  */
191 void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
192 {
193         memset(card, 0, sizeof(struct mmc_card));
194         card->host = host;
195         device_initialize(&card->dev);
196         card->dev.parent = mmc_classdev(host);
197         card->dev.bus = &mmc_bus_type;
198         card->dev.release = mmc_release_card;
199 }
200
201 /*
202  * Internal function.  Register a new MMC card with the driver model.
203  */
204 int mmc_register_card(struct mmc_card *card)
205 {
206         int ret;
207
208         snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
209                  "%s:%04x", mmc_hostname(card->host), card->rca);
210
211         ret = device_add(&card->dev);
212         if (ret == 0) {
213                 if (mmc_card_sd(card)) {
214                         ret = device_create_file(&card->dev, &mmc_dev_attr_scr);
215                         if (ret)
216                                 device_del(&card->dev);
217                 }
218         }
219         if (ret == 0)
220                 mmc_card_set_present(card);
221         return ret;
222 }
223
224 /*
225  * Internal function.  Unregister a new MMC card with the
226  * driver model, and (eventually) free it.
227  */
228 void mmc_remove_card(struct mmc_card *card)
229 {
230         if (mmc_card_present(card)) {
231                 if (mmc_card_sd(card))
232                         device_remove_file(&card->dev, &mmc_dev_attr_scr);
233
234                 device_del(&card->dev);
235         }
236
237         put_device(&card->dev);
238 }
239
240
241 static void mmc_host_classdev_release(struct device *dev)
242 {
243         struct mmc_host *host = cls_dev_to_mmc_host(dev);
244         kfree(host);
245 }
246
247 static struct class mmc_host_class = {
248         .name           = "mmc_host",
249         .dev_release    = mmc_host_classdev_release,
250 };
251
252 static DEFINE_IDR(mmc_host_idr);
253 static DEFINE_SPINLOCK(mmc_host_lock);
254
255 /*
256  * Internal function. Allocate a new MMC host.
257  */
258 struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev)
259 {
260         struct mmc_host *host;
261
262         host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
263         if (host) {
264                 memset(host, 0, sizeof(struct mmc_host) + extra);
265
266                 host->parent = dev;
267                 host->class_dev.parent = dev;
268                 host->class_dev.class = &mmc_host_class;
269                 device_initialize(&host->class_dev);
270         }
271
272         return host;
273 }
274
275 /*
276  * Internal function. Register a new MMC host with the MMC class.
277  */
278 int mmc_add_host_sysfs(struct mmc_host *host)
279 {
280         int err;
281
282         if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
283                 return -ENOMEM;
284
285         spin_lock(&mmc_host_lock);
286         err = idr_get_new(&mmc_host_idr, host, &host->index);
287         spin_unlock(&mmc_host_lock);
288         if (err)
289                 return err;
290
291         snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
292                  "mmc%d", host->index);
293
294         return device_add(&host->class_dev);
295 }
296
297 /*
298  * Internal function. Unregister a MMC host with the MMC class.
299  */
300 void mmc_remove_host_sysfs(struct mmc_host *host)
301 {
302         device_del(&host->class_dev);
303
304         spin_lock(&mmc_host_lock);
305         idr_remove(&mmc_host_idr, host->index);
306         spin_unlock(&mmc_host_lock);
307 }
308
309 /*
310  * Internal function. Free a MMC host.
311  */
312 void mmc_free_host_sysfs(struct mmc_host *host)
313 {
314         put_device(&host->class_dev);
315 }
316
317 static struct workqueue_struct *workqueue;
318
319 /*
320  * Internal function. Schedule delayed work in the MMC work queue.
321  */
322 int mmc_schedule_delayed_work(struct delayed_work *work, unsigned long delay)
323 {
324         return queue_delayed_work(workqueue, work, delay);
325 }
326
327 /*
328  * Internal function. Flush all scheduled work from the MMC work queue.
329  */
330 void mmc_flush_scheduled_work(void)
331 {
332         flush_workqueue(workqueue);
333 }
334
335 static int __init mmc_init(void)
336 {
337         int ret;
338
339         workqueue = create_singlethread_workqueue("kmmcd");
340         if (!workqueue)
341                 return -ENOMEM;
342
343         ret = bus_register(&mmc_bus_type);
344         if (ret == 0) {
345                 ret = class_register(&mmc_host_class);
346                 if (ret)
347                         bus_unregister(&mmc_bus_type);
348         }
349         return ret;
350 }
351
352 static void __exit mmc_exit(void)
353 {
354         class_unregister(&mmc_host_class);
355         bus_unregister(&mmc_bus_type);
356         destroy_workqueue(workqueue);
357 }
358
359 module_init(mmc_init);
360 module_exit(mmc_exit);