stp_uart.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. #include <linux/version.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/types.h>
  6. #include <linux/fcntl.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/ptrace.h>
  9. #include <linux/poll.h>
  10. #include <linux/slab.h>
  11. #include <linux/tty.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/signal.h>
  15. #include <linux/ioctl.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/time.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/kfifo.h>
  22. #include "stp_exp.h"
  23. #define N_MTKSTP (15 + 1) /* refer to linux tty.h use N_HCI. */
  24. #define HCIUARTSETPROTO _IOW('U', 200, int)
  25. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  26. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  27. #define PFX "[UART] "
  28. #define UART_LOG_LOUD 4
  29. #define UART_LOG_DBG 3
  30. #define UART_LOG_INFO 2
  31. #define UART_LOG_WARN 1
  32. #define UART_LOG_ERR 0
  33. #define MAX_PACKET_ALLOWED 2000
  34. static UINT32 gDbgLevel = UART_LOG_INFO;
  35. #define UART_DBG_FUNC(fmt, arg...) \
  36. do { if (gDbgLevel >= UART_LOG_DBG) \
  37. pr_warn(PFX "%s: " fmt, __func__ , ##arg); \
  38. } while (0)
  39. #define UART_INFO_FUNC(fmt, arg...) \
  40. do { if (gDbgLevel >= UART_LOG_INFO) \
  41. pr_warn(PFX "%s: " fmt, __func__ , ##arg); \
  42. } while (0)
  43. #define UART_WARN_FUNC(fmt, arg...) \
  44. do { if (gDbgLevel >= UART_LOG_WARN) \
  45. pr_warn(PFX "%s: " fmt, __func__ , ##arg); \
  46. } while (0)
  47. #define UART_ERR_FUNC(fmt, arg...) \
  48. do { if (gDbgLevel >= UART_LOG_ERR) \
  49. pr_err(PFX "%s: " fmt, __func__ , ##arg); \
  50. } while (0)
  51. #define UART_TRC_FUNC(f) \
  52. do { if (gDbgLevel >= UART_LOG_DBG) \
  53. pr_debug(PFX "<%s> <%d>\n", __func__, __LINE__); \
  54. } while (0)
  55. #include <linux/kfifo.h>
  56. #define LDISC_RX_TASKLET 0
  57. #define LDISC_RX_WORK 1
  58. #if WMT_UART_RX_MODE_WORK
  59. #define LDISC_RX LDISC_RX_WORK
  60. #else
  61. #define LDISC_RX LDISC_RX_TASKLET
  62. #endif
  63. #if (LDISC_RX == LDISC_RX_TASKLET)
  64. #define LDISC_RX_FIFO_SIZE (0x20000) /*8192 bytes */
  65. struct kfifo *g_stp_uart_rx_fifo = NULL;
  66. spinlock_t g_stp_uart_rx_fifo_spinlock;
  67. struct tasklet_struct g_stp_uart_rx_fifo_tasklet;
  68. #define RX_BUFFER_LEN 1024
  69. UINT8 g_rx_data[RX_BUFFER_LEN];
  70. /* static DEFINE_RWLOCK(g_stp_uart_rx_handling_lock); */
  71. #elif (LDISC_RX == LDISC_RX_WORK)
  72. #define LDISC_RX_FIFO_SIZE (0x4000) /* 16K bytes shall be enough...... */
  73. #define LDISC_RX_BUF_SIZE (2048) /* 2K bytes in one shot is enough */
  74. PUINT8 g_stp_uart_rx_buf; /* for stp rx data parsing */
  75. struct kfifo *g_stp_uart_rx_fifo = NULL; /* for uart tty data receiving */
  76. spinlock_t g_stp_uart_rx_fifo_spinlock; /* fifo spinlock */
  77. struct workqueue_struct *g_stp_uart_rx_wq; /* rx work queue (do not use system_wq) */
  78. struct work_struct *g_stp_uart_rx_work; /* rx work */
  79. #endif
  80. struct tty_struct *stp_tty = 0x0;
  81. UINT8 tx_buf[MTKSTP_BUFFER_SIZE] = { 0x0 };
  82. INT32 rd_idx = 0;
  83. INT32 wr_idx = 0;
  84. /* struct semaphore buf_mtx; */
  85. spinlock_t buf_lock;
  86. static INT32 mtk_wcn_uart_tx(const PUINT8 data, const UINT32 size, PUINT32 written_size);
  87. static inline INT32 stp_uart_tx_wakeup(struct tty_struct *tty)
  88. {
  89. INT32 len = 0;
  90. INT32 written = 0;
  91. INT32 written_count = 0;
  92. static INT32 i;
  93. /* UINT32 flags; */
  94. /* get data from ring buffer */
  95. /* down(&buf_mtx); */
  96. UART_DBG_FUNC("++\n");
  97. /* // spin_lock_irqsave(&buf_lock, flags); */
  98. #if 0
  99. if ((i > 1000) && (i % 5) == 0) {
  100. UART_INFO_FUNC("i=(%d), ****** drop data from uart******\n", i);
  101. i++;
  102. return 0;
  103. }
  104. UART_INFO_FUNC("i=(%d)at stp uart **\n", i);
  105. #endif
  106. len = (wr_idx >= rd_idx) ? (wr_idx - rd_idx) : (MTKSTP_BUFFER_SIZE - rd_idx);
  107. if (len > 0 && len < MAX_PACKET_ALLOWED) {
  108. i++;
  109. /*
  110. * ops->write is called by the kernel to write a series of
  111. * characters to the tty device. The characters may come from
  112. * user space or kernel space. This routine will return the
  113. * number of characters actually accepted for writing.
  114. */
  115. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  116. written = tty->ops->write(tty, &tx_buf[rd_idx], len);
  117. if (written != len) {
  118. UART_ERR_FUNC
  119. ("Error(i-%d):[pid(%d)(%s)]tty-ops->write FAIL!len(%d)wr(%d)wr_i(%d)rd_i(%d)\n\r",
  120. i, current->pid, current->comm, len, written, wr_idx, rd_idx);
  121. return -1;
  122. }
  123. written_count = written;
  124. /* pr_debug("len = %d, written = %d\n", len, written); */
  125. rd_idx = ((rd_idx + written) % MTKSTP_BUFFER_SIZE);
  126. /* all data is accepted by UART driver, check again in case roll over */
  127. len = (wr_idx >= rd_idx) ? (wr_idx - rd_idx) : (MTKSTP_BUFFER_SIZE - rd_idx);
  128. if (len > 0 && len < MAX_PACKET_ALLOWED) {
  129. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  130. written = tty->ops->write(tty, &tx_buf[rd_idx], len);
  131. if (written != len) {
  132. UART_ERR_FUNC("Error(i-%d):[pid(%d)(%s)]len(%d)wr(%d)wr_i(%d)rd_i(%d)\n\r",
  133. i, current->pid, current->comm, len, written, wr_idx, rd_idx);
  134. return -1;
  135. }
  136. rd_idx = ((rd_idx + written) % MTKSTP_BUFFER_SIZE);
  137. written_count += written;
  138. } else if (len < 0 || len >= MAX_PACKET_ALLOWED) {
  139. UART_ERR_FUNC("Warnning(i-%d):[pid(%d)(%s)]length verfication(external)\n",
  140. i, current->pid, current->comm);
  141. UART_ERR_FUNC("warnning,len(%d), wr_idx(%d), rd_idx(%d)!\n\r", len, wr_idx, rd_idx);
  142. return -1;
  143. }
  144. } else {
  145. UART_ERR_FUNC("Warnning(i-%d):[pid(%d)(%s)]length verfication(external)\n",
  146. i, current->pid, current->comm);
  147. UART_ERR_FUNC("warnning,len(%d), wr_idx(%d), rd_idx(%d)!\n\r", len, wr_idx, rd_idx);
  148. return -1;
  149. }
  150. /* up(&buf_mtx); */
  151. /* // spin_unlock_irqrestore(&buf_lock, flags); */
  152. UART_DBG_FUNC("--\n");
  153. return written_count;
  154. }
  155. /* ------ LDISC part ------ */
  156. /* stp_uart_tty_open
  157. *
  158. * Called when line discipline changed to HCI_UART.
  159. *
  160. * Arguments:
  161. * tty pointer to tty info structure
  162. * Return Value:
  163. * 0 if success, otherwise error code
  164. */
  165. static int stp_uart_tty_open(struct tty_struct *tty)
  166. {
  167. UART_DBG_FUNC("stp_uart_tty_opentty: %p\n", tty);
  168. tty->receive_room = 65536;
  169. tty->port->low_latency = 1;
  170. /* Flush any pending characters in the driver and line discipline. */
  171. /* FIXME: why is this needed. Note don't use ldisc_ref here as the
  172. open path is before the ldisc is referencable */
  173. /* definition changed!! */
  174. if (tty->ldisc->ops->flush_buffer)
  175. tty->ldisc->ops->flush_buffer(tty);
  176. tty_driver_flush_buffer(tty);
  177. /* init_MUTEX(&buf_mtx); */
  178. /* // spin_lock_init(&buf_lock); */
  179. rd_idx = wr_idx = 0;
  180. stp_tty = tty;
  181. mtk_wcn_stp_register_if_tx(STP_UART_IF_TX, (MTK_WCN_STP_IF_TX) mtk_wcn_uart_tx);
  182. return 0;
  183. }
  184. /* stp_uart_tty_close()
  185. *
  186. * Called when the line discipline is changed to something
  187. * else, the tty is closed, or the tty detects a hangup.
  188. */
  189. static void stp_uart_tty_close(struct tty_struct *tty)
  190. {
  191. UART_DBG_FUNC("stp_uart_tty_close(): tty %p\n", tty);
  192. mtk_wcn_stp_register_if_tx(STP_UART_IF_TX, NULL);
  193. }
  194. /* stp_uart_tty_wakeup()
  195. *
  196. * Callback for transmit wakeup. Called when low level
  197. * device driver can accept more send data.
  198. *
  199. * Arguments: tty pointer to associated tty instance data
  200. * Return Value: None
  201. */
  202. static void stp_uart_tty_wakeup(struct tty_struct *tty)
  203. {
  204. /* pr_debug("%s: start !!\n", __FUNCTION__); */
  205. /* clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); */
  206. /* stp_uart_tx_wakeup(tty); */
  207. }
  208. /* stp_uart_tty_receive()
  209. *
  210. * Called by tty low level driver when receive data is
  211. * available.
  212. *
  213. * Arguments: tty pointer to tty isntance data
  214. * data pointer to received data
  215. * flags pointer to flags for data
  216. * count count of received data in bytes
  217. *
  218. * Return Value: None
  219. */
  220. #if (LDISC_RX == LDISC_RX_TASKLET)
  221. static INT32 stp_uart_fifo_init(VOID)
  222. {
  223. INT32 err = 0;
  224. /*add rx fifo */
  225. g_stp_uart_rx_fifo = kzalloc(sizeof(struct kfifo), GFP_ATOMIC);
  226. if (NULL == g_stp_uart_rx_fifo) {
  227. err = -2;
  228. UART_ERR_FUNC("kzalloc for g_stp_uart_rx_fifo failed (kernel version > 2.6.35)\n");
  229. return err;
  230. }
  231. err = kfifo_alloc(g_stp_uart_rx_fifo, LDISC_RX_FIFO_SIZE, GFP_ATOMIC);
  232. if (0 != err) {
  233. UART_ERR_FUNC("kfifo_alloc failed, errno(%d)(kernel version > 2.6.35)\n", err);
  234. kfree(g_stp_uart_rx_fifo);
  235. g_stp_uart_rx_fifo = NULL;
  236. err = -3;
  237. return err;
  238. }
  239. if (0 == err) {
  240. if (NULL != g_stp_uart_rx_fifo) {
  241. kfifo_reset(g_stp_uart_rx_fifo);
  242. UART_ERR_FUNC("stp_uart_fifo_init() success.\n");
  243. } else {
  244. err = -4;
  245. UART_ERR_FUNC
  246. ("abnormal case, err = 0 but g_stp_uart_rx_fifo = NULL, set err to %d\n",
  247. err);
  248. }
  249. } else
  250. UART_ERR_FUNC("stp_uart_fifo_init() failed.\n");
  251. return err;
  252. }
  253. static INT32 stp_uart_fifo_deinit(VOID)
  254. {
  255. if (NULL != g_stp_uart_rx_fifo) {
  256. kfifo_free(g_stp_uart_rx_fifo);
  257. kfree(g_stp_uart_rx_fifo);
  258. g_stp_uart_rx_fifo = NULL;
  259. }
  260. return 0;
  261. }
  262. static void stp_uart_rx_handling(unsigned long func_data)
  263. {
  264. UINT32 how_much_get = 0;
  265. UINT32 how_much_to_get = 0;
  266. UINT32 flag = 0;
  267. /* read_lock(&g_stp_uart_rx_handling_lock); */
  268. how_much_to_get = kfifo_len(g_stp_uart_rx_fifo);
  269. if (how_much_to_get >= RX_BUFFER_LEN) {
  270. flag = 1;
  271. UART_INFO_FUNC("fifolen(%d)\n", how_much_to_get);
  272. }
  273. do {
  274. how_much_get = kfifo_out(g_stp_uart_rx_fifo, g_rx_data, RX_BUFFER_LEN);
  275. /* UART_INFO_FUNC ("fifoget(%d)\n", how_much_get); */
  276. mtk_wcn_stp_parser_data((UINT8 *) g_rx_data, how_much_get);
  277. how_much_to_get = kfifo_len(g_stp_uart_rx_fifo);
  278. } while (how_much_to_get > 0);
  279. /* read_unlock(&g_stp_uart_rx_handling_lock); */
  280. if (1 == flag)
  281. UART_INFO_FUNC("finish, fifolen(%d)\n", kfifo_len(g_stp_uart_rx_fifo));
  282. }
  283. static void stp_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
  284. {
  285. UINT32 fifo_avail_len = LDISC_RX_FIFO_SIZE - kfifo_len(g_stp_uart_rx_fifo);
  286. UINT32 how_much_put = 0;
  287. #if 0
  288. {
  289. struct timeval now;
  290. do_gettimeofday(&now);
  291. pr_warn("[+STP][ ][R] %4d --> sec = %lu, --> usec --> %lu\n",
  292. count, now.tv_sec, now.tv_usec);
  293. }
  294. #endif
  295. /* write_lock(&g_stp_uart_rx_handling_lock); */
  296. if (count > 2000) {
  297. /*this is abnormal */
  298. UART_ERR_FUNC("abnormal: buffer count = %d\n", count);
  299. }
  300. /*How much empty seat? */
  301. if (fifo_avail_len > 0) {
  302. /* UART_INFO_FUNC ("fifo left(%d), count(%d)\n", fifo_avail_len, count); */
  303. how_much_put = kfifo_in(g_stp_uart_rx_fifo, (PUINT8) data, count);
  304. /*schedule it! */
  305. tasklet_schedule(&g_stp_uart_rx_fifo_tasklet);
  306. } else {
  307. UART_ERR_FUNC("stp_uart_tty_receive rxfifo is full!!\n");
  308. }
  309. #if 0
  310. {
  311. struct timeval now;
  312. do_gettimeofday(&now);
  313. pr_warn("[-STP][ ][R] %4d --> sec = %lu, --> usec --> %lu\n",
  314. count, now.tv_sec, now.tv_usec);
  315. }
  316. #endif
  317. /* write_unlock(&g_stp_uart_rx_handling_lock); */
  318. }
  319. #elif (LDISC_RX == LDISC_RX_WORK)
  320. static INT32 stp_uart_fifo_init(VOID)
  321. {
  322. INT32 err = 0;
  323. g_stp_uart_rx_buf = vzalloc(LDISC_RX_BUF_SIZE);
  324. if (!g_stp_uart_rx_buf) {
  325. UART_ERR_FUNC("kfifo_alloc failed (kernel version >= 2.6.37)\n");
  326. err = -4;
  327. goto fifo_init_end;
  328. }
  329. UART_INFO_FUNC("g_stp_uart_rx_buf alloc ok(0x%p, %d)\n",
  330. g_stp_uart_rx_buf, LDISC_RX_BUF_SIZE);
  331. /*add rx fifo */
  332. /* allocate struct kfifo first */
  333. g_stp_uart_rx_fifo = kzalloc(sizeof(struct kfifo), GFP_KERNEL);
  334. if (NULL == g_stp_uart_rx_fifo) {
  335. err = -2;
  336. UART_ERR_FUNC("kzalloc struct kfifo failed (kernel version > 2.6.33)\n");
  337. goto fifo_init_end;
  338. }
  339. /* allocate kfifo data buffer then */
  340. err = kfifo_alloc(g_stp_uart_rx_fifo, LDISC_RX_FIFO_SIZE, GFP_KERNEL);
  341. if (0 != err) {
  342. UART_ERR_FUNC("kfifo_alloc failed, err(%d)(kernel version > 2.6.33)\n", err);
  343. kfree(g_stp_uart_rx_fifo);
  344. g_stp_uart_rx_fifo = NULL;
  345. err = -3;
  346. goto fifo_init_end;
  347. }
  348. UART_INFO_FUNC("g_stp_uart_rx_fifo alloc ok\n");
  349. fifo_init_end:
  350. if (0 == err) {
  351. /* kfifo init ok */
  352. kfifo_reset(g_stp_uart_rx_fifo);
  353. UART_DBG_FUNC("g_stp_uart_rx_fifo init success\n");
  354. } else {
  355. UART_ERR_FUNC("stp_uart_fifo_init() fail(%d)\n", err);
  356. if (g_stp_uart_rx_buf) {
  357. UART_ERR_FUNC("free g_stp_uart_rx_buf\n");
  358. vfree(g_stp_uart_rx_buf);
  359. g_stp_uart_rx_buf = NULL;
  360. }
  361. }
  362. return err;
  363. }
  364. static INT32 stp_uart_fifo_deinit(VOID)
  365. {
  366. if (g_stp_uart_rx_buf) {
  367. vfree(g_stp_uart_rx_buf);
  368. g_stp_uart_rx_buf = NULL;
  369. }
  370. if (NULL != g_stp_uart_rx_fifo) {
  371. kfifo_free(g_stp_uart_rx_fifo);
  372. kfree(g_stp_uart_rx_fifo);
  373. g_stp_uart_rx_fifo = NULL;
  374. }
  375. return 0;
  376. }
  377. static void stp_uart_rx_worker(struct work_struct *work)
  378. {
  379. UINT32 read;
  380. if (unlikely(!g_stp_uart_rx_fifo)) {
  381. UART_ERR_FUNC("NULL rx fifo!\n");
  382. return;
  383. }
  384. if (unlikely(!g_stp_uart_rx_buf)) {
  385. UART_ERR_FUNC("NULL rx buf!\n");
  386. return;
  387. }
  388. /* run until fifo becomes empty */
  389. while (!kfifo_is_empty(g_stp_uart_rx_fifo)) {
  390. read = kfifo_out(g_stp_uart_rx_fifo, g_stp_uart_rx_buf, LDISC_RX_BUF_SIZE);
  391. UART_DBG_FUNC("kfifo_out(%d)\n", read);
  392. /* pr_debug("rx_work:%d\n\r",read); */
  393. if (likely(read)) {
  394. /* UART_LOUD_FUNC("->%d\n", read); */
  395. mtk_wcn_stp_parser_data((UINT8 *) g_stp_uart_rx_buf, read);
  396. /* UART_LOUD_FUNC("<-\n", read); */
  397. }
  398. }
  399. }
  400. /* stp_uart_tty_receive()
  401. *
  402. * Called by tty low level driver when receive data is
  403. * available.
  404. *
  405. * Arguments: tty pointer to tty isntance data
  406. * data pointer to received data
  407. * flags pointer to flags for data
  408. * count count of received data in bytes
  409. *
  410. * Return Value: None
  411. */
  412. static void stp_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
  413. {
  414. UINT32 written;
  415. /* UART_LOUD_FUNC("URX:%d\n", count); */
  416. if (unlikely(count > 2000))
  417. UART_WARN_FUNC("abnormal: buffer count = %d\n", count);
  418. if (unlikely(!g_stp_uart_rx_fifo || !g_stp_uart_rx_work || !g_stp_uart_rx_wq)) {
  419. UART_ERR_FUNC
  420. ("abnormal g_stp_uart_rx_fifo(0x%p),g_stp_uart_rx_work(0x%p),g_stp_uart_rx_wq(0x%p)\n",
  421. g_stp_uart_rx_fifo, g_stp_uart_rx_work, g_stp_uart_rx_wq);
  422. return;
  423. }
  424. /* need to check available buffer size? skip! */
  425. /* need to lock fifo? skip for single writer single reader! */
  426. written = kfifo_in(g_stp_uart_rx_fifo, (PUINT8) data, count);
  427. /* pr_debug("uart_rx:%d,wr:%d\n\r",count,written); */
  428. queue_work(g_stp_uart_rx_wq, g_stp_uart_rx_work);
  429. if (unlikely(written != count))
  430. UART_ERR_FUNC("c(%d),w(%d) bytes dropped\n", count, written);
  431. }
  432. #else
  433. static void stp_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
  434. {
  435. #if 0
  436. mtk_wcn_stp_debug_gpio_assert(IDX_STP_RX_PROC, DBG_TIE_LOW);
  437. #endif
  438. if (count > 2000) {
  439. /*this is abnormal */
  440. UART_ERR_FUNC("stp_uart_tty_receive buffer count = %d\n", count);
  441. }
  442. #if 0
  443. {
  444. struct timeval now;
  445. do_gettimeofday(&now);
  446. pr_warn("[+STP][ ][R] %4d --> sec = %d, --> usec --> %d\n",
  447. count, now.tv_sec, now.tv_usec);
  448. }
  449. #endif
  450. /*There are multi-context to access here? Need to spinlock? */
  451. /*Only one context: flush_to_ldisc in tty_buffer.c */
  452. mtk_wcn_stp_parser_data((UINT8 *) data, (UINT32) count);
  453. #if 0
  454. mtk_wcn_stp_debug_gpio_assert(IDX_STP_RX_PROC, DBG_TIE_HIGH);
  455. #endif
  456. tty_unthrottle(tty);
  457. #if 0
  458. {
  459. struct timeval now;
  460. do_gettimeofday(&now);
  461. pr_warn("[-STP][ ][R] %4d --> sec = %d, --> usec --> %d\n",
  462. count, now.tv_sec, now.tv_usec);
  463. }
  464. #endif
  465. }
  466. #endif
  467. /* stp_uart_tty_ioctl()
  468. *
  469. * Process IOCTL system call for the tty device.
  470. *
  471. * Arguments:
  472. *
  473. * tty pointer to tty instance data
  474. * file pointer to open file object for device
  475. * cmd IOCTL command code
  476. * arg argument for IOCTL call (cmd dependent)
  477. *
  478. * Return Value: Command dependent
  479. */
  480. static int stp_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
  481. unsigned int cmd, unsigned long arg)
  482. {
  483. INT32 err = 0;
  484. UART_DBG_FUNC("%s =>\n", __func__);
  485. switch (cmd) {
  486. case HCIUARTSETPROTO:
  487. UART_DBG_FUNC("<!!> Set low_latency to TRUE <!!>\n");
  488. tty->port->low_latency = 1;
  489. break;
  490. default:
  491. UART_DBG_FUNC("<!!> n_tty_ioctl_helper <!!>\n");
  492. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  493. break;
  494. };
  495. UART_DBG_FUNC("%s <=\n", __func__);
  496. return err;
  497. }
  498. /*
  499. * We don't provide read/write/poll interface for user space.
  500. */
  501. static ssize_t stp_uart_tty_read(struct tty_struct *tty, struct file *file,
  502. unsigned char __user *buf, size_t nr)
  503. {
  504. return 0;
  505. }
  506. static ssize_t stp_uart_tty_write(struct tty_struct *tty, struct file *file,
  507. const unsigned char *data, size_t count)
  508. {
  509. return 0;
  510. }
  511. static unsigned int stp_uart_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait)
  512. {
  513. return 0;
  514. }
  515. INT32 mtk_wcn_uart_tx(const PUINT8 data, const UINT32 size, PUINT32 written_size)
  516. {
  517. INT32 room;
  518. /* int idx = 0; */
  519. /* unsigned long flags; */
  520. INT32 ret = 0;
  521. UINT32 len;
  522. /* static int tmp=0; */
  523. static INT32 i;
  524. if (stp_tty == NULL)
  525. return -1;
  526. UART_DBG_FUNC("++\n");
  527. (*written_size) = 0;
  528. /* put data into ring buffer */
  529. /* down(&buf_mtx); */
  530. /*
  531. [PatchNeed]
  532. spin_lock_irqsave is redundant
  533. */
  534. /* spin_lock_irqsave(&buf_lock, flags); */
  535. room =
  536. (wr_idx >=
  537. rd_idx) ? (MTKSTP_BUFFER_SIZE - (wr_idx - rd_idx) - 1) : (rd_idx - wr_idx - 1);
  538. UART_DBG_FUNC("r(%d)s(%d)wr_i(%d)rd_i(%d)\n\r", room, size, wr_idx, rd_idx);
  539. /*
  540. [PatchNeed]
  541. Block copy instead of byte copying
  542. */
  543. if (data == NULL) {
  544. UART_ERR_FUNC("pid(%d)(%s): data is NULL\n", current->pid, current->comm);
  545. (*written_size) = 0;
  546. UART_DBG_FUNC("--\n");
  547. return -2;
  548. }
  549. #if 1
  550. if (unlikely(size > room)) {
  551. UART_ERR_FUNC
  552. ("pid(%d)(%s)room is not available, size needed(%d), wr_idx(%d), rd_idx(%d), room left(%d)\n",
  553. current->pid, current->comm, size, wr_idx, rd_idx, room);
  554. UART_DBG_FUNC("--\n");
  555. (*written_size) = 0;
  556. return -3;
  557. }
  558. /* wr_idx : the position next to write
  559. * rd_idx : the position next to read */
  560. len = min(size, MTKSTP_BUFFER_SIZE - (UINT32) wr_idx);
  561. memcpy(&tx_buf[wr_idx], &data[0], len);
  562. memcpy(&tx_buf[0], &data[len], size - len);
  563. wr_idx = (wr_idx + size) % MTKSTP_BUFFER_SIZE;
  564. UART_DBG_FUNC("r(%d)s(%d)wr_i(%d)rd_i(%d)\n\r", room, size, wr_idx, rd_idx);
  565. i++;
  566. if (size == 0) {
  567. (*written_size) = 0;
  568. } else if (size < MAX_PACKET_ALLOWED) {
  569. /* only size ~(0, 2000) is allowed */
  570. ret = stp_uart_tx_wakeup(stp_tty);
  571. if (ret < 0) {
  572. /* reset read and write index of tx_buffer, is there any risk? */
  573. wr_idx = rd_idx = 0;
  574. *written_size = 0;
  575. } else
  576. *written_size = ret;
  577. } else {
  578. /* we filter all packet with size > 2000 */
  579. UART_ERR_FUNC("Warnning(i-%d):[pid(%d)(%s)]len(%d)size(%d)wr_i(%d)rd_i(%d)\n\r",
  580. i, current->pid, current->comm, len, size, wr_idx, rd_idx);
  581. (*written_size) = 0;
  582. }
  583. #endif
  584. #if 0
  585. while ((room > 0) && (size > 0)) {
  586. tx_buf[wr_idx] = data[idx];
  587. wr_idx = ((wr_idx + 1) % MTKSTP_BUFFER_SIZE);
  588. idx++;
  589. room--;
  590. size--;
  591. (*written_size)++;
  592. }
  593. #endif
  594. /* up(&buf_mtx); */
  595. /*
  596. [PatchNeed]
  597. spin_lock_irqsave is redundant
  598. */
  599. /* // spin_lock_irqsave(&buf_lock, flags); */
  600. /*[PatchNeed]To add a tasklet to shedule Uart Tx */
  601. UART_DBG_FUNC("--\n");
  602. return 0;
  603. }
  604. static int mtk_wcn_stp_uart_init(void)
  605. {
  606. static struct tty_ldisc_ops stp_uart_ldisc;
  607. INT32 err;
  608. INT32 fifo_init_done = 0;
  609. UART_INFO_FUNC("mtk_wcn_stp_uart_init(): MTK STP UART driver\n");
  610. #if (LDISC_RX == LDISC_RX_TASKLET)
  611. err = stp_uart_fifo_init();
  612. if (err != 0)
  613. goto init_err;
  614. fifo_init_done = 1;
  615. /*init rx tasklet */
  616. tasklet_init(&g_stp_uart_rx_fifo_tasklet, stp_uart_rx_handling, (unsigned long)0);
  617. #elif (LDISC_RX == LDISC_RX_WORK)
  618. err = stp_uart_fifo_init();
  619. if (err != 0) {
  620. UART_ERR_FUNC("stp_uart_fifo_init(WORK) error(%d)\n", err);
  621. err = -EFAULT;
  622. goto init_err;
  623. }
  624. fifo_init_done = 1;
  625. g_stp_uart_rx_work = vmalloc(sizeof(struct work_struct));
  626. if (!g_stp_uart_rx_work) {
  627. UART_ERR_FUNC("vmalloc work_struct(%d) fail\n", sizeof(struct work_struct));
  628. err = -ENOMEM;
  629. goto init_err;
  630. }
  631. g_stp_uart_rx_wq = create_singlethread_workqueue("mtk_urxd");
  632. if (!g_stp_uart_rx_wq) {
  633. UART_ERR_FUNC("create_singlethread_workqueue fail\n");
  634. err = -ENOMEM;
  635. goto init_err;
  636. }
  637. /* init rx work */
  638. INIT_WORK(g_stp_uart_rx_work, stp_uart_rx_worker);
  639. #endif
  640. /* Register the tty discipline */
  641. memset(&stp_uart_ldisc, 0, sizeof(stp_uart_ldisc));
  642. stp_uart_ldisc.magic = TTY_LDISC_MAGIC;
  643. stp_uart_ldisc.name = "n_mtkstp";
  644. stp_uart_ldisc.open = stp_uart_tty_open;
  645. stp_uart_ldisc.close = stp_uart_tty_close;
  646. stp_uart_ldisc.read = stp_uart_tty_read;
  647. stp_uart_ldisc.write = stp_uart_tty_write;
  648. stp_uart_ldisc.ioctl = stp_uart_tty_ioctl;
  649. stp_uart_ldisc.poll = stp_uart_tty_poll;
  650. stp_uart_ldisc.receive_buf = stp_uart_tty_receive;
  651. stp_uart_ldisc.write_wakeup = stp_uart_tty_wakeup;
  652. stp_uart_ldisc.owner = THIS_MODULE;
  653. err = tty_register_ldisc(N_MTKSTP, &stp_uart_ldisc);
  654. if (err) {
  655. UART_ERR_FUNC("MTK STP line discipline registration failed. (%d)\n", err);
  656. goto init_err;
  657. }
  658. /*
  659. mtk_wcn_stp_register_if_tx( mtk_wcn_uart_tx);
  660. */
  661. return 0;
  662. init_err:
  663. #if (LDISC_RX == LDISC_RX_TASKLET)
  664. /* nothing */
  665. if (fifo_init_done)
  666. stp_uart_fifo_deinit();
  667. #elif (LDISC_RX == LDISC_RX_WORK)
  668. if (g_stp_uart_rx_wq) {
  669. destroy_workqueue(g_stp_uart_rx_wq);
  670. g_stp_uart_rx_wq = NULL;
  671. }
  672. if (g_stp_uart_rx_work)
  673. vfree(g_stp_uart_rx_work);
  674. if (fifo_init_done)
  675. stp_uart_fifo_deinit();
  676. #endif
  677. UART_ERR_FUNC("init fail, return(%d)\n", err);
  678. return err;
  679. }
  680. static void mtk_wcn_stp_uart_exit(void)
  681. {
  682. INT32 err;
  683. mtk_wcn_stp_register_if_tx(STP_UART_IF_TX, NULL); /* unregister if_tx function */
  684. /* Release tty registration of line discipline */
  685. err = tty_unregister_ldisc(N_MTKSTP);
  686. if (err)
  687. UART_ERR_FUNC("Can't unregister MTK STP line discipline (%d)\n", err);
  688. #if (LDISC_RX == LDISC_RX_TASKLET)
  689. tasklet_kill(&g_stp_uart_rx_fifo_tasklet);
  690. stp_uart_fifo_deinit();
  691. #elif (LDISC_RX == LDISC_RX_WORK)
  692. if (g_stp_uart_rx_work)
  693. cancel_work_sync(g_stp_uart_rx_work);
  694. if (g_stp_uart_rx_wq) {
  695. destroy_workqueue(g_stp_uart_rx_wq);
  696. g_stp_uart_rx_wq = NULL;
  697. }
  698. if (g_stp_uart_rx_work) {
  699. vfree(g_stp_uart_rx_work);
  700. g_stp_uart_rx_work = NULL;
  701. }
  702. stp_uart_fifo_deinit();
  703. #endif
  704. }
  705. #ifdef MTK_WCN_REMOVE_KERNEL_MODULE
  706. INT32 mtk_wcn_stp_uart_drv_init(VOID)
  707. {
  708. return mtk_wcn_stp_uart_init();
  709. }
  710. EXPORT_SYMBOL(mtk_wcn_stp_uart_drv_init);
  711. VOID mtk_wcn_stp_uart_drv_exit(VOID)
  712. {
  713. return mtk_wcn_stp_uart_exit();
  714. }
  715. EXPORT_SYMBOL(mtk_wcn_stp_uart_drv_exit);
  716. #else
  717. module_init(mtk_wcn_stp_uart_init);
  718. module_exit(mtk_wcn_stp_uart_exit);
  719. #endif
  720. MODULE_LICENSE("GPL");
  721. MODULE_AUTHOR("MediaTek Inc WCN_SE_CS3");
  722. MODULE_DESCRIPTION("STP-HIF UART Interface");