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