caif_serial.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/init.h>
  7. #include <linux/version.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 int ser_loop;
  37. module_param(ser_loop, bool, S_IRUGO);
  38. MODULE_PARM_DESC(ser_loop, "Run in simulated loopback mode.");
  39. static int ser_use_stx = 1;
  40. module_param(ser_use_stx, bool, S_IRUGO);
  41. MODULE_PARM_DESC(ser_use_stx, "STX enabled or not.");
  42. static int ser_use_fcs = 1;
  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. BUG_ON(skb == NULL);
  168. p = skb_put(skb, count);
  169. memcpy(p, data, count);
  170. skb->protocol = htons(ETH_P_CAIF);
  171. skb_reset_mac_header(skb);
  172. skb->dev = ser->dev;
  173. debugfs_rx(ser, data, count);
  174. /* Push received packet up the stack. */
  175. ret = netif_rx_ni(skb);
  176. if (!ret) {
  177. ser->dev->stats.rx_packets++;
  178. ser->dev->stats.rx_bytes += count;
  179. } else
  180. ++ser->dev->stats.rx_dropped;
  181. update_tty_status(ser);
  182. }
  183. static int handle_tx(struct ser_device *ser)
  184. {
  185. struct tty_struct *tty;
  186. struct sk_buff *skb;
  187. int tty_wr, len, room;
  188. tty = ser->tty;
  189. ser->tx_started = true;
  190. /* Enter critical section */
  191. if (test_and_set_bit(CAIF_SENDING, &ser->state))
  192. return 0;
  193. /* skb_peek is safe because handle_tx is called after skb_queue_tail */
  194. while ((skb = skb_peek(&ser->head)) != NULL) {
  195. /* Make sure you don't write too much */
  196. len = skb->len;
  197. room = tty_write_room(tty);
  198. if (!room)
  199. break;
  200. if (room > ser_write_chunk)
  201. room = ser_write_chunk;
  202. if (len > room)
  203. len = room;
  204. /* Write to tty or loopback */
  205. if (!ser_loop) {
  206. tty_wr = tty->ops->write(tty, skb->data, len);
  207. update_tty_status(ser);
  208. } else {
  209. tty_wr = len;
  210. ldisc_receive(tty, skb->data, NULL, len);
  211. }
  212. ser->dev->stats.tx_packets++;
  213. ser->dev->stats.tx_bytes += tty_wr;
  214. /* Error on TTY ?! */
  215. if (tty_wr < 0)
  216. goto error;
  217. /* Reduce buffer written, and discard if empty */
  218. skb_pull(skb, tty_wr);
  219. if (skb->len == 0) {
  220. struct sk_buff *tmp = skb_dequeue(&ser->head);
  221. BUG_ON(tmp != skb);
  222. if (in_interrupt())
  223. dev_kfree_skb_irq(skb);
  224. else
  225. kfree_skb(skb);
  226. }
  227. }
  228. /* Send flow off if queue is empty */
  229. if (ser->head.qlen <= SEND_QUEUE_LOW &&
  230. test_and_clear_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
  231. ser->common.flowctrl != NULL)
  232. ser->common.flowctrl(ser->dev, ON);
  233. clear_bit(CAIF_SENDING, &ser->state);
  234. return 0;
  235. error:
  236. clear_bit(CAIF_SENDING, &ser->state);
  237. return tty_wr;
  238. }
  239. static int caif_xmit(struct sk_buff *skb, struct net_device *dev)
  240. {
  241. struct ser_device *ser;
  242. BUG_ON(dev == NULL);
  243. ser = netdev_priv(dev);
  244. /* Send flow off once, on high water mark */
  245. if (ser->head.qlen > SEND_QUEUE_HIGH &&
  246. !test_and_set_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
  247. ser->common.flowctrl != NULL)
  248. ser->common.flowctrl(ser->dev, OFF);
  249. skb_queue_tail(&ser->head, skb);
  250. return handle_tx(ser);
  251. }
  252. static void ldisc_tx_wakeup(struct tty_struct *tty)
  253. {
  254. struct ser_device *ser;
  255. ser = tty->disc_data;
  256. BUG_ON(ser == NULL);
  257. BUG_ON(ser->tty != tty);
  258. handle_tx(ser);
  259. }
  260. static int ldisc_open(struct tty_struct *tty)
  261. {
  262. struct ser_device *ser;
  263. struct net_device *dev;
  264. char name[64];
  265. int result;
  266. sprintf(name, "cf%s", tty->name);
  267. dev = alloc_netdev(sizeof(*ser), name, caifdev_setup);
  268. ser = netdev_priv(dev);
  269. ser->tty = tty;
  270. ser->dev = dev;
  271. debugfs_init(ser, tty);
  272. tty->receive_room = N_TTY_BUF_SIZE;
  273. tty->disc_data = ser;
  274. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  275. rtnl_lock();
  276. result = register_netdevice(dev);
  277. if (result) {
  278. rtnl_unlock();
  279. free_netdev(dev);
  280. return -ENODEV;
  281. }
  282. list_add(&ser->node, &ser_list);
  283. rtnl_unlock();
  284. netif_stop_queue(dev);
  285. update_tty_status(ser);
  286. return 0;
  287. }
  288. static void ldisc_close(struct tty_struct *tty)
  289. {
  290. struct ser_device *ser = tty->disc_data;
  291. /* Remove may be called inside or outside of rtnl_lock */
  292. int islocked = rtnl_is_locked();
  293. if (!islocked)
  294. rtnl_lock();
  295. /* device is freed automagically by net-sysfs */
  296. dev_close(ser->dev);
  297. unregister_netdevice(ser->dev);
  298. list_del(&ser->node);
  299. debugfs_deinit(ser);
  300. if (!islocked)
  301. rtnl_unlock();
  302. }
  303. /* The line discipline structure. */
  304. static struct tty_ldisc_ops caif_ldisc = {
  305. .owner = THIS_MODULE,
  306. .magic = TTY_LDISC_MAGIC,
  307. .name = "n_caif",
  308. .open = ldisc_open,
  309. .close = ldisc_close,
  310. .receive_buf = ldisc_receive,
  311. .write_wakeup = ldisc_tx_wakeup
  312. };
  313. static int register_ldisc(void)
  314. {
  315. int result;
  316. result = tty_register_ldisc(N_CAIF, &caif_ldisc);
  317. if (result < 0) {
  318. pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF,
  319. result);
  320. return result;
  321. }
  322. return result;
  323. }
  324. static const struct net_device_ops netdev_ops = {
  325. .ndo_open = caif_net_open,
  326. .ndo_stop = caif_net_close,
  327. .ndo_start_xmit = caif_xmit
  328. };
  329. static void caifdev_setup(struct net_device *dev)
  330. {
  331. struct ser_device *serdev = netdev_priv(dev);
  332. dev->features = 0;
  333. dev->netdev_ops = &netdev_ops;
  334. dev->type = ARPHRD_CAIF;
  335. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  336. dev->mtu = CAIF_MAX_MTU;
  337. dev->hard_header_len = CAIF_NEEDED_HEADROOM;
  338. dev->tx_queue_len = 0;
  339. dev->destructor = free_netdev;
  340. skb_queue_head_init(&serdev->head);
  341. serdev->common.link_select = CAIF_LINK_LOW_LATENCY;
  342. serdev->common.use_frag = true;
  343. serdev->common.use_stx = ser_use_stx;
  344. serdev->common.use_fcs = ser_use_fcs;
  345. serdev->dev = dev;
  346. }
  347. static int caif_net_open(struct net_device *dev)
  348. {
  349. struct ser_device *ser;
  350. ser = netdev_priv(dev);
  351. netif_wake_queue(dev);
  352. return 0;
  353. }
  354. static int caif_net_close(struct net_device *dev)
  355. {
  356. netif_stop_queue(dev);
  357. return 0;
  358. }
  359. static int __init caif_ser_init(void)
  360. {
  361. int ret;
  362. ret = register_ldisc();
  363. debugfsdir = debugfs_create_dir("caif_serial", NULL);
  364. return ret;
  365. }
  366. static void __exit caif_ser_exit(void)
  367. {
  368. struct ser_device *ser = NULL;
  369. struct list_head *node;
  370. struct list_head *_tmp;
  371. list_for_each_safe(node, _tmp, &ser_list) {
  372. ser = list_entry(node, struct ser_device, node);
  373. dev_close(ser->dev);
  374. unregister_netdevice(ser->dev);
  375. list_del(node);
  376. }
  377. tty_unregister_ldisc(N_CAIF);
  378. debugfs_remove_recursive(debugfsdir);
  379. }
  380. module_init(caif_ser_init);
  381. module_exit(caif_ser_exit);