uart.c 11 KB

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