blob: 48446a699ef8c59d8ac61055cd3f2d94427ae737 [file] [log] [blame]
Heiko J Schickd7a30102005-11-16 08:56:43 +01001/*
2 * IBM PowerPC IBM eBus Infrastructure Support.
3 *
4 * Copyright (c) 2005 IBM Corporation
Joachim Fenkes6bccf752007-03-09 19:00:32 +01005 * Joachim Fenkes <fenkes@de.ibm.com>
Heiko J Schickd7a30102005-11-16 08:56:43 +01006 * Heiko J Schick <schickhj@de.ibm.com>
Joachim Fenkesa8308802007-03-09 18:56:46 +01007 *
Heiko J Schickd7a30102005-11-16 08:56:43 +01008 * All rights reserved.
9 *
Joachim Fenkesa8308802007-03-09 18:56:46 +010010 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
11 * BSD.
Heiko J Schickd7a30102005-11-16 08:56:43 +010012 *
13 * OpenIB BSD License
14 *
Joachim Fenkesa8308802007-03-09 18:56:46 +010015 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are met:
Heiko J Schickd7a30102005-11-16 08:56:43 +010017 *
Joachim Fenkesa8308802007-03-09 18:56:46 +010018 * Redistributions of source code must retain the above copyright notice, this
19 * list of conditions and the following disclaimer.
Heiko J Schickd7a30102005-11-16 08:56:43 +010020 *
Joachim Fenkesa8308802007-03-09 18:56:46 +010021 * Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
Heiko J Schickd7a30102005-11-16 08:56:43 +010023 * and/or other materials
Joachim Fenkesa8308802007-03-09 18:56:46 +010024 * provided with the distribution.
Heiko J Schickd7a30102005-11-16 08:56:43 +010025 *
Joachim Fenkesa8308802007-03-09 18:56:46 +010026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Heiko J Schickd7a30102005-11-16 08:56:43 +010032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
Joachim Fenkesa8308802007-03-09 18:56:46 +010034 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Heiko J Schickd7a30102005-11-16 08:56:43 +010036 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/init.h>
40#include <linux/console.h>
41#include <linux/kobject.h>
42#include <linux/dma-mapping.h>
43#include <linux/interrupt.h>
44#include <asm/ibmebus.h>
45#include <asm/abs_addr.h>
46
Joachim Fenkes6bccf752007-03-09 19:00:32 +010047#define MAX_LOC_CODE_LENGTH 80
48
49static struct device ibmebus_bus_device = { /* fake "parent" device */
50 .bus_id = "ibmebus",
Heiko J Schickd7a30102005-11-16 08:56:43 +010051};
52
Joachim Fenkes6bccf752007-03-09 19:00:32 +010053struct bus_type ibmebus_bus_type;
54
Heiko J Schickd7a30102005-11-16 08:56:43 +010055static void *ibmebus_alloc_coherent(struct device *dev,
56 size_t size,
57 dma_addr_t *dma_handle,
58 gfp_t flag)
59{
60 void *mem;
Joachim Fenkesa8308802007-03-09 18:56:46 +010061
Heiko J Schickd7a30102005-11-16 08:56:43 +010062 mem = kmalloc(size, flag);
63 *dma_handle = (dma_addr_t)mem;
64
65 return mem;
66}
67
68static void ibmebus_free_coherent(struct device *dev,
Joachim Fenkesa8308802007-03-09 18:56:46 +010069 size_t size, void *vaddr,
Heiko J Schickd7a30102005-11-16 08:56:43 +010070 dma_addr_t dma_handle)
71{
72 kfree(vaddr);
73}
74
75static dma_addr_t ibmebus_map_single(struct device *dev,
76 void *ptr,
77 size_t size,
78 enum dma_data_direction direction)
79{
80 return (dma_addr_t)(ptr);
81}
82
83static void ibmebus_unmap_single(struct device *dev,
84 dma_addr_t dma_addr,
Joachim Fenkesa8308802007-03-09 18:56:46 +010085 size_t size,
Heiko J Schickd7a30102005-11-16 08:56:43 +010086 enum dma_data_direction direction)
87{
88 return;
89}
90
91static int ibmebus_map_sg(struct device *dev,
92 struct scatterlist *sg,
93 int nents, enum dma_data_direction direction)
94{
95 int i;
Joachim Fenkesa8308802007-03-09 18:56:46 +010096
Heiko J Schickd7a30102005-11-16 08:56:43 +010097 for (i = 0; i < nents; i++) {
Joachim Fenkesa8308802007-03-09 18:56:46 +010098 sg[i].dma_address = (dma_addr_t)page_address(sg[i].page)
Heiko J Schickd7a30102005-11-16 08:56:43 +010099 + sg[i].offset;
100 sg[i].dma_length = sg[i].length;
101 }
Joachim Fenkesa8308802007-03-09 18:56:46 +0100102
Heiko J Schickd7a30102005-11-16 08:56:43 +0100103 return nents;
104}
105
106static void ibmebus_unmap_sg(struct device *dev,
107 struct scatterlist *sg,
108 int nents, enum dma_data_direction direction)
109{
110 return;
111}
112
113static int ibmebus_dma_supported(struct device *dev, u64 mask)
114{
115 return 1;
116}
117
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100118static struct dma_mapping_ops ibmebus_dma_ops = {
Heiko J Schickd7a30102005-11-16 08:56:43 +0100119 .alloc_coherent = ibmebus_alloc_coherent,
120 .free_coherent = ibmebus_free_coherent,
121 .map_single = ibmebus_map_single,
122 .unmap_single = ibmebus_unmap_single,
123 .map_sg = ibmebus_map_sg,
124 .unmap_sg = ibmebus_unmap_sg,
125 .dma_supported = ibmebus_dma_supported,
126};
127
128static int ibmebus_bus_probe(struct device *dev)
129{
130 struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
131 struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
132 const struct of_device_id *id;
133 int error = -ENODEV;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100134
Heiko J Schickd7a30102005-11-16 08:56:43 +0100135 if (!ibmebusdrv->probe)
136 return error;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100137
Heiko J Schickd7a30102005-11-16 08:56:43 +0100138 id = of_match_device(ibmebusdrv->id_table, &ibmebusdev->ofdev);
139 if (id) {
140 error = ibmebusdrv->probe(ibmebusdev, id);
141 }
Joachim Fenkesa8308802007-03-09 18:56:46 +0100142
Heiko J Schickd7a30102005-11-16 08:56:43 +0100143 return error;
144}
145
146static int ibmebus_bus_remove(struct device *dev)
147{
148 struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
149 struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
Joachim Fenkesa8308802007-03-09 18:56:46 +0100150
Heiko J Schickd7a30102005-11-16 08:56:43 +0100151 if (ibmebusdrv->remove) {
152 return ibmebusdrv->remove(ibmebusdev);
153 }
Joachim Fenkesa8308802007-03-09 18:56:46 +0100154
Heiko J Schickd7a30102005-11-16 08:56:43 +0100155 return 0;
156}
157
158static void __devinit ibmebus_dev_release(struct device *dev)
159{
160 of_node_put(to_ibmebus_dev(dev)->ofdev.node);
161 kfree(to_ibmebus_dev(dev));
162}
163
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100164static int __devinit ibmebus_register_device_common(
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000165 struct ibmebus_dev *dev, const char *name)
Heiko J Schickd7a30102005-11-16 08:56:43 +0100166{
167 int err = 0;
168
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100169 dev->ofdev.dev.parent = &ibmebus_bus_device;
Heiko J Schickd7a30102005-11-16 08:56:43 +0100170 dev->ofdev.dev.bus = &ibmebus_bus_type;
171 dev->ofdev.dev.release = ibmebus_dev_release;
172
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100173 dev->ofdev.dev.archdata.of_node = dev->ofdev.node;
174 dev->ofdev.dev.archdata.dma_ops = &ibmebus_dma_ops;
175 dev->ofdev.dev.archdata.numa_node = of_node_to_nid(dev->ofdev.node);
176
Heiko J Schickd7a30102005-11-16 08:56:43 +0100177 /* An ibmebusdev is based on a of_device. We have to change the
Joachim Fenkesa8308802007-03-09 18:56:46 +0100178 * bus type to use our own DMA mapping operations.
179 */
Heiko J Schickd7a30102005-11-16 08:56:43 +0100180 if ((err = of_device_register(&dev->ofdev)) != 0) {
181 printk(KERN_ERR "%s: failed to register device (%d).\n",
182 __FUNCTION__, err);
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100183 return -ENODEV;
Heiko J Schickd7a30102005-11-16 08:56:43 +0100184 }
Joachim Fenkesa8308802007-03-09 18:56:46 +0100185
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100186 return 0;
Heiko J Schickd7a30102005-11-16 08:56:43 +0100187}
188
189static struct ibmebus_dev* __devinit ibmebus_register_device_node(
190 struct device_node *dn)
191{
192 struct ibmebus_dev *dev;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000193 const char *loc_code;
Heiko J Schickd7a30102005-11-16 08:56:43 +0100194 int length;
195
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000196 loc_code = of_get_property(dn, "ibm,loc-code", NULL);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100197 if (!loc_code) {
198 printk(KERN_WARNING "%s: node %s missing 'ibm,loc-code'\n",
199 __FUNCTION__, dn->name ? dn->name : "<unknown>");
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100200 return ERR_PTR(-EINVAL);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100201 }
Joachim Fenkesa8308802007-03-09 18:56:46 +0100202
Heiko J Schickd7a30102005-11-16 08:56:43 +0100203 if (strlen(loc_code) == 0) {
204 printk(KERN_WARNING "%s: 'ibm,loc-code' is invalid\n",
205 __FUNCTION__);
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100206 return ERR_PTR(-EINVAL);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100207 }
208
Yan Burmanf8485352006-12-02 13:26:57 +0200209 dev = kzalloc(sizeof(struct ibmebus_dev), GFP_KERNEL);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100210 if (!dev) {
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100211 return ERR_PTR(-ENOMEM);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100212 }
Heiko J Schickd7a30102005-11-16 08:56:43 +0100213
214 dev->ofdev.node = of_node_get(dn);
Joachim Fenkesa8308802007-03-09 18:56:46 +0100215
Heiko J Schickd7a30102005-11-16 08:56:43 +0100216 length = strlen(loc_code);
Joachim Fenkesa8308802007-03-09 18:56:46 +0100217 memcpy(dev->ofdev.dev.bus_id, loc_code
218 + (length - min(length, BUS_ID_SIZE - 1)),
Heiko J Schickd7a30102005-11-16 08:56:43 +0100219 min(length, BUS_ID_SIZE - 1));
220
221 /* Register with generic device framework. */
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100222 if (ibmebus_register_device_common(dev, dn->name) != 0) {
Heiko J Schickd7a30102005-11-16 08:56:43 +0100223 kfree(dev);
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100224 return ERR_PTR(-ENODEV);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100225 }
226
227 return dev;
228}
229
230static void ibmebus_probe_of_nodes(char* name)
231{
232 struct device_node *dn = NULL;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100233
Heiko J Schickd7a30102005-11-16 08:56:43 +0100234 while ((dn = of_find_node_by_name(dn, name))) {
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100235 if (IS_ERR(ibmebus_register_device_node(dn))) {
Heiko J Schickd7a30102005-11-16 08:56:43 +0100236 of_node_put(dn);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100237 return;
238 }
239 }
Joachim Fenkesa8308802007-03-09 18:56:46 +0100240
Heiko J Schickd7a30102005-11-16 08:56:43 +0100241 of_node_put(dn);
Joachim Fenkesa8308802007-03-09 18:56:46 +0100242
Heiko J Schickd7a30102005-11-16 08:56:43 +0100243 return;
244}
245
246static void ibmebus_add_devices_by_id(struct of_device_id *idt)
247{
248 while (strlen(idt->name) > 0) {
249 ibmebus_probe_of_nodes(idt->name);
250 idt++;
251 }
252
253 return;
254}
255
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100256static int ibmebus_match_helper_name(struct device *dev, void *data)
Heiko J Schickd7a30102005-11-16 08:56:43 +0100257{
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100258 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000259 const char *name;
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100260
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000261 name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100262
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000263 if (name && (strcmp(data, name) == 0))
Heiko J Schickd7a30102005-11-16 08:56:43 +0100264 return 1;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100265
Heiko J Schickd7a30102005-11-16 08:56:43 +0100266 return 0;
267}
268
269static int ibmebus_unregister_device(struct device *dev)
270{
Heiko J Schickd7a30102005-11-16 08:56:43 +0100271 of_device_unregister(to_of_device(dev));
272
273 return 0;
274}
275
276static void ibmebus_remove_devices_by_id(struct of_device_id *idt)
277{
278 struct device *dev;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100279
Heiko J Schickd7a30102005-11-16 08:56:43 +0100280 while (strlen(idt->name) > 0) {
Joachim Fenkesa8308802007-03-09 18:56:46 +0100281 while ((dev = bus_find_device(&ibmebus_bus_type, NULL,
Heiko J Schickd7a30102005-11-16 08:56:43 +0100282 (void*)idt->name,
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100283 ibmebus_match_helper_name))) {
Heiko J Schickd7a30102005-11-16 08:56:43 +0100284 ibmebus_unregister_device(dev);
285 }
286 idt++;
Heiko J Schickd7a30102005-11-16 08:56:43 +0100287 }
Joachim Fenkesa8308802007-03-09 18:56:46 +0100288
Heiko J Schickd7a30102005-11-16 08:56:43 +0100289 return;
290}
291
292int ibmebus_register_driver(struct ibmebus_driver *drv)
293{
294 int err = 0;
295
296 drv->driver.name = drv->name;
297 drv->driver.bus = &ibmebus_bus_type;
298 drv->driver.probe = ibmebus_bus_probe;
299 drv->driver.remove = ibmebus_bus_remove;
300
301 if ((err = driver_register(&drv->driver) != 0))
302 return err;
303
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100304 /* remove all supported devices first, in case someone
305 * probed them manually before registering the driver */
306 ibmebus_remove_devices_by_id(drv->id_table);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100307 ibmebus_add_devices_by_id(drv->id_table);
Joachim Fenkesa8308802007-03-09 18:56:46 +0100308
Heiko J Schickd7a30102005-11-16 08:56:43 +0100309 return 0;
310}
311EXPORT_SYMBOL(ibmebus_register_driver);
312
313void ibmebus_unregister_driver(struct ibmebus_driver *drv)
Joachim Fenkesa8308802007-03-09 18:56:46 +0100314{
Heiko J Schickd7a30102005-11-16 08:56:43 +0100315 driver_unregister(&drv->driver);
316 ibmebus_remove_devices_by_id(drv->id_table);
317}
318EXPORT_SYMBOL(ibmebus_unregister_driver);
319
320int ibmebus_request_irq(struct ibmebus_dev *dev,
Joachim Fenkesa8308802007-03-09 18:56:46 +0100321 u32 ist,
David Howells40220c12006-10-09 12:19:47 +0100322 irq_handler_t handler,
Heiko J Schickd7a30102005-11-16 08:56:43 +0100323 unsigned long irq_flags, const char * devname,
324 void *dev_id)
325{
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700326 unsigned int irq = irq_create_mapping(NULL, ist);
Joachim Fenkesa8308802007-03-09 18:56:46 +0100327
Heiko J Schickd7a30102005-11-16 08:56:43 +0100328 if (irq == NO_IRQ)
329 return -EINVAL;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100330
Heiko J Schickd7a30102005-11-16 08:56:43 +0100331 return request_irq(irq, handler,
332 irq_flags, devname, dev_id);
333}
334EXPORT_SYMBOL(ibmebus_request_irq);
335
336void ibmebus_free_irq(struct ibmebus_dev *dev, u32 ist, void *dev_id)
337{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000338 unsigned int irq = irq_find_mapping(NULL, ist);
Joachim Fenkesa8308802007-03-09 18:56:46 +0100339
Heiko J Schickd7a30102005-11-16 08:56:43 +0100340 free_irq(irq, dev_id);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100341}
342EXPORT_SYMBOL(ibmebus_free_irq);
343
344static int ibmebus_bus_match(struct device *dev, struct device_driver *drv)
Joachim Fenkesa8308802007-03-09 18:56:46 +0100345{
Heiko J Schickd7a30102005-11-16 08:56:43 +0100346 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
347 struct ibmebus_driver *ebus_drv = to_ibmebus_driver(drv);
348 const struct of_device_id *ids = ebus_drv->id_table;
349 const struct of_device_id *found_id;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100350
Heiko J Schickd7a30102005-11-16 08:56:43 +0100351 if (!ids)
352 return 0;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100353
Heiko J Schickd7a30102005-11-16 08:56:43 +0100354 found_id = of_match_device(ids, &ebus_dev->ofdev);
355 if (found_id)
356 return 1;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100357
Heiko J Schickd7a30102005-11-16 08:56:43 +0100358 return 0;
359}
360
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100361static ssize_t name_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
364 struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000365 const char *name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100366 return sprintf(buf, "%s\n", name);
367}
368
369static struct device_attribute ibmebus_dev_attrs[] = {
370 __ATTR_RO(name),
371 __ATTR_NULL
372};
373
374static int ibmebus_match_helper_loc_code(struct device *dev, void *data)
375{
376 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000377 const char *loc_code;
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100378
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000379 loc_code = of_get_property(ebus_dev->ofdev.node, "ibm,loc-code", NULL);
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100380
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000381 if (loc_code && (strcmp(data, loc_code) == 0))
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100382 return 1;
383
384 return 0;
385}
386
387static ssize_t ibmebus_store_probe(struct bus_type *bus,
388 const char *buf, size_t count)
389{
390 struct device_node *dn = NULL;
391 struct ibmebus_dev *dev;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000392 const char *loc_code;
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100393 char parm[MAX_LOC_CODE_LENGTH];
394
395 if (count >= MAX_LOC_CODE_LENGTH)
396 return -EINVAL;
397 memcpy(parm, buf, count);
398 parm[count] = '\0';
399 if (parm[count-1] == '\n')
400 parm[count-1] = '\0';
401
402 if (bus_find_device(&ibmebus_bus_type, NULL, parm,
403 ibmebus_match_helper_loc_code)) {
404 printk(KERN_WARNING "%s: loc_code %s has already been probed\n",
405 __FUNCTION__, parm);
406 return -EINVAL;
407 }
408
409 while ((dn = of_find_all_nodes(dn))) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000410 loc_code = of_get_property(dn, "ibm,loc-code", NULL);
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100411 if (loc_code && (strncmp(loc_code, parm, count) == 0)) {
412 dev = ibmebus_register_device_node(dn);
413 if (IS_ERR(dev)) {
414 of_node_put(dn);
415 return PTR_ERR(dev);
416 } else
417 return count; /* success */
418 }
419 }
420
421 /* if we drop out of the loop, the loc code was invalid */
422 printk(KERN_WARNING "%s: no device with loc_code %s found\n",
423 __FUNCTION__, parm);
424 return -ENODEV;
425}
426
427static ssize_t ibmebus_store_remove(struct bus_type *bus,
428 const char *buf, size_t count)
429{
430 struct device *dev;
431 char parm[MAX_LOC_CODE_LENGTH];
432
433 if (count >= MAX_LOC_CODE_LENGTH)
434 return -EINVAL;
435 memcpy(parm, buf, count);
436 parm[count] = '\0';
437 if (parm[count-1] == '\n')
438 parm[count-1] = '\0';
439
440 /* The location code is unique, so we will find one device at most */
441 if ((dev = bus_find_device(&ibmebus_bus_type, NULL, parm,
442 ibmebus_match_helper_loc_code))) {
443 ibmebus_unregister_device(dev);
444 } else {
445 printk(KERN_WARNING "%s: loc_code %s not on the bus\n",
446 __FUNCTION__, parm);
447 return -ENODEV;
448 }
449
450 return count;
451}
452
453static struct bus_attribute ibmebus_bus_attrs[] = {
454 __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe),
455 __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove),
456 __ATTR_NULL
457};
458
Heiko J Schickd7a30102005-11-16 08:56:43 +0100459struct bus_type ibmebus_bus_type = {
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100460 .name = "ibmebus",
461 .match = ibmebus_bus_match,
462 .dev_attrs = ibmebus_dev_attrs,
463 .bus_attrs = ibmebus_bus_attrs
Heiko J Schickd7a30102005-11-16 08:56:43 +0100464};
465EXPORT_SYMBOL(ibmebus_bus_type);
466
467static int __init ibmebus_bus_init(void)
468{
469 int err;
Joachim Fenkesa8308802007-03-09 18:56:46 +0100470
Heiko J Schickd7a30102005-11-16 08:56:43 +0100471 printk(KERN_INFO "IBM eBus Device Driver\n");
Joachim Fenkesa8308802007-03-09 18:56:46 +0100472
Heiko J Schickd7a30102005-11-16 08:56:43 +0100473 err = bus_register(&ibmebus_bus_type);
474 if (err) {
475 printk(KERN_ERR ":%s: failed to register IBM eBus.\n",
476 __FUNCTION__);
477 return err;
478 }
Joachim Fenkesa8308802007-03-09 18:56:46 +0100479
Joachim Fenkes6bccf752007-03-09 19:00:32 +0100480 err = device_register(&ibmebus_bus_device);
Heiko J Schickd7a30102005-11-16 08:56:43 +0100481 if (err) {
Joachim Fenkesa8308802007-03-09 18:56:46 +0100482 printk(KERN_WARNING "%s: device_register returned %i\n",
Heiko J Schickd7a30102005-11-16 08:56:43 +0100483 __FUNCTION__, err);
484 bus_unregister(&ibmebus_bus_type);
485
486 return err;
487 }
Joachim Fenkesa8308802007-03-09 18:56:46 +0100488
Heiko J Schickd7a30102005-11-16 08:56:43 +0100489 return 0;
490}
491__initcall(ibmebus_bus_init);