| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /********************************************************************* | 
|  | 2 | * | 
|  | 3 | *	sir_dongle.c:	manager for serial dongle protocol drivers | 
|  | 4 | * | 
|  | 5 | *	Copyright (c) 2002 Martin Diehl | 
|  | 6 | * | 
|  | 7 | *	This program is free software; you can redistribute it and/or | 
|  | 8 | *	modify it under the terms of the GNU General Public License as | 
|  | 9 | *	published by the Free Software Foundation; either version 2 of | 
|  | 10 | *	the License, or (at your option) any later version. | 
|  | 11 | * | 
|  | 12 | ********************************************************************/ | 
|  | 13 |  | 
|  | 14 | #include <linux/module.h> | 
|  | 15 | #include <linux/kernel.h> | 
|  | 16 | #include <linux/init.h> | 
|  | 17 | #include <linux/smp_lock.h> | 
|  | 18 | #include <linux/kmod.h> | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 19 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | #include <net/irda/irda.h> | 
|  | 22 |  | 
|  | 23 | #include "sir-dev.h" | 
|  | 24 |  | 
|  | 25 | /************************************************************************** | 
|  | 26 | * | 
|  | 27 | * dongle registration and attachment | 
|  | 28 | * | 
|  | 29 | */ | 
|  | 30 |  | 
|  | 31 | static LIST_HEAD(dongle_list);			/* list of registered dongle drivers */ | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 32 | static DEFINE_MUTEX(dongle_list_lock);		/* protects the list */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | int irda_register_dongle(struct dongle_driver *new) | 
|  | 35 | { | 
|  | 36 | struct list_head *entry; | 
|  | 37 | struct dongle_driver *drv; | 
|  | 38 |  | 
|  | 39 | IRDA_DEBUG(0, "%s : registering dongle \"%s\" (%d).\n", | 
|  | 40 | __FUNCTION__, new->driver_name, new->type); | 
|  | 41 |  | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 42 | mutex_lock(&dongle_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | list_for_each(entry, &dongle_list) { | 
|  | 44 | drv = list_entry(entry, struct dongle_driver, dongle_list); | 
|  | 45 | if (new->type == drv->type) { | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 46 | mutex_unlock(&dongle_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | return -EEXIST; | 
|  | 48 | } | 
|  | 49 | } | 
|  | 50 | list_add(&new->dongle_list, &dongle_list); | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 51 | mutex_unlock(&dongle_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | return 0; | 
|  | 53 | } | 
| Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 54 | EXPORT_SYMBOL(irda_register_dongle); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | int irda_unregister_dongle(struct dongle_driver *drv) | 
|  | 57 | { | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 58 | mutex_lock(&dongle_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | list_del(&drv->dongle_list); | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 60 | mutex_unlock(&dongle_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return 0; | 
|  | 62 | } | 
| Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 63 | EXPORT_SYMBOL(irda_unregister_dongle); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | int sirdev_get_dongle(struct sir_dev *dev, IRDA_DONGLE type) | 
|  | 66 | { | 
|  | 67 | struct list_head *entry; | 
|  | 68 | const struct dongle_driver *drv = NULL; | 
|  | 69 | int err = -EINVAL; | 
|  | 70 |  | 
|  | 71 | #ifdef CONFIG_KMOD | 
|  | 72 | request_module("irda-dongle-%d", type); | 
|  | 73 | #endif | 
|  | 74 |  | 
|  | 75 | if (dev->dongle_drv != NULL) | 
|  | 76 | return -EBUSY; | 
|  | 77 |  | 
|  | 78 | /* serialize access to the list of registered dongles */ | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 79 | mutex_lock(&dongle_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 |  | 
|  | 81 | list_for_each(entry, &dongle_list) { | 
|  | 82 | drv = list_entry(entry, struct dongle_driver, dongle_list); | 
|  | 83 | if (drv->type == type) | 
|  | 84 | break; | 
|  | 85 | else | 
|  | 86 | drv = NULL; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | if (!drv) { | 
|  | 90 | err = -ENODEV; | 
|  | 91 | goto out_unlock;	/* no such dongle */ | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | /* handling of SMP races with dongle module removal - three cases: | 
|  | 95 | * 1) dongle driver was already unregistered - then we haven't found the | 
|  | 96 | *	requested dongle above and are already out here | 
|  | 97 | * 2) the module is already marked deleted but the driver is still | 
|  | 98 | *	registered - then the try_module_get() below will fail | 
|  | 99 | * 3) the try_module_get() below succeeds before the module is marked | 
|  | 100 | *	deleted - then sys_delete_module() fails and prevents the removal | 
|  | 101 | *	because the module is in use. | 
|  | 102 | */ | 
|  | 103 |  | 
|  | 104 | if (!try_module_get(drv->owner)) { | 
|  | 105 | err = -ESTALE; | 
|  | 106 | goto out_unlock;	/* rmmod already pending */ | 
|  | 107 | } | 
|  | 108 | dev->dongle_drv = drv; | 
|  | 109 |  | 
|  | 110 | if (!drv->open  ||  (err=drv->open(dev))!=0) | 
|  | 111 | goto out_reject;		/* failed to open driver */ | 
|  | 112 |  | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 113 | mutex_unlock(&dongle_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return 0; | 
|  | 115 |  | 
|  | 116 | out_reject: | 
|  | 117 | dev->dongle_drv = NULL; | 
|  | 118 | module_put(drv->owner); | 
|  | 119 | out_unlock: | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 120 | mutex_unlock(&dongle_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return err; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | int sirdev_put_dongle(struct sir_dev *dev) | 
|  | 125 | { | 
|  | 126 | const struct dongle_driver *drv = dev->dongle_drv; | 
|  | 127 |  | 
|  | 128 | if (drv) { | 
|  | 129 | if (drv->close) | 
|  | 130 | drv->close(dev);		/* close this dongle instance */ | 
|  | 131 |  | 
|  | 132 | dev->dongle_drv = NULL;			/* unlink the dongle driver */ | 
|  | 133 | module_put(drv->owner);/* decrement driver's module refcount */ | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | return 0; | 
|  | 137 | } |