uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. if (nu->tx_skb)
  158. kfree_skb(nu->tx_skb);
  159. if (nu->rx_skb)
  160. kfree_skb(nu->rx_skb);
  161. skb_queue_purge(&nu->tx_q);
  162. nu->ops.close(nu);
  163. nu->tty = NULL;
  164. module_put(nu->owner);
  165. cancel_work_sync(&nu->write_work);
  166. kfree(nu);
  167. }
  168. /* nci_uart_tty_wakeup()
  169. *
  170. * Callback for transmit wakeup. Called when low level
  171. * device driver can accept more send data.
  172. *
  173. * Arguments: tty pointer to associated tty instance data
  174. * Return Value: None
  175. */
  176. static void nci_uart_tty_wakeup(struct tty_struct *tty)
  177. {
  178. struct nci_uart *nu = (void *)tty->disc_data;
  179. if (!nu)
  180. return;
  181. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  182. if (tty != nu->tty)
  183. return;
  184. nci_uart_tx_wakeup(nu);
  185. }
  186. /* nci_uart_tty_receive()
  187. *
  188. * Called by tty low level driver when receive data is
  189. * available.
  190. *
  191. * Arguments: tty pointer to tty isntance data
  192. * data pointer to received data
  193. * flags pointer to flags for data
  194. * count count of received data in bytes
  195. *
  196. * Return Value: None
  197. */
  198. static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
  199. char *flags, int count)
  200. {
  201. struct nci_uart *nu = (void *)tty->disc_data;
  202. if (!nu || tty != nu->tty)
  203. return;
  204. spin_lock(&nu->rx_lock);
  205. nu->ops.recv_buf(nu, (void *)data, flags, count);
  206. spin_unlock(&nu->rx_lock);
  207. tty_unthrottle(tty);
  208. }
  209. /* nci_uart_tty_ioctl()
  210. *
  211. * Process IOCTL system call for the tty device.
  212. *
  213. * Arguments:
  214. *
  215. * tty pointer to tty instance data
  216. * file pointer to open file object for device
  217. * cmd IOCTL command code
  218. * arg argument for IOCTL call (cmd dependent)
  219. *
  220. * Return Value: Command dependent
  221. */
  222. static int nci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
  223. unsigned int cmd, unsigned long arg)
  224. {
  225. struct nci_uart *nu = (void *)tty->disc_data;
  226. int err = 0;
  227. switch (cmd) {
  228. case NCIUARTSETDRIVER:
  229. if (!nu)
  230. return nci_uart_set_driver(tty, (unsigned int)arg);
  231. else
  232. return -EBUSY;
  233. break;
  234. default:
  235. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  236. break;
  237. }
  238. return err;
  239. }
  240. /* We don't provide read/write/poll interface for user space. */
  241. static ssize_t nci_uart_tty_read(struct tty_struct *tty, struct file *file,
  242. unsigned char __user *buf, size_t nr)
  243. {
  244. return 0;
  245. }
  246. static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file,
  247. const unsigned char *data, size_t count)
  248. {
  249. return 0;
  250. }
  251. static unsigned int nci_uart_tty_poll(struct tty_struct *tty,
  252. struct file *filp, poll_table *wait)
  253. {
  254. return 0;
  255. }
  256. static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
  257. {
  258. /* Queue TX packet */
  259. skb_queue_tail(&nu->tx_q, skb);
  260. /* Try to start TX (if possible) */
  261. nci_uart_tx_wakeup(nu);
  262. return 0;
  263. }
  264. /* -- Default recv_buf handler --
  265. *
  266. * This handler supposes that NCI frames are sent over UART link without any
  267. * framing. It reads NCI header, retrieve the packet size and once all packet
  268. * bytes are received it passes it to nci_uart driver for processing.
  269. */
  270. static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
  271. char *flags, int count)
  272. {
  273. int chunk_len;
  274. if (!nu->ndev) {
  275. nfc_err(nu->tty->dev,
  276. "receive data from tty but no NCI dev is attached yet, drop buffer\n");
  277. return 0;
  278. }
  279. /* Decode all incoming data in packets
  280. * and enqueue then for processing.
  281. */
  282. while (count > 0) {
  283. /* If this is the first data of a packet, allocate a buffer */
  284. if (!nu->rx_skb) {
  285. nu->rx_packet_len = -1;
  286. nu->rx_skb = nci_skb_alloc(nu->ndev,
  287. NCI_MAX_PACKET_SIZE,
  288. GFP_KERNEL);
  289. if (!nu->rx_skb)
  290. return -ENOMEM;
  291. }
  292. /* Eat byte after byte till full packet header is received */
  293. if (nu->rx_skb->len < NCI_CTRL_HDR_SIZE) {
  294. *skb_put(nu->rx_skb, 1) = *data++;
  295. --count;
  296. continue;
  297. }
  298. /* Header was received but packet len was not read */
  299. if (nu->rx_packet_len < 0)
  300. nu->rx_packet_len = NCI_CTRL_HDR_SIZE +
  301. nci_plen(nu->rx_skb->data);
  302. /* Compute how many bytes are missing and how many bytes can
  303. * be consumed.
  304. */
  305. chunk_len = nu->rx_packet_len - nu->rx_skb->len;
  306. if (count < chunk_len)
  307. chunk_len = count;
  308. memcpy(skb_put(nu->rx_skb, chunk_len), data, chunk_len);
  309. data += chunk_len;
  310. count -= chunk_len;
  311. /* Chcek if packet is fully received */
  312. if (nu->rx_packet_len == nu->rx_skb->len) {
  313. /* Pass RX packet to driver */
  314. if (nu->ops.recv(nu, nu->rx_skb) != 0)
  315. nfc_err(nu->tty->dev, "corrupted RX packet\n");
  316. /* Next packet will be a new one */
  317. nu->rx_skb = NULL;
  318. }
  319. }
  320. return 0;
  321. }
  322. /* -- Default recv handler -- */
  323. static int nci_uart_default_recv(struct nci_uart *nu, struct sk_buff *skb)
  324. {
  325. return nci_recv_frame(nu->ndev, skb);
  326. }
  327. int nci_uart_register(struct nci_uart *nu)
  328. {
  329. if (!nu || !nu->ops.open ||
  330. !nu->ops.recv || !nu->ops.close)
  331. return -EINVAL;
  332. /* Set the send callback */
  333. nu->ops.send = nci_uart_send;
  334. /* Install default handlers if not overridden */
  335. if (!nu->ops.recv_buf)
  336. nu->ops.recv_buf = nci_uart_default_recv_buf;
  337. if (!nu->ops.recv)
  338. nu->ops.recv = nci_uart_default_recv;
  339. /* Add this driver in the driver list */
  340. if (nci_uart_drivers[nu->driver]) {
  341. pr_err("driver %d is already registered\n", nu->driver);
  342. return -EBUSY;
  343. }
  344. nci_uart_drivers[nu->driver] = nu;
  345. pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
  346. return 0;
  347. }
  348. EXPORT_SYMBOL_GPL(nci_uart_register);
  349. void nci_uart_unregister(struct nci_uart *nu)
  350. {
  351. pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
  352. nu->driver);
  353. /* Remove this driver from the driver list */
  354. nci_uart_drivers[nu->driver] = NULL;
  355. }
  356. EXPORT_SYMBOL_GPL(nci_uart_unregister);
  357. void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
  358. {
  359. struct ktermios new_termios;
  360. if (!nu->tty)
  361. return;
  362. down_read(&nu->tty->termios_rwsem);
  363. new_termios = nu->tty->termios;
  364. up_read(&nu->tty->termios_rwsem);
  365. tty_termios_encode_baud_rate(&new_termios, baudrate, baudrate);
  366. if (flow_ctrl)
  367. new_termios.c_cflag |= CRTSCTS;
  368. else
  369. new_termios.c_cflag &= ~CRTSCTS;
  370. tty_set_termios(nu->tty, &new_termios);
  371. }
  372. EXPORT_SYMBOL_GPL(nci_uart_set_config);
  373. static struct tty_ldisc_ops nci_uart_ldisc = {
  374. .magic = TTY_LDISC_MAGIC,
  375. .owner = THIS_MODULE,
  376. .name = "n_nci",
  377. .open = nci_uart_tty_open,
  378. .close = nci_uart_tty_close,
  379. .read = nci_uart_tty_read,
  380. .write = nci_uart_tty_write,
  381. .poll = nci_uart_tty_poll,
  382. .receive_buf = nci_uart_tty_receive,
  383. .write_wakeup = nci_uart_tty_wakeup,
  384. .ioctl = nci_uart_tty_ioctl,
  385. };
  386. static int __init nci_uart_init(void)
  387. {
  388. memset(nci_uart_drivers, 0, sizeof(nci_uart_drivers));
  389. return tty_register_ldisc(N_NCI, &nci_uart_ldisc);
  390. }
  391. static void __exit nci_uart_exit(void)
  392. {
  393. tty_unregister_ldisc(N_NCI);
  394. }
  395. module_init(nci_uart_init);
  396. module_exit(nci_uart_exit);
  397. MODULE_AUTHOR("Marvell International Ltd.");
  398. MODULE_DESCRIPTION("NFC NCI UART driver");
  399. MODULE_LICENSE("GPL");
  400. MODULE_ALIAS_LDISC(N_NCI);