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 | 4d0eb00 | 2006-07-06 12:34:41 +0200 | [diff] [blame] | 16 | static inline char *typetostr(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | { |
Marcel Holtmann | 4d0eb00 | 2006-07-06 12:34:41 +0200 | [diff] [blame] | 18 | switch (type) { |
| 19 | case HCI_VHCI: |
| 20 | return "VIRTUAL"; |
| 21 | case HCI_USB: |
| 22 | return "USB"; |
| 23 | case HCI_PCCARD: |
| 24 | return "PCCARD"; |
| 25 | case HCI_UART: |
| 26 | return "UART"; |
| 27 | case HCI_RS232: |
| 28 | return "RS232"; |
| 29 | case HCI_PCI: |
| 30 | return "PCI"; |
| 31 | default: |
| 32 | return "UNKNOWN"; |
| 33 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 36 | 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] | 37 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 38 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 4d0eb00 | 2006-07-06 12:34:41 +0200 | [diff] [blame] | 39 | return sprintf(buf, "%s\n", typetostr(hdev->type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 42 | 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] | 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 | bdaddr_t bdaddr; |
| 46 | baswap(&bdaddr, &hdev->bdaddr); |
| 47 | return sprintf(buf, "%s\n", batostr(&bdaddr)); |
| 48 | } |
| 49 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 50 | 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] | 51 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 52 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | struct inquiry_cache *cache = &hdev->inq_cache; |
| 54 | struct inquiry_entry *e; |
| 55 | int n = 0; |
| 56 | |
| 57 | hci_dev_lock_bh(hdev); |
| 58 | |
| 59 | for (e = cache->list; e; e = e->next) { |
| 60 | struct inquiry_data *data = &e->data; |
| 61 | bdaddr_t bdaddr; |
| 62 | baswap(&bdaddr, &data->bdaddr); |
| 63 | n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n", |
| 64 | batostr(&bdaddr), |
| 65 | data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode, |
| 66 | data->dev_class[2], data->dev_class[1], data->dev_class[0], |
| 67 | __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp); |
| 68 | } |
| 69 | |
| 70 | hci_dev_unlock_bh(hdev); |
| 71 | return n; |
| 72 | } |
| 73 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 74 | 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] | 75 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 76 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 77 | return sprintf(buf, "%d\n", hdev->idle_timeout); |
| 78 | } |
| 79 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 80 | 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] | 81 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 82 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 83 | char *ptr; |
| 84 | __u32 val; |
| 85 | |
| 86 | val = simple_strtoul(buf, &ptr, 10); |
| 87 | if (ptr == buf) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | if (val != 0 && (val < 500 || val > 3600000)) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | hdev->idle_timeout = val; |
| 94 | |
| 95 | return count; |
| 96 | } |
| 97 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 98 | 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] | 99 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 100 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 101 | return sprintf(buf, "%d\n", hdev->sniff_max_interval); |
| 102 | } |
| 103 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 104 | 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] | 105 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 106 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 107 | char *ptr; |
| 108 | __u16 val; |
| 109 | |
| 110 | val = simple_strtoul(buf, &ptr, 10); |
| 111 | if (ptr == buf) |
| 112 | return -EINVAL; |
| 113 | |
| 114 | if (val < 0x0002 || val > 0xFFFE || val % 2) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | if (val < hdev->sniff_min_interval) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | hdev->sniff_max_interval = val; |
| 121 | |
| 122 | return count; |
| 123 | } |
| 124 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 125 | 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] | 126 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 127 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 128 | return sprintf(buf, "%d\n", hdev->sniff_min_interval); |
| 129 | } |
| 130 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 131 | 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] | 132 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 133 | struct hci_dev *hdev = dev_get_drvdata(dev); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 134 | char *ptr; |
| 135 | __u16 val; |
| 136 | |
| 137 | val = simple_strtoul(buf, &ptr, 10); |
| 138 | if (ptr == buf) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | if (val < 0x0002 || val > 0xFFFE || val % 2) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | if (val > hdev->sniff_max_interval) |
| 145 | return -EINVAL; |
| 146 | |
| 147 | hdev->sniff_min_interval = val; |
| 148 | |
| 149 | return count; |
| 150 | } |
| 151 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 152 | static DEVICE_ATTR(type, S_IRUGO, show_type, NULL); |
| 153 | static DEVICE_ATTR(address, S_IRUGO, show_address, NULL); |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 154 | static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 156 | static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR, |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 157 | show_idle_timeout, store_idle_timeout); |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 158 | static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR, |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 159 | show_sniff_max_interval, store_sniff_max_interval); |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 160 | static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR, |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 161 | show_sniff_min_interval, store_sniff_min_interval); |
| 162 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 163 | static struct device_attribute *bt_attrs[] = { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 164 | &dev_attr_type, |
| 165 | &dev_attr_address, |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 166 | &dev_attr_inquiry_cache, |
| 167 | &dev_attr_idle_timeout, |
| 168 | &dev_attr_sniff_max_interval, |
| 169 | &dev_attr_sniff_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | NULL |
| 171 | }; |
| 172 | |
Marcel Holtmann | b219e3a | 2006-07-06 12:38:46 +0200 | [diff] [blame^] | 173 | static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf) |
| 174 | { |
| 175 | struct hci_conn *conn = dev_get_drvdata(dev); |
| 176 | return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO"); |
| 177 | } |
| 178 | |
| 179 | static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf) |
| 180 | { |
| 181 | struct hci_conn *conn = dev_get_drvdata(dev); |
| 182 | bdaddr_t bdaddr; |
| 183 | baswap(&bdaddr, &conn->dst); |
| 184 | return sprintf(buf, "%s\n", batostr(&bdaddr)); |
| 185 | } |
| 186 | |
| 187 | #define CONN_ATTR(_name,_mode,_show,_store) \ |
| 188 | struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store) |
| 189 | |
| 190 | static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL); |
| 191 | static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL); |
| 192 | |
| 193 | static struct device_attribute *conn_attrs[] = { |
| 194 | &conn_attr_type, |
| 195 | &conn_attr_address, |
| 196 | NULL |
| 197 | }; |
| 198 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 199 | struct class *bt_class = NULL; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(bt_class); |
| 201 | |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 202 | static struct bus_type bt_bus = { |
| 203 | .name = "bluetooth", |
| 204 | }; |
| 205 | |
| 206 | static struct platform_device *bt_platform; |
| 207 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 208 | static void bt_release(struct device *dev) |
| 209 | { |
Marcel Holtmann | b219e3a | 2006-07-06 12:38:46 +0200 | [diff] [blame^] | 210 | void *data = dev_get_drvdata(dev); |
| 211 | kfree(data); |
| 212 | } |
| 213 | |
| 214 | static void add_conn(void *data) |
| 215 | { |
| 216 | struct hci_conn *conn = data; |
| 217 | int i; |
| 218 | |
| 219 | device_register(&conn->dev); |
| 220 | |
| 221 | for (i = 0; conn_attrs[i]; i++) |
| 222 | device_create_file(&conn->dev, conn_attrs[i]); |
| 223 | } |
| 224 | |
| 225 | void hci_conn_add_sysfs(struct hci_conn *conn) |
| 226 | { |
| 227 | struct hci_dev *hdev = conn->hdev; |
| 228 | bdaddr_t *ba = &conn->dst; |
| 229 | |
| 230 | BT_DBG("conn %p", conn); |
| 231 | |
| 232 | conn->dev.parent = &hdev->dev; |
| 233 | conn->dev.release = bt_release; |
| 234 | |
| 235 | snprintf(conn->dev.bus_id, BUS_ID_SIZE, |
| 236 | "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X", |
| 237 | conn->type == ACL_LINK ? "acl" : "sco", |
| 238 | ba->b[5], ba->b[4], ba->b[3], |
| 239 | ba->b[2], ba->b[1], ba->b[0]); |
| 240 | |
| 241 | dev_set_drvdata(&conn->dev, conn); |
| 242 | |
| 243 | INIT_WORK(&conn->work, add_conn, (void *) conn); |
| 244 | |
| 245 | schedule_work(&conn->work); |
| 246 | } |
| 247 | |
| 248 | static void del_conn(void *data) |
| 249 | { |
| 250 | struct hci_conn *conn = data; |
| 251 | device_del(&conn->dev); |
| 252 | } |
| 253 | |
| 254 | void hci_conn_del_sysfs(struct hci_conn *conn) |
| 255 | { |
| 256 | BT_DBG("conn %p", conn); |
| 257 | |
| 258 | INIT_WORK(&conn->work, del_conn, (void *) conn); |
| 259 | |
| 260 | schedule_work(&conn->work); |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | int hci_register_sysfs(struct hci_dev *hdev) |
| 264 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 265 | struct device *dev = &hdev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | unsigned int i; |
| 267 | int err; |
| 268 | |
| 269 | BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); |
| 270 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 271 | dev->class = bt_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 273 | if (hdev->parent) |
| 274 | dev->parent = hdev->parent; |
| 275 | else |
| 276 | dev->parent = &bt_platform->dev; |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 277 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 278 | strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE); |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 279 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 280 | dev->release = bt_release; |
| 281 | |
| 282 | dev_set_drvdata(dev, hdev); |
| 283 | |
| 284 | err = device_register(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (err < 0) |
| 286 | return err; |
| 287 | |
| 288 | for (i = 0; bt_attrs[i]; i++) |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 289 | device_create_file(dev, bt_attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | void hci_unregister_sysfs(struct hci_dev *hdev) |
| 295 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); |
| 297 | |
Marcel Holtmann | 4d0eb00 | 2006-07-06 12:34:41 +0200 | [diff] [blame] | 298 | device_del(&hdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | int __init bt_sysfs_init(void) |
| 302 | { |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 303 | int err; |
| 304 | |
| 305 | bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0); |
| 306 | if (IS_ERR(bt_platform)) |
| 307 | return PTR_ERR(bt_platform); |
| 308 | |
| 309 | err = bus_register(&bt_bus); |
| 310 | if (err < 0) { |
| 311 | platform_device_unregister(bt_platform); |
| 312 | return err; |
| 313 | } |
| 314 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 315 | bt_class = class_create(THIS_MODULE, "bluetooth"); |
| 316 | if (IS_ERR(bt_class)) { |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 317 | bus_unregister(&bt_bus); |
| 318 | platform_device_unregister(bt_platform); |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 319 | return PTR_ERR(bt_class); |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void __exit bt_sysfs_cleanup(void) |
| 326 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 327 | class_destroy(bt_class); |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 328 | |
| 329 | bus_unregister(&bt_bus); |
| 330 | |
| 331 | platform_device_unregister(bt_platform); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |