uart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <[email protected]>
  9. * Copyright (C) 2004-2005 Marcel Holtmann <[email protected]>
  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(sizeof(*nu), GFP_KERNEL);
  91. if (!nu)
  92. return -ENOMEM;
  93. memcpy(nu, nci_uart_drivers[driver], sizeof(struct nci_uart));
  94. nu->tty = tty;
  95. tty->disc_data = nu;
  96. skb_queue_head_init(&nu->tx_q);
  97. INIT_WORK(&nu->write_work, nci_uart_write_work);
  98. spin_lock_init(&nu->rx_lock);
  99. ret = nu->ops.open(nu);
  100. if (ret) {
  101. tty->disc_data = NULL;
  102. kfree(nu);
  103. } else if (!try_module_get(nu->owner)) {
  104. nu->ops.close(nu);
  105. tty->disc_data = NULL;
  106. kfree(nu);
  107. return -ENOENT;
  108. }
  109. return ret;
  110. }
  111. /* ------ LDISC part ------ */
  112. /* nci_uart_tty_open
  113. *
  114. * Called when line discipline changed to NCI_UART.
  115. *
  116. * Arguments:
  117. * tty pointer to tty info structure
  118. * Return Value:
  119. * 0 if success, otherwise error code
  120. */
  121. static int nci_uart_tty_open(struct tty_struct *tty)
  122. {
  123. /* Error if the tty has no write op instead of leaving an exploitable
  124. * hole
  125. */
  126. if (!tty->ops->write)
  127. return -EOPNOTSUPP;
  128. tty->disc_data = NULL;
  129. tty->receive_room = 65536;
  130. /* Flush any pending characters in the driver */
  131. tty_driver_flush_buffer(tty);
  132. return 0;
  133. }
  134. /* nci_uart_tty_close()
  135. *
  136. * Called when the line discipline is changed to something
  137. * else, the tty is closed, or the tty detects a hangup.
  138. */
  139. static void nci_uart_tty_close(struct tty_struct *tty)
  140. {
  141. struct nci_uart *nu = (void *)tty->disc_data;
  142. /* Detach from the tty */
  143. tty->disc_data = NULL;
  144. if (!nu)
  145. return;
  146. kfree_skb(nu->tx_skb);
  147. kfree_skb(nu->rx_skb);
  148. skb_queue_purge(&nu->tx_q);
  149. nu->ops.close(nu);
  150. nu->tty = NULL;
  151. module_put(nu->owner);
  152. cancel_work_sync(&nu->write_work);
  153. kfree(nu);
  154. }
  155. /* nci_uart_tty_wakeup()
  156. *
  157. * Callback for transmit wakeup. Called when low level
  158. * device driver can accept more send data.
  159. *
  160. * Arguments: tty pointer to associated tty instance data
  161. * Return Value: None
  162. */
  163. static void nci_uart_tty_wakeup(struct tty_struct *tty)
  164. {
  165. struct nci_uart *nu = (void *)tty->disc_data;
  166. if (!nu)
  167. return;
  168. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  169. if (tty != nu->tty)
  170. return;
  171. nci_uart_tx_wakeup(nu);
  172. }
  173. /* -- Default recv_buf handler --
  174. *
  175. * This handler supposes that NCI frames are sent over UART link without any
  176. * framing. It reads NCI header, retrieve the packet size and once all packet
  177. * bytes are received it passes it to nci_uart driver for processing.
  178. */
  179. static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
  180. int count)
  181. {
  182. int chunk_len;
  183. if (!nu->ndev) {
  184. nfc_err(nu->tty->dev,
  185. "receive data from tty but no NCI dev is attached yet, drop buffer\n");
  186. return 0;
  187. }
  188. /* Decode all incoming data in packets
  189. * and enqueue then for processing.
  190. */
  191. while (count > 0) {
  192. /* If this is the first data of a packet, allocate a buffer */
  193. if (!nu->rx_skb) {
  194. nu->rx_packet_len = -1;
  195. nu->rx_skb = nci_skb_alloc(nu->ndev,
  196. NCI_MAX_PACKET_SIZE,
  197. GFP_ATOMIC);
  198. if (!nu->rx_skb)
  199. return -ENOMEM;
  200. }
  201. /* Eat byte after byte till full packet header is received */
  202. if (nu->rx_skb->len < NCI_CTRL_HDR_SIZE) {
  203. skb_put_u8(nu->rx_skb, *data++);
  204. --count;
  205. continue;
  206. }
  207. /* Header was received but packet len was not read */
  208. if (nu->rx_packet_len < 0)
  209. nu->rx_packet_len = NCI_CTRL_HDR_SIZE +
  210. nci_plen(nu->rx_skb->data);
  211. /* Compute how many bytes are missing and how many bytes can
  212. * be consumed.
  213. */
  214. chunk_len = nu->rx_packet_len - nu->rx_skb->len;
  215. if (count < chunk_len)
  216. chunk_len = count;
  217. skb_put_data(nu->rx_skb, data, chunk_len);
  218. data += chunk_len;
  219. count -= chunk_len;
  220. /* Check if packet is fully received */
  221. if (nu->rx_packet_len == nu->rx_skb->len) {
  222. /* Pass RX packet to driver */
  223. if (nu->ops.recv(nu, nu->rx_skb) != 0)
  224. nfc_err(nu->tty->dev, "corrupted RX packet\n");
  225. /* Next packet will be a new one */
  226. nu->rx_skb = NULL;
  227. }
  228. }
  229. return 0;
  230. }
  231. /* nci_uart_tty_receive()
  232. *
  233. * Called by tty low level driver when receive data is
  234. * available.
  235. *
  236. * Arguments: tty pointer to tty instance data
  237. * data pointer to received data
  238. * flags pointer to flags for data
  239. * count count of received data in bytes
  240. *
  241. * Return Value: None
  242. */
  243. static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
  244. const char *flags, int count)
  245. {
  246. struct nci_uart *nu = (void *)tty->disc_data;
  247. if (!nu || tty != nu->tty)
  248. return;
  249. spin_lock(&nu->rx_lock);
  250. nci_uart_default_recv_buf(nu, data, count);
  251. spin_unlock(&nu->rx_lock);
  252. tty_unthrottle(tty);
  253. }
  254. /* nci_uart_tty_ioctl()
  255. *
  256. * Process IOCTL system call for the tty device.
  257. *
  258. * Arguments:
  259. *
  260. * tty pointer to tty instance data
  261. * cmd IOCTL command code
  262. * arg argument for IOCTL call (cmd dependent)
  263. *
  264. * Return Value: Command dependent
  265. */
  266. static int nci_uart_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
  267. unsigned long arg)
  268. {
  269. struct nci_uart *nu = (void *)tty->disc_data;
  270. int err = 0;
  271. switch (cmd) {
  272. case NCIUARTSETDRIVER:
  273. if (!nu)
  274. return nci_uart_set_driver(tty, (unsigned int)arg);
  275. else
  276. return -EBUSY;
  277. break;
  278. default:
  279. err = n_tty_ioctl_helper(tty, cmd, arg);
  280. break;
  281. }
  282. return err;
  283. }
  284. /* We don't provide read/write/poll interface for user space. */
  285. static ssize_t nci_uart_tty_read(struct tty_struct *tty, struct file *file,
  286. unsigned char *buf, size_t nr,
  287. void **cookie, unsigned long offset)
  288. {
  289. return 0;
  290. }
  291. static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file,
  292. const unsigned char *data, size_t count)
  293. {
  294. return 0;
  295. }
  296. static __poll_t nci_uart_tty_poll(struct tty_struct *tty,
  297. struct file *filp, poll_table *wait)
  298. {
  299. return 0;
  300. }
  301. static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
  302. {
  303. /* Queue TX packet */
  304. skb_queue_tail(&nu->tx_q, skb);
  305. /* Try to start TX (if possible) */
  306. nci_uart_tx_wakeup(nu);
  307. return 0;
  308. }
  309. int nci_uart_register(struct nci_uart *nu)
  310. {
  311. if (!nu || !nu->ops.open ||
  312. !nu->ops.recv || !nu->ops.close)
  313. return -EINVAL;
  314. /* Set the send callback */
  315. nu->ops.send = nci_uart_send;
  316. /* Add this driver in the driver list */
  317. if (nci_uart_drivers[nu->driver]) {
  318. pr_err("driver %d is already registered\n", nu->driver);
  319. return -EBUSY;
  320. }
  321. nci_uart_drivers[nu->driver] = nu;
  322. pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
  323. return 0;
  324. }
  325. EXPORT_SYMBOL_GPL(nci_uart_register);
  326. void nci_uart_unregister(struct nci_uart *nu)
  327. {
  328. pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
  329. nu->driver);
  330. /* Remove this driver from the driver list */
  331. nci_uart_drivers[nu->driver] = NULL;
  332. }
  333. EXPORT_SYMBOL_GPL(nci_uart_unregister);
  334. void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
  335. {
  336. struct ktermios new_termios;
  337. if (!nu->tty)
  338. return;
  339. down_read(&nu->tty->termios_rwsem);
  340. new_termios = nu->tty->termios;
  341. up_read(&nu->tty->termios_rwsem);
  342. tty_termios_encode_baud_rate(&new_termios, baudrate, baudrate);
  343. if (flow_ctrl)
  344. new_termios.c_cflag |= CRTSCTS;
  345. else
  346. new_termios.c_cflag &= ~CRTSCTS;
  347. tty_set_termios(nu->tty, &new_termios);
  348. }
  349. EXPORT_SYMBOL_GPL(nci_uart_set_config);
  350. static struct tty_ldisc_ops nci_uart_ldisc = {
  351. .owner = THIS_MODULE,
  352. .num = N_NCI,
  353. .name = "n_nci",
  354. .open = nci_uart_tty_open,
  355. .close = nci_uart_tty_close,
  356. .read = nci_uart_tty_read,
  357. .write = nci_uart_tty_write,
  358. .poll = nci_uart_tty_poll,
  359. .receive_buf = nci_uart_tty_receive,
  360. .write_wakeup = nci_uart_tty_wakeup,
  361. .ioctl = nci_uart_tty_ioctl,
  362. .compat_ioctl = nci_uart_tty_ioctl,
  363. };
  364. static int __init nci_uart_init(void)
  365. {
  366. return tty_register_ldisc(&nci_uart_ldisc);
  367. }
  368. static void __exit nci_uart_exit(void)
  369. {
  370. tty_unregister_ldisc(&nci_uart_ldisc);
  371. }
  372. module_init(nci_uart_init);
  373. module_exit(nci_uart_exit);
  374. MODULE_AUTHOR("Marvell International Ltd.");
  375. MODULE_DESCRIPTION("NFC NCI UART driver");
  376. MODULE_LICENSE("GPL");
  377. MODULE_ALIAS_LDISC(N_NCI);