2 * Bluetooth HCI serdev driver lib
6 * Based on hci_ldisc.c:
8 * Copyright (C) 2000-2001 Qualcomm Incorporated
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/serdev.h>
27 #include <linux/skbuff.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
34 static struct serdev_device_ops hci_serdev_client_ops;
36 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
38 struct hci_dev *hdev = hu->hdev;
40 /* Update HCI stat counters */
56 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
58 struct sk_buff *skb = hu->tx_skb;
61 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
62 skb = hu->proto->dequeue(hu);
69 static void hci_uart_write_work(struct work_struct *work)
71 struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
72 struct serdev_device *serdev = hu->serdev;
73 struct hci_dev *hdev = hu->hdev;
77 * should we cope with bad skbs or ->write() returning an error value?
80 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
82 while ((skb = hci_uart_dequeue(hu))) {
85 len = serdev_device_write_buf(serdev,
87 hdev->stat.byte_tx += len;
95 hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
98 } while (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
100 clear_bit(HCI_UART_SENDING, &hu->tx_state);
103 /* ------- Interface to HCI layer ------ */
106 static int hci_uart_flush(struct hci_dev *hdev)
108 struct hci_uart *hu = hci_get_drvdata(hdev);
110 BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
113 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
116 /* Flush any pending characters in the driver and discipline. */
117 serdev_device_write_flush(hu->serdev);
119 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
120 hu->proto->flush(hu);
125 /* Initialize device */
126 static int hci_uart_open(struct hci_dev *hdev)
128 BT_DBG("%s %p", hdev->name, hdev);
130 /* Undo clearing this from hci_uart_close() */
131 hdev->flush = hci_uart_flush;
137 static int hci_uart_close(struct hci_dev *hdev)
139 BT_DBG("hdev %p", hdev);
141 hci_uart_flush(hdev);
147 /* Send frames from HCI layer */
148 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
150 struct hci_uart *hu = hci_get_drvdata(hdev);
152 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
155 hu->proto->enqueue(hu, skb);
157 hci_uart_tx_wakeup(hu);
162 static int hci_uart_setup(struct hci_dev *hdev)
164 struct hci_uart *hu = hci_get_drvdata(hdev);
165 struct hci_rp_read_local_version *ver;
170 /* Init speed if any */
172 speed = hu->init_speed;
173 else if (hu->proto->init_speed)
174 speed = hu->proto->init_speed;
179 serdev_device_set_baudrate(hu->serdev, speed);
181 /* Operational speed if any */
183 speed = hu->oper_speed;
184 else if (hu->proto->oper_speed)
185 speed = hu->proto->oper_speed;
189 if (hu->proto->set_baudrate && speed) {
190 err = hu->proto->set_baudrate(hu, speed);
192 bt_dev_err(hdev, "Failed to set baudrate");
194 serdev_device_set_baudrate(hu->serdev, speed);
197 if (hu->proto->setup)
198 return hu->proto->setup(hu);
200 if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
203 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
206 bt_dev_err(hdev, "Reading local version info failed (%ld)",
211 if (skb->len != sizeof(*ver))
212 bt_dev_err(hdev, "Event length mismatch for version info");
218 /** hci_uart_write_wakeup - transmit buffer wakeup
219 * @serdev: serial device
221 * This function is called by the serdev framework when it accepts
222 * more data being sent.
224 static void hci_uart_write_wakeup(struct serdev_device *serdev)
226 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
230 if (!hu || serdev != hu->serdev) {
235 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
236 hci_uart_tx_wakeup(hu);
239 /** hci_uart_receive_buf - receive buffer wakeup
240 * @serdev: serial device
241 * @data: pointer to received data
242 * @count: count of received data in bytes
244 * This function is called by the serdev framework when it received data
247 * Return: number of processed bytes
249 static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
252 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
254 if (!hu || serdev != hu->serdev) {
259 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
262 /* It does not need a lock here as it is already protected by a mutex in
265 hu->proto->recv(hu, data, count);
268 hu->hdev->stat.byte_rx += count;
273 static struct serdev_device_ops hci_serdev_client_ops = {
274 .receive_buf = hci_uart_receive_buf,
275 .write_wakeup = hci_uart_write_wakeup,
278 int hci_uart_register_device(struct hci_uart *hu,
279 const struct hci_uart_proto *p)
282 struct hci_dev *hdev;
286 serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
288 err = serdev_device_open(hu->serdev);
297 set_bit(HCI_UART_PROTO_READY, &hu->flags);
299 /* Initialize and register HCI device */
300 hdev = hci_alloc_dev();
302 BT_ERR("Can't allocate HCI device");
309 hdev->bus = HCI_UART;
310 hci_set_drvdata(hdev, hu);
312 INIT_WORK(&hu->init_ready, hci_uart_init_work);
313 INIT_WORK(&hu->write_work, hci_uart_write_work);
314 percpu_init_rwsem(&hu->proto_lock);
316 /* Only when vendor specific setup callback is provided, consider
317 * the manufacturer information valid. This avoids filling in the
318 * value for Ericsson when nothing is specified.
320 if (hu->proto->setup)
321 hdev->manufacturer = hu->proto->manufacturer;
323 hdev->open = hci_uart_open;
324 hdev->close = hci_uart_close;
325 hdev->flush = hci_uart_flush;
326 hdev->send = hci_uart_send_frame;
327 hdev->setup = hci_uart_setup;
328 SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
330 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
331 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
333 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
334 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
336 if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
337 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
339 if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
340 hdev->dev_type = HCI_AMP;
342 hdev->dev_type = HCI_PRIMARY;
344 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
347 if (hci_register_dev(hdev) < 0) {
348 BT_ERR("Can't register HCI device");
353 set_bit(HCI_UART_REGISTERED, &hu->flags);
360 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
363 serdev_device_close(hu->serdev);
366 EXPORT_SYMBOL_GPL(hci_uart_register_device);
368 void hci_uart_unregister_device(struct hci_uart *hu)
370 struct hci_dev *hdev = hu->hdev;
372 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
373 hci_unregister_dev(hdev);
376 cancel_work_sync(&hu->write_work);
378 hu->proto->close(hu);
379 serdev_device_close(hu->serdev);
381 EXPORT_SYMBOL_GPL(hci_uart_unregister_device);