Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* Bluetooth HCI driver model support. */ |
| 3 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 4 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | #include <net/bluetooth/bluetooth.h> |
| 7 | #include <net/bluetooth/hci_core.h> |
| 8 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 9 | static struct class *bt_class; |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 10 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 11 | static void bt_link_release(struct device *dev) |
| 12 | { |
David Herrmann | 2dd1068 | 2012-02-09 21:58:34 +0100 | [diff] [blame] | 13 | struct hci_conn *conn = to_hci_conn(dev); |
| 14 | kfree(conn); |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 15 | } |
| 16 | |
Bhumika Goyal | 9374bf1 | 2017-08-19 14:54:47 +0530 | [diff] [blame] | 17 | static const struct device_type bt_link = { |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 18 | .name = "link", |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 19 | .release = bt_link_release, |
| 20 | }; |
| 21 | |
Gustavo F. Padovan | 6d438e3 | 2011-12-17 18:53:02 -0200 | [diff] [blame] | 22 | /* |
| 23 | * The rfcomm tty device will possibly retain even when conn |
| 24 | * is down, and sysfs doesn't support move zombie device, |
| 25 | * so we should move the device before conn device is destroyed. |
| 26 | */ |
| 27 | static int __match_tty(struct device *dev, void *data) |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 28 | { |
Gustavo F. Padovan | 6d438e3 | 2011-12-17 18:53:02 -0200 | [diff] [blame] | 29 | return !strncmp(dev_name(dev), "rfcomm", 6); |
| 30 | } |
| 31 | |
| 32 | void hci_conn_init_sysfs(struct hci_conn *conn) |
| 33 | { |
Marcel Holtmann | 457ca7b | 2009-05-05 13:09:01 -0700 | [diff] [blame] | 34 | struct hci_dev *hdev = conn->hdev; |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 35 | |
Gustavo F. Padovan | 6d438e3 | 2011-12-17 18:53:02 -0200 | [diff] [blame] | 36 | BT_DBG("conn %p", conn); |
| 37 | |
| 38 | conn->dev.type = &bt_link; |
| 39 | conn->dev.class = bt_class; |
| 40 | conn->dev.parent = &hdev->dev; |
| 41 | |
| 42 | device_initialize(&conn->dev); |
| 43 | } |
| 44 | |
| 45 | void hci_conn_add_sysfs(struct hci_conn *conn) |
| 46 | { |
| 47 | struct hci_dev *hdev = conn->hdev; |
| 48 | |
| 49 | BT_DBG("conn %p", conn); |
| 50 | |
Marcel Holtmann | 457ca7b | 2009-05-05 13:09:01 -0700 | [diff] [blame] | 51 | dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle); |
| 52 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 53 | if (device_add(&conn->dev) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 54 | bt_dev_err(hdev, "failed to register connection device"); |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 55 | return; |
| 56 | } |
Marcel Holtmann | 384943e | 2009-05-08 18:20:43 -0700 | [diff] [blame] | 57 | |
| 58 | hci_dev_hold(hdev); |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Gustavo F. Padovan | 6d438e3 | 2011-12-17 18:53:02 -0200 | [diff] [blame] | 61 | void hci_conn_del_sysfs(struct hci_conn *conn) |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 62 | { |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 63 | struct hci_dev *hdev = conn->hdev; |
| 64 | |
Marcel Holtmann | a67e899 | 2009-05-02 18:24:06 -0700 | [diff] [blame] | 65 | if (!device_is_registered(&conn->dev)) |
| 66 | return; |
Roger Quadros | f3784d8 | 2009-04-23 14:50:54 +0300 | [diff] [blame] | 67 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 68 | while (1) { |
| 69 | struct device *dev; |
| 70 | |
| 71 | dev = device_find_child(&conn->dev, NULL, __match_tty); |
| 72 | if (!dev) |
| 73 | break; |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 74 | device_move(dev, NULL, DPM_ORDER_DEV_LAST); |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 75 | put_device(dev); |
| 76 | } |
| 77 | |
| 78 | device_del(&conn->dev); |
Marcel Holtmann | 384943e | 2009-05-08 18:20:43 -0700 | [diff] [blame] | 79 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 80 | hci_dev_put(hdev); |
| 81 | } |
| 82 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 83 | static void bt_host_release(struct device *dev) |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 84 | { |
David Herrmann | 2dd1068 | 2012-02-09 21:58:34 +0100 | [diff] [blame] | 85 | struct hci_dev *hdev = to_hci_dev(dev); |
| 86 | kfree(hdev); |
David Herrmann | 46e0653 | 2012-01-07 15:47:21 +0100 | [diff] [blame] | 87 | module_put(THIS_MODULE); |
Marcel Holtmann | b219e3a | 2006-07-06 12:38:46 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Bhumika Goyal | 9374bf1 | 2017-08-19 14:54:47 +0530 | [diff] [blame] | 90 | static const struct device_type bt_host = { |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 91 | .name = "host", |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 92 | .release = bt_host_release, |
| 93 | }; |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 94 | |
David Herrmann | 0ac7e70 | 2011-10-08 14:58:47 +0200 | [diff] [blame] | 95 | void hci_init_sysfs(struct hci_dev *hdev) |
| 96 | { |
| 97 | struct device *dev = &hdev->dev; |
| 98 | |
| 99 | dev->type = &bt_host; |
| 100 | dev->class = bt_class; |
| 101 | |
David Herrmann | 46e0653 | 2012-01-07 15:47:21 +0100 | [diff] [blame] | 102 | __module_get(THIS_MODULE); |
David Herrmann | 0ac7e70 | 2011-10-08 14:58:47 +0200 | [diff] [blame] | 103 | device_initialize(dev); |
| 104 | } |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | int __init bt_sysfs_init(void) |
| 107 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 108 | bt_class = class_create(THIS_MODULE, "bluetooth"); |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 109 | |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 110 | return PTR_ERR_OR_ZERO(bt_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Arnaud Patard | 860e13b | 2006-09-28 15:29:37 -0700 | [diff] [blame] | 113 | void bt_sysfs_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 115 | class_destroy(bt_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |