| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8 -*- */ | 
|  | 2 |  | 
|  | 3 | /* | 
|  | 4 | * MCA bus support functions for sysfs. | 
|  | 5 | * | 
|  | 6 | * (C) 2002 James Bottomley <James.Bottomley@HansenPartnership.com> | 
|  | 7 | * | 
|  | 8 | **----------------------------------------------------------------------------- | 
|  | 9 | ** | 
|  | 10 | **  This program is free software; you can redistribute it and/or modify | 
|  | 11 | **  it under the terms of the GNU General Public License as published by | 
|  | 12 | **  the Free Software Foundation; either version 2 of the License, or | 
|  | 13 | **  (at your option) any later version. | 
|  | 14 | ** | 
|  | 15 | **  This program is distributed in the hope that it will be useful, | 
|  | 16 | **  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 17 | **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 18 | **  GNU General Public License for more details. | 
|  | 19 | ** | 
|  | 20 | **  You should have received a copy of the GNU General Public License | 
|  | 21 | **  along with this program; if not, write to the Free Software | 
|  | 22 | **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 23 | ** | 
|  | 24 | **----------------------------------------------------------------------------- | 
|  | 25 | */ | 
|  | 26 |  | 
|  | 27 | #include <linux/kernel.h> | 
|  | 28 | #include <linux/device.h> | 
|  | 29 | #include <linux/mca.h> | 
|  | 30 | #include <linux/module.h> | 
|  | 31 | #include <linux/init.h> | 
|  | 32 | #include <linux/slab.h> | 
|  | 33 |  | 
|  | 34 | /* Very few machines have more than one MCA bus.  However, there are | 
|  | 35 | * those that do (Voyager 35xx/5xxx), so we do it this way for future | 
|  | 36 | * expansion.  None that I know have more than 2 */ | 
|  | 37 | static struct mca_bus *mca_root_busses[MAX_MCA_BUSSES]; | 
|  | 38 |  | 
|  | 39 | #define MCA_DEVINFO(i,s) { .pos = i, .name = s } | 
|  | 40 |  | 
|  | 41 | struct mca_device_info { | 
|  | 42 | short pos_id;		/* the 2 byte pos id for this card */ | 
|  | 43 | char name[DEVICE_NAME_SIZE]; | 
|  | 44 | }; | 
|  | 45 |  | 
|  | 46 | static int mca_bus_match (struct device *dev, struct device_driver *drv) | 
|  | 47 | { | 
|  | 48 | struct mca_device *mca_dev = to_mca_device (dev); | 
|  | 49 | struct mca_driver *mca_drv = to_mca_driver (drv); | 
|  | 50 | const short *mca_ids = mca_drv->id_table; | 
|  | 51 | int i; | 
|  | 52 |  | 
|  | 53 | if (!mca_ids) | 
|  | 54 | return 0; | 
|  | 55 |  | 
|  | 56 | for(i = 0; mca_ids[i]; i++) { | 
|  | 57 | if (mca_ids[i] == mca_dev->pos_id) { | 
|  | 58 | mca_dev->index = i; | 
|  | 59 | return 1; | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | return 0; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | struct bus_type mca_bus_type = { | 
|  | 67 | .name  = "MCA", | 
|  | 68 | .match = mca_bus_match, | 
|  | 69 | }; | 
|  | 70 | EXPORT_SYMBOL (mca_bus_type); | 
|  | 71 |  | 
| Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 72 | static ssize_t mca_show_pos_id(struct device *dev, struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { | 
|  | 74 | /* four digits, \n and trailing \0 */ | 
|  | 75 | struct mca_device *mca_dev = to_mca_device(dev); | 
|  | 76 | int len; | 
|  | 77 |  | 
|  | 78 | if(mca_dev->pos_id < MCA_DUMMY_POS_START) | 
|  | 79 | len = sprintf(buf, "%04x\n", mca_dev->pos_id); | 
|  | 80 | else | 
|  | 81 | len = sprintf(buf, "none\n"); | 
|  | 82 | return len; | 
|  | 83 | } | 
| Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 84 | static ssize_t mca_show_pos(struct device *dev, struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { | 
|  | 86 | /* enough for 8 two byte hex chars plus space and new line */ | 
|  | 87 | int j, len=0; | 
|  | 88 | struct mca_device *mca_dev = to_mca_device(dev); | 
|  | 89 |  | 
|  | 90 | for(j=0; j<8; j++) | 
|  | 91 | len += sprintf(buf+len, "%02x ", mca_dev->pos[j]); | 
|  | 92 | /* change last trailing space to new line */ | 
|  | 93 | buf[len-1] = '\n'; | 
|  | 94 | return len; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | static DEVICE_ATTR(id, S_IRUGO, mca_show_pos_id, NULL); | 
|  | 98 | static DEVICE_ATTR(pos, S_IRUGO, mca_show_pos, NULL); | 
|  | 99 |  | 
|  | 100 | int __init mca_register_device(int bus, struct mca_device *mca_dev) | 
|  | 101 | { | 
|  | 102 | struct mca_bus *mca_bus = mca_root_busses[bus]; | 
| Jeff Garzik | 49a6cbe | 2006-10-11 01:22:23 -0700 | [diff] [blame] | 103 | int rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 |  | 
|  | 105 | mca_dev->dev.parent = &mca_bus->dev; | 
|  | 106 | mca_dev->dev.bus = &mca_bus_type; | 
|  | 107 | sprintf (mca_dev->dev.bus_id, "%02d:%02X", bus, mca_dev->slot); | 
|  | 108 | mca_dev->dma_mask = mca_bus->default_dma_mask; | 
|  | 109 | mca_dev->dev.dma_mask = &mca_dev->dma_mask; | 
|  | 110 | mca_dev->dev.coherent_dma_mask = mca_dev->dma_mask; | 
|  | 111 |  | 
| Jeff Garzik | 49a6cbe | 2006-10-11 01:22:23 -0700 | [diff] [blame] | 112 | rc = device_register(&mca_dev->dev); | 
|  | 113 | if (rc) | 
|  | 114 | goto err_out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 |  | 
| Jeff Garzik | 49a6cbe | 2006-10-11 01:22:23 -0700 | [diff] [blame] | 116 | rc = device_create_file(&mca_dev->dev, &dev_attr_id); | 
|  | 117 | if (rc) goto err_out_devreg; | 
|  | 118 | rc = device_create_file(&mca_dev->dev, &dev_attr_pos); | 
|  | 119 | if (rc) goto err_out_id; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | return 1; | 
| Jeff Garzik | 49a6cbe | 2006-10-11 01:22:23 -0700 | [diff] [blame] | 122 |  | 
|  | 123 | err_out_id: | 
|  | 124 | device_remove_file(&mca_dev->dev, &dev_attr_id); | 
|  | 125 | err_out_devreg: | 
|  | 126 | device_unregister(&mca_dev->dev); | 
|  | 127 | err_out: | 
|  | 128 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
|  | 131 | /* */ | 
|  | 132 | struct mca_bus * __devinit mca_attach_bus(int bus) | 
|  | 133 | { | 
|  | 134 | struct mca_bus *mca_bus; | 
|  | 135 |  | 
|  | 136 | if (unlikely(mca_root_busses[bus] != NULL)) { | 
|  | 137 | /* This should never happen, but just in case */ | 
|  | 138 | printk(KERN_EMERG "MCA tried to add already existing bus %d\n", | 
|  | 139 | bus); | 
|  | 140 | dump_stack(); | 
|  | 141 | return NULL; | 
|  | 142 | } | 
|  | 143 |  | 
| Jeff Garzik | 49a6cbe | 2006-10-11 01:22:23 -0700 | [diff] [blame] | 144 | mca_bus = kzalloc(sizeof(struct mca_bus), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (!mca_bus) | 
|  | 146 | return NULL; | 
| Jeff Garzik | 49a6cbe | 2006-10-11 01:22:23 -0700 | [diff] [blame] | 147 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | sprintf(mca_bus->dev.bus_id,"mca%d",bus); | 
|  | 149 | sprintf(mca_bus->name,"Host %s MCA Bridge", bus ? "Secondary" : "Primary"); | 
| Jeff Garzik | 49a6cbe | 2006-10-11 01:22:23 -0700 | [diff] [blame] | 150 | if (device_register(&mca_bus->dev)) { | 
|  | 151 | kfree(mca_bus); | 
|  | 152 | return NULL; | 
|  | 153 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | mca_root_busses[bus] = mca_bus; | 
|  | 156 |  | 
|  | 157 | return mca_bus; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | int __init mca_system_init (void) | 
|  | 161 | { | 
|  | 162 | return bus_register(&mca_bus_type); | 
|  | 163 | } |