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 skb = hu->proto->dequeue(hu);
68 static void hci_uart_write_work(struct work_struct *work)
70 struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
71 struct serdev_device *serdev = hu->serdev;
72 struct hci_dev *hdev = hu->hdev;
76 * should we cope with bad skbs or ->write() returning an error value?
79 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
81 while ((skb = hci_uart_dequeue(hu))) {
84 len = serdev_device_write_buf(serdev,
86 hdev->stat.byte_tx += len;
94 hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
97 } while(test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
99 clear_bit(HCI_UART_SENDING, &hu->tx_state);
102 /* ------- Interface to HCI layer ------ */
105 static int hci_uart_flush(struct hci_dev *hdev)
107 struct hci_uart *hu = hci_get_drvdata(hdev);
109 BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
112 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
115 /* Flush any pending characters in the driver and discipline. */
116 serdev_device_write_flush(hu->serdev);
118 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
119 hu->proto->flush(hu);
124 /* Initialize device */
125 static int hci_uart_open(struct hci_dev *hdev)
127 BT_DBG("%s %p", hdev->name, hdev);
129 /* Undo clearing this from hci_uart_close() */
130 hdev->flush = hci_uart_flush;
136 static int hci_uart_close(struct hci_dev *hdev)
138 BT_DBG("hdev %p", hdev);
140 hci_uart_flush(hdev);
146 /* Send frames from HCI layer */
147 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
149 struct hci_uart *hu = hci_get_drvdata(hdev);
151 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
154 hu->proto->enqueue(hu, skb);
156 hci_uart_tx_wakeup(hu);
161 static int hci_uart_setup(struct hci_dev *hdev)
163 struct hci_uart *hu = hci_get_drvdata(hdev);
164 struct hci_rp_read_local_version *ver;
169 /* Init speed if any */
171 speed = hu->init_speed;
172 else if (hu->proto->init_speed)
173 speed = hu->proto->init_speed;
178 serdev_device_set_baudrate(hu->serdev, speed);
180 /* Operational speed if any */
182 speed = hu->oper_speed;
183 else if (hu->proto->oper_speed)
184 speed = hu->proto->oper_speed;
188 if (hu->proto->set_baudrate && speed) {
189 err = hu->proto->set_baudrate(hu, speed);
191 bt_dev_err(hdev, "Failed to set baudrate");
193 serdev_device_set_baudrate(hu->serdev, speed);
196 if (hu->proto->setup)
197 return hu->proto->setup(hu);
199 if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
202 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
205 bt_dev_err(hdev, "Reading local version info failed (%ld)",
210 if (skb->len != sizeof(*ver))
211 bt_dev_err(hdev, "Event length mismatch for version info");
217 /** hci_uart_write_wakeup - transmit buffer wakeup
218 * @serdev: serial device
220 * This function is called by the serdev framework when it accepts
221 * more data being sent.
223 static void hci_uart_write_wakeup(struct serdev_device *serdev)
225 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
229 if (!hu || serdev != hu->serdev) {
234 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
235 hci_uart_tx_wakeup(hu);
238 /** hci_uart_receive_buf - receive buffer wakeup
239 * @serdev: serial device
240 * @data: pointer to received data
241 * @count: count of received data in bytes
243 * This function is called by the serdev framework when it received data
246 * Return: number of processed bytes
248 static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
251 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
253 if (!hu || serdev != hu->serdev) {
258 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
261 /* It does not need a lock here as it is already protected by a mutex in
264 hu->proto->recv(hu, data, count);
267 hu->hdev->stat.byte_rx += count;
272 static struct serdev_device_ops hci_serdev_client_ops = {
273 .receive_buf = hci_uart_receive_buf,
274 .write_wakeup = hci_uart_write_wakeup,
277 int hci_uart_register_device(struct hci_uart *hu,
278 const struct hci_uart_proto *p)
281 struct hci_dev *hdev;
285 serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
287 err = serdev_device_open(hu->serdev);
296 set_bit(HCI_UART_PROTO_READY, &hu->flags);
298 /* Initialize and register HCI device */
299 hdev = hci_alloc_dev();
301 BT_ERR("Can't allocate HCI device");
308 hdev->bus = HCI_UART;
309 hci_set_drvdata(hdev, hu);
311 INIT_WORK(&hu->init_ready, hci_uart_init_work);
312 INIT_WORK(&hu->write_work, hci_uart_write_work);
313 percpu_init_rwsem(&hu->proto_lock);
315 /* Only when vendor specific setup callback is provided, consider
316 * the manufacturer information valid. This avoids filling in the
317 * value for Ericsson when nothing is specified.
319 if (hu->proto->setup)
320 hdev->manufacturer = hu->proto->manufacturer;
322 hdev->open = hci_uart_open;
323 hdev->close = hci_uart_close;
324 hdev->flush = hci_uart_flush;
325 hdev->send = hci_uart_send_frame;
326 hdev->setup = hci_uart_setup;
327 SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
329 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
330 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
332 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
333 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
335 if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
336 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
338 if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
339 hdev->dev_type = HCI_AMP;
341 hdev->dev_type = HCI_PRIMARY;
343 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
346 if (hci_register_dev(hdev) < 0) {
347 BT_ERR("Can't register HCI device");
352 set_bit(HCI_UART_REGISTERED, &hu->flags);
359 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
362 serdev_device_close(hu->serdev);
365 EXPORT_SYMBOL_GPL(hci_uart_register_device);
367 void hci_uart_unregister_device(struct hci_uart *hu)
369 struct hci_dev *hdev = hu->hdev;
371 hci_unregister_dev(hdev);
374 cancel_work_sync(&hu->write_work);
376 hu->proto->close(hu);
377 serdev_device_close(hu->serdev);
379 EXPORT_SYMBOL_GPL(hci_uart_unregister_device);