caif_serial.c 10 KB

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