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