caif_serial.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson AB 2010
  4. * Author: Sjur Brendeland
  5. */
  6. #include <linux/hardirq.h>
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/device.h>
  10. #include <linux/types.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/rtnetlink.h>
  14. #include <linux/tty.h>
  15. #include <linux/file.h>
  16. #include <linux/if_arp.h>
  17. #include <net/caif/caif_device.h>
  18. #include <net/caif/cfcnfg.h>
  19. #include <linux/err.h>
  20. #include <linux/debugfs.h>
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("Sjur Brendeland");
  23. MODULE_DESCRIPTION("CAIF serial device TTY line discipline");
  24. MODULE_LICENSE("GPL");
  25. MODULE_ALIAS_LDISC(N_CAIF);
  26. #define SEND_QUEUE_LOW 10
  27. #define SEND_QUEUE_HIGH 100
  28. #define CAIF_SENDING 1 /* Bit 1 = 0x02*/
  29. #define CAIF_FLOW_OFF_SENT 4 /* Bit 4 = 0x10 */
  30. #define MAX_WRITE_CHUNK 4096
  31. #define ON 1
  32. #define OFF 0
  33. #define CAIF_MAX_MTU 4096
  34. static DEFINE_SPINLOCK(ser_lock);
  35. static LIST_HEAD(ser_list);
  36. static LIST_HEAD(ser_release_list);
  37. static bool ser_loop;
  38. module_param(ser_loop, bool, 0444);
  39. MODULE_PARM_DESC(ser_loop, "Run in simulated loopback mode.");
  40. static bool ser_use_stx = true;
  41. module_param(ser_use_stx, bool, 0444);
  42. MODULE_PARM_DESC(ser_use_stx, "STX enabled or not.");
  43. static bool ser_use_fcs = true;
  44. module_param(ser_use_fcs, bool, 0444);
  45. MODULE_PARM_DESC(ser_use_fcs, "FCS enabled or not.");
  46. static int ser_write_chunk = MAX_WRITE_CHUNK;
  47. module_param(ser_write_chunk, int, 0444);
  48. MODULE_PARM_DESC(ser_write_chunk, "Maximum size of data written to UART.");
  49. static struct dentry *debugfsdir;
  50. static int caif_net_open(struct net_device *dev);
  51. static int caif_net_close(struct net_device *dev);
  52. struct ser_device {
  53. struct caif_dev_common common;
  54. struct list_head node;
  55. struct net_device *dev;
  56. struct sk_buff_head head;
  57. struct tty_struct *tty;
  58. bool tx_started;
  59. unsigned long state;
  60. #ifdef CONFIG_DEBUG_FS
  61. struct dentry *debugfs_tty_dir;
  62. struct debugfs_blob_wrapper tx_blob;
  63. struct debugfs_blob_wrapper rx_blob;
  64. u8 rx_data[128];
  65. u8 tx_data[128];
  66. u8 tty_status;
  67. #endif
  68. };
  69. static void caifdev_setup(struct net_device *dev);
  70. static void ldisc_tx_wakeup(struct tty_struct *tty);
  71. #ifdef CONFIG_DEBUG_FS
  72. static inline void update_tty_status(struct ser_device *ser)
  73. {
  74. ser->tty_status =
  75. ser->tty->stopped << 5 |
  76. ser->tty->flow_stopped << 3 |
  77. ser->tty->packet << 2 |
  78. ser->tty->port->low_latency << 1;
  79. }
  80. static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
  81. {
  82. ser->debugfs_tty_dir = debugfs_create_dir(tty->name, debugfsdir);
  83. debugfs_create_blob("last_tx_msg", 0400, ser->debugfs_tty_dir,
  84. &ser->tx_blob);
  85. debugfs_create_blob("last_rx_msg", 0400, ser->debugfs_tty_dir,
  86. &ser->rx_blob);
  87. debugfs_create_x32("ser_state", 0400, ser->debugfs_tty_dir,
  88. (u32 *)&ser->state);
  89. debugfs_create_x8("tty_status", 0400, ser->debugfs_tty_dir,
  90. &ser->tty_status);
  91. ser->tx_blob.data = ser->tx_data;
  92. ser->tx_blob.size = 0;
  93. ser->rx_blob.data = ser->rx_data;
  94. ser->rx_blob.size = 0;
  95. }
  96. static inline void debugfs_deinit(struct ser_device *ser)
  97. {
  98. debugfs_remove_recursive(ser->debugfs_tty_dir);
  99. }
  100. static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
  101. {
  102. if (size > sizeof(ser->rx_data))
  103. size = sizeof(ser->rx_data);
  104. memcpy(ser->rx_data, data, size);
  105. ser->rx_blob.data = ser->rx_data;
  106. ser->rx_blob.size = size;
  107. }
  108. static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
  109. {
  110. if (size > sizeof(ser->tx_data))
  111. size = sizeof(ser->tx_data);
  112. memcpy(ser->tx_data, data, size);
  113. ser->tx_blob.data = ser->tx_data;
  114. ser->tx_blob.size = size;
  115. }
  116. #else
  117. static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
  118. {
  119. }
  120. static inline void debugfs_deinit(struct ser_device *ser)
  121. {
  122. }
  123. static inline void update_tty_status(struct ser_device *ser)
  124. {
  125. }
  126. static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
  127. {
  128. }
  129. static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
  130. {
  131. }
  132. #endif
  133. static void ldisc_receive(struct tty_struct *tty, const u8 *data,
  134. char *flags, int count)
  135. {
  136. struct sk_buff *skb = NULL;
  137. struct ser_device *ser;
  138. int ret;
  139. ser = tty->disc_data;
  140. /*
  141. * NOTE: flags may contain information about break or overrun.
  142. * This is not yet handled.
  143. */
  144. /*
  145. * Workaround for garbage at start of transmission,
  146. * only enable if STX handling is not enabled.
  147. */
  148. if (!ser->common.use_stx && !ser->tx_started) {
  149. dev_info(&ser->dev->dev,
  150. "Bytes received before initial transmission -"
  151. "bytes discarded.\n");
  152. return;
  153. }
  154. BUG_ON(ser->dev == NULL);
  155. /* Get a suitable caif packet and copy in data. */
  156. skb = netdev_alloc_skb(ser->dev, count+1);
  157. if (skb == NULL)
  158. return;
  159. skb_put_data(skb, data, count);
  160. skb->protocol = htons(ETH_P_CAIF);
  161. skb_reset_mac_header(skb);
  162. debugfs_rx(ser, data, count);
  163. /* Push received packet up the stack. */
  164. ret = netif_rx_ni(skb);
  165. if (!ret) {
  166. ser->dev->stats.rx_packets++;
  167. ser->dev->stats.rx_bytes += count;
  168. } else
  169. ++ser->dev->stats.rx_dropped;
  170. update_tty_status(ser);
  171. }
  172. static int handle_tx(struct ser_device *ser)
  173. {
  174. struct tty_struct *tty;
  175. struct sk_buff *skb;
  176. int tty_wr, len, room;
  177. tty = ser->tty;
  178. ser->tx_started = true;
  179. /* Enter critical section */
  180. if (test_and_set_bit(CAIF_SENDING, &ser->state))
  181. return 0;
  182. /* skb_peek is safe because handle_tx is called after skb_queue_tail */
  183. while ((skb = skb_peek(&ser->head)) != NULL) {
  184. /* Make sure you don't write too much */
  185. len = skb->len;
  186. room = tty_write_room(tty);
  187. if (!room)
  188. break;
  189. if (room > ser_write_chunk)
  190. room = ser_write_chunk;
  191. if (len > room)
  192. len = room;
  193. /* Write to tty or loopback */
  194. if (!ser_loop) {
  195. tty_wr = tty->ops->write(tty, skb->data, len);
  196. update_tty_status(ser);
  197. } else {
  198. tty_wr = len;
  199. ldisc_receive(tty, skb->data, NULL, len);
  200. }
  201. ser->dev->stats.tx_packets++;
  202. ser->dev->stats.tx_bytes += tty_wr;
  203. /* Error on TTY ?! */
  204. if (tty_wr < 0)
  205. goto error;
  206. /* Reduce buffer written, and discard if empty */
  207. skb_pull(skb, tty_wr);
  208. if (skb->len == 0) {
  209. struct sk_buff *tmp = skb_dequeue(&ser->head);
  210. WARN_ON(tmp != skb);
  211. dev_consume_skb_any(skb);
  212. }
  213. }
  214. /* Send flow off if queue is empty */
  215. if (ser->head.qlen <= SEND_QUEUE_LOW &&
  216. test_and_clear_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
  217. ser->common.flowctrl != NULL)
  218. ser->common.flowctrl(ser->dev, ON);
  219. clear_bit(CAIF_SENDING, &ser->state);
  220. return 0;
  221. error:
  222. clear_bit(CAIF_SENDING, &ser->state);
  223. return tty_wr;
  224. }
  225. static int caif_xmit(struct sk_buff *skb, struct net_device *dev)
  226. {
  227. struct ser_device *ser;
  228. ser = netdev_priv(dev);
  229. /* Send flow off once, on high water mark */
  230. if (ser->head.qlen > SEND_QUEUE_HIGH &&
  231. !test_and_set_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
  232. ser->common.flowctrl != NULL)
  233. ser->common.flowctrl(ser->dev, OFF);
  234. skb_queue_tail(&ser->head, skb);
  235. return handle_tx(ser);
  236. }
  237. static void ldisc_tx_wakeup(struct tty_struct *tty)
  238. {
  239. struct ser_device *ser;
  240. ser = tty->disc_data;
  241. BUG_ON(ser == NULL);
  242. WARN_ON(ser->tty != tty);
  243. handle_tx(ser);
  244. }
  245. static void ser_release(struct work_struct *work)
  246. {
  247. struct list_head list;
  248. struct ser_device *ser, *tmp;
  249. spin_lock(&ser_lock);
  250. list_replace_init(&ser_release_list, &list);
  251. spin_unlock(&ser_lock);
  252. if (!list_empty(&list)) {
  253. rtnl_lock();
  254. list_for_each_entry_safe(ser, tmp, &list, node) {
  255. dev_close(ser->dev);
  256. unregister_netdevice(ser->dev);
  257. debugfs_deinit(ser);
  258. }
  259. rtnl_unlock();
  260. }
  261. }
  262. static DECLARE_WORK(ser_release_work, ser_release);
  263. static int ldisc_open(struct tty_struct *tty)
  264. {
  265. struct ser_device *ser;
  266. struct net_device *dev;
  267. char name[64];
  268. int result;
  269. /* No write no play */
  270. if (tty->ops->write == NULL)
  271. return -EOPNOTSUPP;
  272. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_TTY_CONFIG))
  273. return -EPERM;
  274. /* release devices to avoid name collision */
  275. ser_release(NULL);
  276. result = snprintf(name, sizeof(name), "cf%s", tty->name);
  277. if (result >= IFNAMSIZ)
  278. return -EINVAL;
  279. dev = alloc_netdev(sizeof(*ser), name, NET_NAME_UNKNOWN,
  280. caifdev_setup);
  281. if (!dev)
  282. return -ENOMEM;
  283. ser = netdev_priv(dev);
  284. ser->tty = tty_kref_get(tty);
  285. ser->dev = dev;
  286. debugfs_init(ser, tty);
  287. tty->receive_room = N_TTY_BUF_SIZE;
  288. tty->disc_data = ser;
  289. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  290. rtnl_lock();
  291. result = register_netdevice(dev);
  292. if (result) {
  293. tty_kref_put(tty);
  294. rtnl_unlock();
  295. free_netdev(dev);
  296. return -ENODEV;
  297. }
  298. spin_lock(&ser_lock);
  299. list_add(&ser->node, &ser_list);
  300. spin_unlock(&ser_lock);
  301. rtnl_unlock();
  302. netif_stop_queue(dev);
  303. update_tty_status(ser);
  304. return 0;
  305. }
  306. static void ldisc_close(struct tty_struct *tty)
  307. {
  308. struct ser_device *ser = tty->disc_data;
  309. tty_kref_put(ser->tty);
  310. spin_lock(&ser_lock);
  311. list_move(&ser->node, &ser_release_list);
  312. spin_unlock(&ser_lock);
  313. schedule_work(&ser_release_work);
  314. }
  315. /* The line discipline structure. */
  316. static struct tty_ldisc_ops caif_ldisc = {
  317. .owner = THIS_MODULE,
  318. .magic = TTY_LDISC_MAGIC,
  319. .name = "n_caif",
  320. .open = ldisc_open,
  321. .close = ldisc_close,
  322. .receive_buf = ldisc_receive,
  323. .write_wakeup = ldisc_tx_wakeup
  324. };
  325. static int register_ldisc(void)
  326. {
  327. int result;
  328. result = tty_register_ldisc(N_CAIF, &caif_ldisc);
  329. if (result < 0) {
  330. pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF,
  331. result);
  332. return result;
  333. }
  334. return result;
  335. }
  336. static const struct net_device_ops netdev_ops = {
  337. .ndo_open = caif_net_open,
  338. .ndo_stop = caif_net_close,
  339. .ndo_start_xmit = caif_xmit
  340. };
  341. static void caifdev_setup(struct net_device *dev)
  342. {
  343. struct ser_device *serdev = netdev_priv(dev);
  344. dev->features = 0;
  345. dev->netdev_ops = &netdev_ops;
  346. dev->type = ARPHRD_CAIF;
  347. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  348. dev->mtu = CAIF_MAX_MTU;
  349. dev->priv_flags |= IFF_NO_QUEUE;
  350. dev->needs_free_netdev = true;
  351. skb_queue_head_init(&serdev->head);
  352. serdev->common.link_select = CAIF_LINK_LOW_LATENCY;
  353. serdev->common.use_frag = true;
  354. serdev->common.use_stx = ser_use_stx;
  355. serdev->common.use_fcs = ser_use_fcs;
  356. serdev->dev = dev;
  357. }
  358. static int caif_net_open(struct net_device *dev)
  359. {
  360. netif_wake_queue(dev);
  361. return 0;
  362. }
  363. static int caif_net_close(struct net_device *dev)
  364. {
  365. netif_stop_queue(dev);
  366. return 0;
  367. }
  368. static int __init caif_ser_init(void)
  369. {
  370. int ret;
  371. ret = register_ldisc();
  372. debugfsdir = debugfs_create_dir("caif_serial", NULL);
  373. return ret;
  374. }
  375. static void __exit caif_ser_exit(void)
  376. {
  377. spin_lock(&ser_lock);
  378. list_splice(&ser_list, &ser_release_list);
  379. spin_unlock(&ser_lock);
  380. ser_release(NULL);
  381. cancel_work_sync(&ser_release_work);
  382. tty_unregister_ldisc(N_CAIF);
  383. debugfs_remove_recursive(debugfsdir);
  384. }
  385. module_init(caif_ser_init);
  386. module_exit(caif_ser_exit);