caif_serial.c 10 KB

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