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 | |
| 6 | #include <net/bluetooth/bluetooth.h> |
| 7 | #include <net/bluetooth/hci_core.h> |
| 8 | |
| 9 | #ifndef CONFIG_BT_HCI_CORE_DEBUG |
| 10 | #undef BT_DBG |
| 11 | #define BT_DBG(D...) |
| 12 | #endif |
| 13 | |
| 14 | static ssize_t show_name(struct class_device *cdev, char *buf) |
| 15 | { |
| 16 | struct hci_dev *hdev = class_get_devdata(cdev); |
| 17 | return sprintf(buf, "%s\n", hdev->name); |
| 18 | } |
| 19 | |
| 20 | static ssize_t show_type(struct class_device *cdev, char *buf) |
| 21 | { |
| 22 | struct hci_dev *hdev = class_get_devdata(cdev); |
| 23 | return sprintf(buf, "%d\n", hdev->type); |
| 24 | } |
| 25 | |
| 26 | static ssize_t show_address(struct class_device *cdev, char *buf) |
| 27 | { |
| 28 | struct hci_dev *hdev = class_get_devdata(cdev); |
| 29 | bdaddr_t bdaddr; |
| 30 | baswap(&bdaddr, &hdev->bdaddr); |
| 31 | return sprintf(buf, "%s\n", batostr(&bdaddr)); |
| 32 | } |
| 33 | |
| 34 | static ssize_t show_flags(struct class_device *cdev, char *buf) |
| 35 | { |
| 36 | struct hci_dev *hdev = class_get_devdata(cdev); |
| 37 | return sprintf(buf, "0x%lx\n", hdev->flags); |
| 38 | } |
| 39 | |
| 40 | static ssize_t show_inquiry_cache(struct class_device *cdev, char *buf) |
| 41 | { |
| 42 | struct hci_dev *hdev = class_get_devdata(cdev); |
| 43 | struct inquiry_cache *cache = &hdev->inq_cache; |
| 44 | struct inquiry_entry *e; |
| 45 | int n = 0; |
| 46 | |
| 47 | hci_dev_lock_bh(hdev); |
| 48 | |
| 49 | for (e = cache->list; e; e = e->next) { |
| 50 | struct inquiry_data *data = &e->data; |
| 51 | bdaddr_t bdaddr; |
| 52 | baswap(&bdaddr, &data->bdaddr); |
| 53 | n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n", |
| 54 | batostr(&bdaddr), |
| 55 | data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode, |
| 56 | data->dev_class[2], data->dev_class[1], data->dev_class[0], |
| 57 | __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp); |
| 58 | } |
| 59 | |
| 60 | hci_dev_unlock_bh(hdev); |
| 61 | return n; |
| 62 | } |
| 63 | |
| 64 | static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 65 | static CLASS_DEVICE_ATTR(type, S_IRUGO, show_type, NULL); |
| 66 | static CLASS_DEVICE_ATTR(address, S_IRUGO, show_address, NULL); |
| 67 | static CLASS_DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL); |
| 68 | static CLASS_DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL); |
| 69 | |
| 70 | static struct class_device_attribute *bt_attrs[] = { |
| 71 | &class_device_attr_name, |
| 72 | &class_device_attr_type, |
| 73 | &class_device_attr_address, |
| 74 | &class_device_attr_flags, |
| 75 | &class_device_attr_inquiry_cache, |
| 76 | NULL |
| 77 | }; |
| 78 | |
| 79 | #ifdef CONFIG_HOTPLUG |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 80 | static int bt_uevent(struct class_device *cdev, char **envp, int num_envp, char *buf, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
| 82 | struct hci_dev *hdev = class_get_devdata(cdev); |
| 83 | int n, i = 0; |
| 84 | |
| 85 | envp[i++] = buf; |
| 86 | n = snprintf(buf, size, "INTERFACE=%s", hdev->name) + 1; |
| 87 | buf += n; |
| 88 | size -= n; |
| 89 | |
| 90 | if ((size <= 0) || (i >= num_envp)) |
| 91 | return -ENOMEM; |
| 92 | |
| 93 | envp[i] = NULL; |
| 94 | return 0; |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | static void bt_release(struct class_device *cdev) |
| 99 | { |
| 100 | struct hci_dev *hdev = class_get_devdata(cdev); |
| 101 | |
| 102 | kfree(hdev); |
| 103 | } |
| 104 | |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 105 | struct class bt_class = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | .name = "bluetooth", |
| 107 | .release = bt_release, |
| 108 | #ifdef CONFIG_HOTPLUG |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 109 | .uevent = bt_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #endif |
| 111 | }; |
| 112 | |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 113 | EXPORT_SYMBOL_GPL(bt_class); |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | int hci_register_sysfs(struct hci_dev *hdev) |
| 116 | { |
| 117 | struct class_device *cdev = &hdev->class_dev; |
| 118 | unsigned int i; |
| 119 | int err; |
| 120 | |
| 121 | BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); |
| 122 | |
| 123 | cdev->class = &bt_class; |
| 124 | class_set_devdata(cdev, hdev); |
| 125 | |
| 126 | strlcpy(cdev->class_id, hdev->name, BUS_ID_SIZE); |
| 127 | err = class_device_register(cdev); |
| 128 | if (err < 0) |
| 129 | return err; |
| 130 | |
| 131 | for (i = 0; bt_attrs[i]; i++) |
| 132 | class_device_create_file(cdev, bt_attrs[i]); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | void hci_unregister_sysfs(struct hci_dev *hdev) |
| 138 | { |
| 139 | struct class_device * cdev = &hdev->class_dev; |
| 140 | |
| 141 | BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); |
| 142 | |
| 143 | class_device_del(cdev); |
| 144 | } |
| 145 | |
| 146 | int __init bt_sysfs_init(void) |
| 147 | { |
| 148 | return class_register(&bt_class); |
| 149 | } |
| 150 | |
| 151 | void __exit bt_sysfs_cleanup(void) |
| 152 | { |
| 153 | class_unregister(&bt_class); |
| 154 | } |