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