uart.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015, Marvell International Ltd.
  4. *
  5. * Inspired (hugely) by HCI LDISC implementation in Bluetooth.
  6. *
  7. * Copyright (C) 2000-2001 Qualcomm Incorporated
  8. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  9. * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/types.h>
  15. #include <linux/fcntl.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/poll.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/errno.h>
  22. #include <linux/string.h>
  23. #include <linux/signal.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/skbuff.h>
  26. #include <net/nfc/nci.h>
  27. #include <net/nfc/nci_core.h>
  28. /* TX states */
  29. #define NCI_UART_SENDING 1
  30. #define NCI_UART_TX_WAKEUP 2
  31. static struct nci_uart *nci_uart_drivers[NCI_UART_DRIVER_MAX];
  32. static inline struct sk_buff *nci_uart_dequeue(struct nci_uart *nu)
  33. {
  34. struct sk_buff *skb = nu->tx_skb;
  35. if (!skb)
  36. skb = skb_dequeue(&nu->tx_q);
  37. else
  38. nu->tx_skb = NULL;
  39. return skb;
  40. }
  41. static inline int nci_uart_queue_empty(struct nci_uart *nu)
  42. {
  43. if (nu->tx_skb)
  44. return 0;
  45. return skb_queue_empty(&nu->tx_q);
  46. }
  47. static int nci_uart_tx_wakeup(struct nci_uart *nu)
  48. {
  49. if (test_and_set_bit(NCI_UART_SENDING, &nu->tx_state)) {
  50. set_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
  51. return 0;
  52. }
  53. schedule_work(&nu->write_work);
  54. return 0;
  55. }
  56. static void nci_uart_write_work(struct work_struct *work)
  57. {
  58. struct nci_uart *nu = container_of(work, struct nci_uart, write_work);
  59. struct tty_struct *tty = nu->tty;
  60. struct sk_buff *skb;
  61. restart:
  62. clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
  63. if (nu->ops.tx_start)
  64. nu->ops.tx_start(nu);
  65. while ((skb = nci_uart_dequeue(nu))) {
  66. int len;
  67. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  68. len = tty->ops->write(tty, skb->data, skb->len);
  69. skb_pull(skb, len);
  70. if (skb->len) {
  71. nu->tx_skb = skb;
  72. break;
  73. }
  74. kfree_skb(skb);
  75. }
  76. if (test_bit(NCI_UART_TX_WAKEUP, &nu->tx_state))
  77. goto restart;
  78. if (nu->ops.tx_done && nci_uart_queue_empty(nu))
  79. nu->ops.tx_done(nu);
  80. clear_bit(NCI_UART_SENDING, &nu->tx_state);
  81. }
  82. static int nci_uart_set_driver(struct tty_struct *tty, unsigned int driver)
  83. {
  84. struct nci_uart *nu = NULL;
  85. int ret;
  86. if (driver >= NCI_UART_DRIVER_MAX)
  87. return -EINVAL;
  88. if (!nci_uart_drivers[driver])
  89. return -ENOENT;
  90. nu = kzalloc_obj(*nu);
  91. if (!nu)
  92. return -ENOMEM;
  93. memcpy(nu, nci_uart_drivers[driver], sizeof(struct nci_uart));
  94. nu->tty = tty;
  95. skb_queue_head_init(&nu->tx_q);
  96. INIT_WORK(&nu->write_work, nci_uart_write_work);
  97. spin_lock_init(&nu->rx_lock);
  98. ret = nu->ops.open(nu);
  99. if (ret) {
  100. kfree(nu);
  101. return ret;
  102. } else if (!try_module_get(nu->owner)) {
  103. nu->ops.close(nu);
  104. kfree(nu);
  105. return -ENOENT;
  106. }
  107. tty->disc_data = nu;
  108. return 0;
  109. }
  110. /* ------ LDISC part ------ */
  111. /* nci_uart_tty_open
  112. *
  113. * Called when line discipline changed to NCI_UART.
  114. *
  115. * Arguments:
  116. * tty pointer to tty info structure
  117. * Return Value:
  118. * 0 if success, otherwise error code
  119. */
  120. static int nci_uart_tty_open(struct tty_struct *tty)
  121. {
  122. /* Error if the tty has no write op instead of leaving an exploitable
  123. * hole
  124. */
  125. if (!tty->ops->write)
  126. return -EOPNOTSUPP;
  127. tty->disc_data = NULL;
  128. tty->receive_room = 65536;
  129. /* Flush any pending characters in the driver */
  130. tty_driver_flush_buffer(tty);
  131. return 0;
  132. }
  133. /* nci_uart_tty_close()
  134. *
  135. * Called when the line discipline is changed to something
  136. * else, the tty is closed, or the tty detects a hangup.
  137. */
  138. static void nci_uart_tty_close(struct tty_struct *tty)
  139. {
  140. struct nci_uart *nu = tty->disc_data;
  141. /* Detach from the tty */
  142. tty->disc_data = NULL;
  143. if (!nu)
  144. return;
  145. kfree_skb(nu->tx_skb);
  146. kfree_skb(nu->rx_skb);
  147. skb_queue_purge(&nu->tx_q);
  148. nu->ops.close(nu);
  149. nu->tty = NULL;
  150. module_put(nu->owner);
  151. cancel_work_sync(&nu->write_work);
  152. kfree(nu);
  153. }
  154. /* nci_uart_tty_wakeup()
  155. *
  156. * Callback for transmit wakeup. Called when low level
  157. * device driver can accept more send data.
  158. *
  159. * Arguments: tty pointer to associated tty instance data
  160. * Return Value: None
  161. */
  162. static void nci_uart_tty_wakeup(struct tty_struct *tty)
  163. {
  164. struct nci_uart *nu = tty->disc_data;
  165. if (!nu)
  166. return;
  167. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  168. if (tty != nu->tty)
  169. return;
  170. nci_uart_tx_wakeup(nu);
  171. }
  172. /* -- Default recv_buf handler --
  173. *
  174. * This handler supposes that NCI frames are sent over UART link without any
  175. * framing. It reads NCI header, retrieve the packet size and once all packet
  176. * bytes are received it passes it to nci_uart driver for processing.
  177. */
  178. static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
  179. int count)
  180. {
  181. int chunk_len;
  182. if (!nu->ndev) {
  183. nfc_err(nu->tty->dev,
  184. "receive data from tty but no NCI dev is attached yet, drop buffer\n");
  185. return 0;
  186. }
  187. /* Decode all incoming data in packets
  188. * and enqueue then for processing.
  189. */
  190. while (count > 0) {
  191. /* If this is the first data of a packet, allocate a buffer */
  192. if (!nu->rx_skb) {
  193. nu->rx_packet_len = -1;
  194. nu->rx_skb = nci_skb_alloc(nu->ndev,
  195. NCI_MAX_PACKET_SIZE,
  196. GFP_ATOMIC);
  197. if (!nu->rx_skb)
  198. return -ENOMEM;
  199. }
  200. /* Eat byte after byte till full packet header is received */
  201. if (nu->rx_skb->len < NCI_CTRL_HDR_SIZE) {
  202. skb_put_u8(nu->rx_skb, *data++);
  203. --count;
  204. continue;
  205. }
  206. /* Header was received but packet len was not read */
  207. if (nu->rx_packet_len < 0)
  208. nu->rx_packet_len = NCI_CTRL_HDR_SIZE +
  209. nci_plen(nu->rx_skb->data);
  210. /* Compute how many bytes are missing and how many bytes can
  211. * be consumed.
  212. */
  213. chunk_len = nu->rx_packet_len - nu->rx_skb->len;
  214. if (count < chunk_len)
  215. chunk_len = count;
  216. skb_put_data(nu->rx_skb, data, chunk_len);
  217. data += chunk_len;
  218. count -= chunk_len;
  219. /* Check if packet is fully received */
  220. if (nu->rx_packet_len == nu->rx_skb->len) {
  221. /* Pass RX packet to driver */
  222. if (nu->ops.recv(nu, nu->rx_skb) != 0)
  223. nfc_err(nu->tty->dev, "corrupted RX packet\n");
  224. /* Next packet will be a new one */
  225. nu->rx_skb = NULL;
  226. }
  227. }
  228. return 0;
  229. }
  230. /* nci_uart_tty_receive()
  231. *
  232. * Called by tty low level driver when receive data is
  233. * available.
  234. *
  235. * Arguments: tty pointer to tty instance data
  236. * data pointer to received data
  237. * flags pointer to flags for data
  238. * count count of received data in bytes
  239. *
  240. * Return Value: None
  241. */
  242. static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
  243. const u8 *flags, size_t count)
  244. {
  245. struct nci_uart *nu = tty->disc_data;
  246. if (!nu || tty != nu->tty)
  247. return;
  248. spin_lock(&nu->rx_lock);
  249. nci_uart_default_recv_buf(nu, data, count);
  250. spin_unlock(&nu->rx_lock);
  251. tty_unthrottle(tty);
  252. }
  253. /* nci_uart_tty_ioctl()
  254. *
  255. * Process IOCTL system call for the tty device.
  256. *
  257. * Arguments:
  258. *
  259. * tty pointer to tty instance data
  260. * cmd IOCTL command code
  261. * arg argument for IOCTL call (cmd dependent)
  262. *
  263. * Return Value: Command dependent
  264. */
  265. static int nci_uart_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
  266. unsigned long arg)
  267. {
  268. struct nci_uart *nu = tty->disc_data;
  269. int err = 0;
  270. switch (cmd) {
  271. case NCIUARTSETDRIVER:
  272. if (!nu)
  273. return nci_uart_set_driver(tty, (unsigned int)arg);
  274. else
  275. return -EBUSY;
  276. break;
  277. default:
  278. err = n_tty_ioctl_helper(tty, cmd, arg);
  279. break;
  280. }
  281. return err;
  282. }
  283. /* We don't provide read/write/poll interface for user space. */
  284. static ssize_t nci_uart_tty_read(struct tty_struct *tty, struct file *file,
  285. u8 *buf, size_t nr, void **cookie,
  286. unsigned long offset)
  287. {
  288. return 0;
  289. }
  290. static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file,
  291. const u8 *data, size_t count)
  292. {
  293. return 0;
  294. }
  295. static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
  296. {
  297. /* Queue TX packet */
  298. skb_queue_tail(&nu->tx_q, skb);
  299. /* Try to start TX (if possible) */
  300. nci_uart_tx_wakeup(nu);
  301. return 0;
  302. }
  303. int nci_uart_register(struct nci_uart *nu)
  304. {
  305. if (!nu || !nu->ops.open ||
  306. !nu->ops.recv || !nu->ops.close)
  307. return -EINVAL;
  308. /* Set the send callback */
  309. nu->ops.send = nci_uart_send;
  310. /* Add this driver in the driver list */
  311. if (nci_uart_drivers[nu->driver]) {
  312. pr_err("driver %d is already registered\n", nu->driver);
  313. return -EBUSY;
  314. }
  315. nci_uart_drivers[nu->driver] = nu;
  316. pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_GPL(nci_uart_register);
  320. void nci_uart_unregister(struct nci_uart *nu)
  321. {
  322. pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
  323. nu->driver);
  324. /* Remove this driver from the driver list */
  325. nci_uart_drivers[nu->driver] = NULL;
  326. }
  327. EXPORT_SYMBOL_GPL(nci_uart_unregister);
  328. void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
  329. {
  330. struct ktermios new_termios;
  331. if (!nu->tty)
  332. return;
  333. down_read(&nu->tty->termios_rwsem);
  334. new_termios = nu->tty->termios;
  335. up_read(&nu->tty->termios_rwsem);
  336. tty_termios_encode_baud_rate(&new_termios, baudrate, baudrate);
  337. if (flow_ctrl)
  338. new_termios.c_cflag |= CRTSCTS;
  339. else
  340. new_termios.c_cflag &= ~CRTSCTS;
  341. tty_set_termios(nu->tty, &new_termios);
  342. }
  343. EXPORT_SYMBOL_GPL(nci_uart_set_config);
  344. static struct tty_ldisc_ops nci_uart_ldisc = {
  345. .owner = THIS_MODULE,
  346. .num = N_NCI,
  347. .name = "n_nci",
  348. .open = nci_uart_tty_open,
  349. .close = nci_uart_tty_close,
  350. .read = nci_uart_tty_read,
  351. .write = nci_uart_tty_write,
  352. .receive_buf = nci_uart_tty_receive,
  353. .write_wakeup = nci_uart_tty_wakeup,
  354. .ioctl = nci_uart_tty_ioctl,
  355. .compat_ioctl = nci_uart_tty_ioctl,
  356. };
  357. static int __init nci_uart_init(void)
  358. {
  359. return tty_register_ldisc(&nci_uart_ldisc);
  360. }
  361. static void __exit nci_uart_exit(void)
  362. {
  363. tty_unregister_ldisc(&nci_uart_ldisc);
  364. }
  365. module_init(nci_uart_init);
  366. module_exit(nci_uart_exit);
  367. MODULE_AUTHOR("Marvell International Ltd.");
  368. MODULE_DESCRIPTION("NFC NCI UART driver");
  369. MODULE_LICENSE("GPL");
  370. MODULE_ALIAS_LDISC(N_NCI);