Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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