Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Bluetooth HCI driver model support. */ |
| 2 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | #include <linux/kernel.h> |
| 4 | #include <linux/init.h> |
| 5 | |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 6 | #include <linux/platform_device.h> |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <net/bluetooth/bluetooth.h> |
| 9 | #include <net/bluetooth/hci_core.h> |
| 10 | |
| 11 | #ifndef CONFIG_BT_HCI_CORE_DEBUG |
| 12 | #undef BT_DBG |
| 13 | #define BT_DBG(D...) |
| 14 | #endif |
| 15 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 16 | static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 18 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | return sprintf(buf, "%s\n", hdev->name); |
| 20 | } |
| 21 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 22 | static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 24 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | return sprintf(buf, "%d\n", hdev->type); |
| 26 | } |
| 27 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 28 | static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 30 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | bdaddr_t bdaddr; |
| 32 | baswap(&bdaddr, &hdev->bdaddr); |
| 33 | return sprintf(buf, "%s\n", batostr(&bdaddr)); |
| 34 | } |
| 35 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 36 | static ssize_t show_flags(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 38 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | return sprintf(buf, "0x%lx\n", hdev->flags); |
| 40 | } |
| 41 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 42 | static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 44 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | struct inquiry_cache *cache = &hdev->inq_cache; |
| 46 | struct inquiry_entry *e; |
| 47 | int n = 0; |
| 48 | |
| 49 | hci_dev_lock_bh(hdev); |
| 50 | |
| 51 | for (e = cache->list; e; e = e->next) { |
| 52 | struct inquiry_data *data = &e->data; |
| 53 | bdaddr_t bdaddr; |
| 54 | baswap(&bdaddr, &data->bdaddr); |
| 55 | n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n", |
| 56 | batostr(&bdaddr), |
| 57 | data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode, |
| 58 | data->dev_class[2], data->dev_class[1], data->dev_class[0], |
| 59 | __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp); |
| 60 | } |
| 61 | |
| 62 | hci_dev_unlock_bh(hdev); |
| 63 | return n; |
| 64 | } |
| 65 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 66 | static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 67 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 68 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 69 | return sprintf(buf, "%d\n", hdev->idle_timeout); |
| 70 | } |
| 71 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 72 | static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 73 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 74 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 75 | char *ptr; |
| 76 | __u32 val; |
| 77 | |
| 78 | val = simple_strtoul(buf, &ptr, 10); |
| 79 | if (ptr == buf) |
| 80 | return -EINVAL; |
| 81 | |
| 82 | if (val != 0 && (val < 500 || val > 3600000)) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | hdev->idle_timeout = val; |
| 86 | |
| 87 | return count; |
| 88 | } |
| 89 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 90 | static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 91 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 92 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 93 | return sprintf(buf, "%d\n", hdev->sniff_max_interval); |
| 94 | } |
| 95 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 96 | static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 97 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 98 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 99 | char *ptr; |
| 100 | __u16 val; |
| 101 | |
| 102 | val = simple_strtoul(buf, &ptr, 10); |
| 103 | if (ptr == buf) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | if (val < 0x0002 || val > 0xFFFE || val % 2) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | if (val < hdev->sniff_min_interval) |
| 110 | return -EINVAL; |
| 111 | |
| 112 | hdev->sniff_max_interval = val; |
| 113 | |
| 114 | return count; |
| 115 | } |
| 116 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 117 | static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 118 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 119 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 120 | return sprintf(buf, "%d\n", hdev->sniff_min_interval); |
| 121 | } |
| 122 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 123 | static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 124 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 125 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 126 | char *ptr; |
| 127 | __u16 val; |
| 128 | |
| 129 | val = simple_strtoul(buf, &ptr, 10); |
| 130 | if (ptr == buf) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | if (val < 0x0002 || val > 0xFFFE || val % 2) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | if (val > hdev->sniff_max_interval) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | hdev->sniff_min_interval = val; |
| 140 | |
| 141 | return count; |
| 142 | } |
| 143 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 144 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 145 | static DEVICE_ATTR(type, S_IRUGO, show_type, NULL); |
| 146 | static DEVICE_ATTR(address, S_IRUGO, show_address, NULL); |
| 147 | static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL); |
| 148 | static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 150 | static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR, |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 151 | show_idle_timeout, store_idle_timeout); |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 152 | static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR, |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 153 | show_sniff_max_interval, store_sniff_max_interval); |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 154 | static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR, |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 155 | show_sniff_min_interval, store_sniff_min_interval); |
| 156 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 157 | static struct device_attribute *bt_attrs[] = { |
| 158 | &dev_attr_name, |
| 159 | &dev_attr_type, |
| 160 | &dev_attr_address, |
| 161 | &dev_attr_flags, |
| 162 | &dev_attr_inquiry_cache, |
| 163 | &dev_attr_idle_timeout, |
| 164 | &dev_attr_sniff_max_interval, |
| 165 | &dev_attr_sniff_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | NULL |
| 167 | }; |
| 168 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 169 | struct class *bt_class = NULL; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 170 | EXPORT_SYMBOL_GPL(bt_class); |
| 171 | |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 172 | static struct bus_type bt_bus = { |
| 173 | .name = "bluetooth", |
| 174 | }; |
| 175 | |
| 176 | static struct platform_device *bt_platform; |
| 177 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 178 | static void bt_release(struct device *dev) |
| 179 | { |
| 180 | struct hci_dev *hdev = dev_get_drvdata(dev); |
| 181 | kfree(hdev); |
| 182 | } |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int hci_register_sysfs(struct hci_dev *hdev) |
| 185 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 186 | struct device *dev = &hdev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | unsigned int i; |
| 188 | int err; |
| 189 | |
| 190 | BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); |
| 191 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 192 | dev->class = bt_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 194 | if (hdev->parent) |
| 195 | dev->parent = hdev->parent; |
| 196 | else |
| 197 | dev->parent = &bt_platform->dev; |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 198 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 199 | strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE); |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 200 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 201 | dev->release = bt_release; |
| 202 | |
| 203 | dev_set_drvdata(dev, hdev); |
| 204 | |
| 205 | err = device_register(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (err < 0) |
| 207 | return err; |
| 208 | |
| 209 | for (i = 0; bt_attrs[i]; i++) |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 210 | device_create_file(dev, bt_attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | void hci_unregister_sysfs(struct hci_dev *hdev) |
| 216 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 217 | struct device *dev = &hdev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); |
| 220 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 221 | device_del(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | int __init bt_sysfs_init(void) |
| 225 | { |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 226 | int err; |
| 227 | |
| 228 | bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0); |
| 229 | if (IS_ERR(bt_platform)) |
| 230 | return PTR_ERR(bt_platform); |
| 231 | |
| 232 | err = bus_register(&bt_bus); |
| 233 | if (err < 0) { |
| 234 | platform_device_unregister(bt_platform); |
| 235 | return err; |
| 236 | } |
| 237 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 238 | bt_class = class_create(THIS_MODULE, "bluetooth"); |
| 239 | if (IS_ERR(bt_class)) { |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 240 | bus_unregister(&bt_bus); |
| 241 | platform_device_unregister(bt_platform); |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 242 | return PTR_ERR(bt_class); |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void __exit bt_sysfs_cleanup(void) |
| 249 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame^] | 250 | class_destroy(bt_class); |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 251 | |
| 252 | bus_unregister(&bt_bus); |
| 253 | |
| 254 | platform_device_unregister(bt_platform); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |