caif_serial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Sjur Brendeland
  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");
  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, S_IRUGO);
  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, S_IRUGO);
  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, S_IRUGO);
  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, S_IRUGO);
  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 =
  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. debugfs_rx(ser, data, count);
  172. /* Push received packet up the stack. */
  173. ret = netif_rx_ni(skb);
  174. if (!ret) {
  175. ser->dev->stats.rx_packets++;
  176. ser->dev->stats.rx_bytes += count;
  177. } else
  178. ++ser->dev->stats.rx_dropped;
  179. update_tty_status(ser);
  180. }
  181. static int handle_tx(struct ser_device *ser)
  182. {
  183. struct tty_struct *tty;
  184. struct sk_buff *skb;
  185. int tty_wr, len, room;
  186. tty = ser->tty;
  187. ser->tx_started = true;
  188. /* Enter critical section */
  189. if (test_and_set_bit(CAIF_SENDING, &ser->state))
  190. return 0;
  191. /* skb_peek is safe because handle_tx is called after skb_queue_tail */
  192. while ((skb = skb_peek(&ser->head)) != NULL) {
  193. /* Make sure you don't write too much */
  194. len = skb->len;
  195. room = tty_write_room(tty);
  196. if (!room)
  197. break;
  198. if (room > ser_write_chunk)
  199. room = ser_write_chunk;
  200. if (len > room)
  201. len = room;
  202. /* Write to tty or loopback */
  203. if (!ser_loop) {
  204. tty_wr = tty->ops->write(tty, skb->data, len);
  205. update_tty_status(ser);
  206. } else {
  207. tty_wr = len;
  208. ldisc_receive(tty, skb->data, NULL, len);
  209. }
  210. ser->dev->stats.tx_packets++;
  211. ser->dev->stats.tx_bytes += tty_wr;
  212. /* Error on TTY ?! */
  213. if (tty_wr < 0)
  214. goto error;
  215. /* Reduce buffer written, and discard if empty */
  216. skb_pull(skb, tty_wr);
  217. if (skb->len == 0) {
  218. struct sk_buff *tmp = skb_dequeue(&ser->head);
  219. WARN_ON(tmp != skb);
  220. if (in_interrupt())
  221. dev_kfree_skb_irq(skb);
  222. else
  223. kfree_skb(skb);
  224. }
  225. }
  226. /* Send flow off if queue is empty */
  227. if (ser->head.qlen <= SEND_QUEUE_LOW &&
  228. test_and_clear_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
  229. ser->common.flowctrl != NULL)
  230. ser->common.flowctrl(ser->dev, ON);
  231. clear_bit(CAIF_SENDING, &ser->state);
  232. return 0;
  233. error:
  234. clear_bit(CAIF_SENDING, &ser->state);
  235. return tty_wr;
  236. }
  237. static int caif_xmit(struct sk_buff *skb, struct net_device *dev)
  238. {
  239. struct ser_device *ser;
  240. BUG_ON(dev == NULL);
  241. ser = netdev_priv(dev);
  242. /* Send flow off once, on high water mark */
  243. if (ser->head.qlen > SEND_QUEUE_HIGH &&
  244. !test_and_set_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
  245. ser->common.flowctrl != NULL)
  246. ser->common.flowctrl(ser->dev, OFF);
  247. skb_queue_tail(&ser->head, skb);
  248. return handle_tx(ser);
  249. }
  250. static void ldisc_tx_wakeup(struct tty_struct *tty)
  251. {
  252. struct ser_device *ser;
  253. ser = tty->disc_data;
  254. BUG_ON(ser == NULL);
  255. WARN_ON(ser->tty != tty);
  256. handle_tx(ser);
  257. }
  258. static void ser_release(struct work_struct *work)
  259. {
  260. struct list_head list;
  261. struct ser_device *ser, *tmp;
  262. spin_lock(&ser_lock);
  263. list_replace_init(&ser_release_list, &list);
  264. spin_unlock(&ser_lock);
  265. if (!list_empty(&list)) {
  266. rtnl_lock();
  267. list_for_each_entry_safe(ser, tmp, &list, node) {
  268. dev_close(ser->dev);
  269. unregister_netdevice(ser->dev);
  270. debugfs_deinit(ser);
  271. }
  272. rtnl_unlock();
  273. }
  274. }
  275. static DECLARE_WORK(ser_release_work, ser_release);
  276. static int ldisc_open(struct tty_struct *tty)
  277. {
  278. struct ser_device *ser;
  279. struct net_device *dev;
  280. char name[64];
  281. int result;
  282. /* No write no play */
  283. if (tty->ops->write == NULL)
  284. return -EOPNOTSUPP;
  285. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_TTY_CONFIG))
  286. return -EPERM;
  287. /* release devices to avoid name collision */
  288. ser_release(NULL);
  289. result = snprintf(name, sizeof(name), "cf%s", tty->name);
  290. if (result >= IFNAMSIZ)
  291. return -EINVAL;
  292. dev = alloc_netdev(sizeof(*ser), name, NET_NAME_UNKNOWN,
  293. caifdev_setup);
  294. if (!dev)
  295. return -ENOMEM;
  296. ser = netdev_priv(dev);
  297. ser->tty = tty_kref_get(tty);
  298. ser->dev = dev;
  299. debugfs_init(ser, tty);
  300. tty->receive_room = N_TTY_BUF_SIZE;
  301. tty->disc_data = ser;
  302. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  303. rtnl_lock();
  304. result = register_netdevice(dev);
  305. if (result) {
  306. rtnl_unlock();
  307. free_netdev(dev);
  308. return -ENODEV;
  309. }
  310. spin_lock(&ser_lock);
  311. list_add(&ser->node, &ser_list);
  312. spin_unlock(&ser_lock);
  313. rtnl_unlock();
  314. netif_stop_queue(dev);
  315. update_tty_status(ser);
  316. return 0;
  317. }
  318. static void ldisc_close(struct tty_struct *tty)
  319. {
  320. struct ser_device *ser = tty->disc_data;
  321. tty_kref_put(ser->tty);
  322. spin_lock(&ser_lock);
  323. list_move(&ser->node, &ser_release_list);
  324. spin_unlock(&ser_lock);
  325. schedule_work(&ser_release_work);
  326. }
  327. /* The line discipline structure. */
  328. static struct tty_ldisc_ops caif_ldisc = {
  329. .owner = THIS_MODULE,
  330. .magic = TTY_LDISC_MAGIC,
  331. .name = "n_caif",
  332. .open = ldisc_open,
  333. .close = ldisc_close,
  334. .receive_buf = ldisc_receive,
  335. .write_wakeup = ldisc_tx_wakeup
  336. };
  337. static int register_ldisc(void)
  338. {
  339. int result;
  340. result = tty_register_ldisc(N_CAIF, &caif_ldisc);
  341. if (result < 0) {
  342. pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF,
  343. result);
  344. return result;
  345. }
  346. return result;
  347. }
  348. static const struct net_device_ops netdev_ops = {
  349. .ndo_open = caif_net_open,
  350. .ndo_stop = caif_net_close,
  351. .ndo_start_xmit = caif_xmit
  352. };
  353. static void caifdev_setup(struct net_device *dev)
  354. {
  355. struct ser_device *serdev = netdev_priv(dev);
  356. dev->features = 0;
  357. dev->netdev_ops = &netdev_ops;
  358. dev->type = ARPHRD_CAIF;
  359. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  360. dev->mtu = CAIF_MAX_MTU;
  361. dev->priv_flags |= IFF_NO_QUEUE;
  362. dev->destructor = free_netdev;
  363. skb_queue_head_init(&serdev->head);
  364. serdev->common.link_select = CAIF_LINK_LOW_LATENCY;
  365. serdev->common.use_frag = true;
  366. serdev->common.use_stx = ser_use_stx;
  367. serdev->common.use_fcs = ser_use_fcs;
  368. serdev->dev = dev;
  369. }
  370. static int caif_net_open(struct net_device *dev)
  371. {
  372. netif_wake_queue(dev);
  373. return 0;
  374. }
  375. static int caif_net_close(struct net_device *dev)
  376. {
  377. netif_stop_queue(dev);
  378. return 0;
  379. }
  380. static int __init caif_ser_init(void)
  381. {
  382. int ret;
  383. ret = register_ldisc();
  384. debugfsdir = debugfs_create_dir("caif_serial", NULL);
  385. return ret;
  386. }
  387. static void __exit caif_ser_exit(void)
  388. {
  389. spin_lock(&ser_lock);
  390. list_splice(&ser_list, &ser_release_list);
  391. spin_unlock(&ser_lock);
  392. ser_release(NULL);
  393. cancel_work_sync(&ser_release_work);
  394. tty_unregister_ldisc(N_CAIF);
  395. debugfs_remove_recursive(debugfsdir);
  396. }
  397. module_init(caif_ser_init);
  398. module_exit(caif_ser_exit);