uart.c 11 KB

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