blob: 67b8e9453b191a2c701970f4ccbdd4a5207dd1e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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 */
37static struct mca_bus *mca_root_busses[MAX_MCA_BUSSES];
38
39#define MCA_DEVINFO(i,s) { .pos = i, .name = s }
40
41struct mca_device_info {
42 short pos_id; /* the 2 byte pos id for this card */
43 char name[DEVICE_NAME_SIZE];
44};
45
46static 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);
James Bottomley809aa502007-05-09 02:33:29 -070050 const unsigned short *mca_ids = mca_drv->id_table;
James Bottomley8813d1c2007-05-09 02:33:30 -070051 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
James Bottomley8813d1c2007-05-09 02:33:30 -070053 if (mca_ids) {
54 for(i = 0; mca_ids[i]; i++) {
55 if (mca_ids[i] == mca_dev->pos_id) {
56 mca_dev->index = i;
57 return 1;
58 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60 }
James Bottomley8813d1c2007-05-09 02:33:30 -070061 /* If the integrated id is present, treat it as though it were an
62 * additional id in the id_table (it can't be because by definition,
63 * integrated id's overflow a short */
64 if (mca_drv->integrated_id && mca_dev->pos_id ==
65 mca_drv->integrated_id) {
66 mca_dev->index = i;
67 return 1;
68 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return 0;
70}
71
72struct bus_type mca_bus_type = {
73 .name = "MCA",
74 .match = mca_bus_match,
75};
76EXPORT_SYMBOL (mca_bus_type);
77
Yani Ioannoue404e272005-05-17 06:42:58 -040078static ssize_t mca_show_pos_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 /* four digits, \n and trailing \0 */
81 struct mca_device *mca_dev = to_mca_device(dev);
82 int len;
83
84 if(mca_dev->pos_id < MCA_DUMMY_POS_START)
85 len = sprintf(buf, "%04x\n", mca_dev->pos_id);
86 else
87 len = sprintf(buf, "none\n");
88 return len;
89}
Yani Ioannoue404e272005-05-17 06:42:58 -040090static ssize_t mca_show_pos(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 /* enough for 8 two byte hex chars plus space and new line */
93 int j, len=0;
94 struct mca_device *mca_dev = to_mca_device(dev);
95
96 for(j=0; j<8; j++)
97 len += sprintf(buf+len, "%02x ", mca_dev->pos[j]);
98 /* change last trailing space to new line */
99 buf[len-1] = '\n';
100 return len;
101}
102
103static DEVICE_ATTR(id, S_IRUGO, mca_show_pos_id, NULL);
104static DEVICE_ATTR(pos, S_IRUGO, mca_show_pos, NULL);
105
106int __init mca_register_device(int bus, struct mca_device *mca_dev)
107{
108 struct mca_bus *mca_bus = mca_root_busses[bus];
Jeff Garzik49a6cbe2006-10-11 01:22:23 -0700109 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 mca_dev->dev.parent = &mca_bus->dev;
112 mca_dev->dev.bus = &mca_bus_type;
113 sprintf (mca_dev->dev.bus_id, "%02d:%02X", bus, mca_dev->slot);
114 mca_dev->dma_mask = mca_bus->default_dma_mask;
115 mca_dev->dev.dma_mask = &mca_dev->dma_mask;
116 mca_dev->dev.coherent_dma_mask = mca_dev->dma_mask;
117
Jeff Garzik49a6cbe2006-10-11 01:22:23 -0700118 rc = device_register(&mca_dev->dev);
119 if (rc)
120 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jeff Garzik49a6cbe2006-10-11 01:22:23 -0700122 rc = device_create_file(&mca_dev->dev, &dev_attr_id);
123 if (rc) goto err_out_devreg;
124 rc = device_create_file(&mca_dev->dev, &dev_attr_pos);
125 if (rc) goto err_out_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return 1;
Jeff Garzik49a6cbe2006-10-11 01:22:23 -0700128
129err_out_id:
130 device_remove_file(&mca_dev->dev, &dev_attr_id);
131err_out_devreg:
132 device_unregister(&mca_dev->dev);
133err_out:
134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
137/* */
138struct mca_bus * __devinit mca_attach_bus(int bus)
139{
140 struct mca_bus *mca_bus;
141
142 if (unlikely(mca_root_busses[bus] != NULL)) {
143 /* This should never happen, but just in case */
144 printk(KERN_EMERG "MCA tried to add already existing bus %d\n",
145 bus);
146 dump_stack();
147 return NULL;
148 }
149
Jeff Garzik49a6cbe2006-10-11 01:22:23 -0700150 mca_bus = kzalloc(sizeof(struct mca_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!mca_bus)
152 return NULL;
Jeff Garzik49a6cbe2006-10-11 01:22:23 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 sprintf(mca_bus->dev.bus_id,"mca%d",bus);
155 sprintf(mca_bus->name,"Host %s MCA Bridge", bus ? "Secondary" : "Primary");
Jeff Garzik49a6cbe2006-10-11 01:22:23 -0700156 if (device_register(&mca_bus->dev)) {
157 kfree(mca_bus);
158 return NULL;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 mca_root_busses[bus] = mca_bus;
162
163 return mca_bus;
164}
165
166int __init mca_system_init (void)
167{
168 return bus_register(&mca_bus_type);
169}