sdio_uart.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /*
  2. * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
  3. *
  4. * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
  5. * by Russell King.
  6. *
  7. * Author: Nicolas Pitre
  8. * Created: June 15, 2007
  9. * Copyright: MontaVista Software, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. */
  16. /*
  17. * Note: Although this driver assumes a 16550A-like UART implementation,
  18. * it is not possible to leverage the common 8250/16550 driver, nor the
  19. * core UART infrastructure, as they assumes direct access to the hardware
  20. * registers, often under a spinlock. This is not possible in the SDIO
  21. * context as SDIO access functions must be able to sleep.
  22. *
  23. * Because we need to lock the SDIO host to ensure an exclusive access to
  24. * the card, we simply rely on that lock to also prevent and serialize
  25. * concurrent access to the same port.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/kernel.h>
  30. #include <linux/sched.h>
  31. #include <linux/mutex.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/circ_buf.h>
  35. #include <linux/tty.h>
  36. #include <linux/tty_flip.h>
  37. #include <linux/kfifo.h>
  38. #include <linux/slab.h>
  39. #include <linux/mmc/core.h>
  40. #include <linux/mmc/card.h>
  41. #include <linux/mmc/sdio_func.h>
  42. #include <linux/mmc/sdio_ids.h>
  43. #define UART_NR 8 /* Number of UARTs this driver can handle */
  44. #define FIFO_SIZE PAGE_SIZE
  45. #define WAKEUP_CHARS 256
  46. struct uart_icount {
  47. __u32 cts;
  48. __u32 dsr;
  49. __u32 rng;
  50. __u32 dcd;
  51. __u32 rx;
  52. __u32 tx;
  53. __u32 frame;
  54. __u32 overrun;
  55. __u32 parity;
  56. __u32 brk;
  57. };
  58. struct sdio_uart_port {
  59. struct tty_port port;
  60. unsigned int index;
  61. struct sdio_func *func;
  62. struct mutex func_lock;
  63. struct task_struct *in_sdio_uart_irq;
  64. unsigned int regs_offset;
  65. struct kfifo xmit_fifo;
  66. spinlock_t write_lock;
  67. struct uart_icount icount;
  68. unsigned int uartclk;
  69. unsigned int mctrl;
  70. unsigned int rx_mctrl;
  71. unsigned int read_status_mask;
  72. unsigned int ignore_status_mask;
  73. unsigned char x_char;
  74. unsigned char ier;
  75. unsigned char lcr;
  76. };
  77. static struct sdio_uart_port *sdio_uart_table[UART_NR];
  78. static DEFINE_SPINLOCK(sdio_uart_table_lock);
  79. static int sdio_uart_add_port(struct sdio_uart_port *port)
  80. {
  81. int index, ret = -EBUSY;
  82. mutex_init(&port->func_lock);
  83. spin_lock_init(&port->write_lock);
  84. if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
  85. return -ENOMEM;
  86. spin_lock(&sdio_uart_table_lock);
  87. for (index = 0; index < UART_NR; index++) {
  88. if (!sdio_uart_table[index]) {
  89. port->index = index;
  90. sdio_uart_table[index] = port;
  91. ret = 0;
  92. break;
  93. }
  94. }
  95. spin_unlock(&sdio_uart_table_lock);
  96. return ret;
  97. }
  98. static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
  99. {
  100. struct sdio_uart_port *port;
  101. if (index >= UART_NR)
  102. return NULL;
  103. spin_lock(&sdio_uart_table_lock);
  104. port = sdio_uart_table[index];
  105. if (port)
  106. tty_port_get(&port->port);
  107. spin_unlock(&sdio_uart_table_lock);
  108. return port;
  109. }
  110. static void sdio_uart_port_put(struct sdio_uart_port *port)
  111. {
  112. tty_port_put(&port->port);
  113. }
  114. static void sdio_uart_port_remove(struct sdio_uart_port *port)
  115. {
  116. struct sdio_func *func;
  117. struct tty_struct *tty;
  118. BUG_ON(sdio_uart_table[port->index] != port);
  119. spin_lock(&sdio_uart_table_lock);
  120. sdio_uart_table[port->index] = NULL;
  121. spin_unlock(&sdio_uart_table_lock);
  122. /*
  123. * We're killing a port that potentially still is in use by
  124. * the tty layer. Be careful to prevent any further access
  125. * to the SDIO function and arrange for the tty layer to
  126. * give up on that port ASAP.
  127. * Beware: the lock ordering is critical.
  128. */
  129. mutex_lock(&port->port.mutex);
  130. mutex_lock(&port->func_lock);
  131. func = port->func;
  132. sdio_claim_host(func);
  133. port->func = NULL;
  134. mutex_unlock(&port->func_lock);
  135. tty = tty_port_tty_get(&port->port);
  136. /* tty_hangup is async so is this safe as is ?? */
  137. if (tty) {
  138. tty_hangup(tty);
  139. tty_kref_put(tty);
  140. }
  141. mutex_unlock(&port->port.mutex);
  142. sdio_release_irq(func);
  143. sdio_disable_func(func);
  144. sdio_release_host(func);
  145. sdio_uart_port_put(port);
  146. }
  147. static int sdio_uart_claim_func(struct sdio_uart_port *port)
  148. {
  149. mutex_lock(&port->func_lock);
  150. if (unlikely(!port->func)) {
  151. mutex_unlock(&port->func_lock);
  152. return -ENODEV;
  153. }
  154. if (likely(port->in_sdio_uart_irq != current))
  155. sdio_claim_host(port->func);
  156. mutex_unlock(&port->func_lock);
  157. return 0;
  158. }
  159. static inline void sdio_uart_release_func(struct sdio_uart_port *port)
  160. {
  161. if (likely(port->in_sdio_uart_irq != current))
  162. sdio_release_host(port->func);
  163. }
  164. static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
  165. {
  166. unsigned char c;
  167. c = sdio_readb(port->func, port->regs_offset + offset, NULL);
  168. return c;
  169. }
  170. static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
  171. {
  172. sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
  173. }
  174. static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
  175. {
  176. unsigned char status;
  177. unsigned int ret;
  178. /* FIXME: What stops this losing the delta bits and breaking
  179. sdio_uart_check_modem_status ? */
  180. status = sdio_in(port, UART_MSR);
  181. ret = 0;
  182. if (status & UART_MSR_DCD)
  183. ret |= TIOCM_CAR;
  184. if (status & UART_MSR_RI)
  185. ret |= TIOCM_RNG;
  186. if (status & UART_MSR_DSR)
  187. ret |= TIOCM_DSR;
  188. if (status & UART_MSR_CTS)
  189. ret |= TIOCM_CTS;
  190. return ret;
  191. }
  192. static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
  193. unsigned int mctrl)
  194. {
  195. unsigned char mcr = 0;
  196. if (mctrl & TIOCM_RTS)
  197. mcr |= UART_MCR_RTS;
  198. if (mctrl & TIOCM_DTR)
  199. mcr |= UART_MCR_DTR;
  200. if (mctrl & TIOCM_OUT1)
  201. mcr |= UART_MCR_OUT1;
  202. if (mctrl & TIOCM_OUT2)
  203. mcr |= UART_MCR_OUT2;
  204. if (mctrl & TIOCM_LOOP)
  205. mcr |= UART_MCR_LOOP;
  206. sdio_out(port, UART_MCR, mcr);
  207. }
  208. static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
  209. unsigned int set, unsigned int clear)
  210. {
  211. unsigned int old;
  212. old = port->mctrl;
  213. port->mctrl = (old & ~clear) | set;
  214. if (old != port->mctrl)
  215. sdio_uart_write_mctrl(port, port->mctrl);
  216. }
  217. #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
  218. #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
  219. static void sdio_uart_change_speed(struct sdio_uart_port *port,
  220. struct ktermios *termios,
  221. struct ktermios *old)
  222. {
  223. unsigned char cval, fcr = 0;
  224. unsigned int baud, quot;
  225. switch (termios->c_cflag & CSIZE) {
  226. case CS5:
  227. cval = UART_LCR_WLEN5;
  228. break;
  229. case CS6:
  230. cval = UART_LCR_WLEN6;
  231. break;
  232. case CS7:
  233. cval = UART_LCR_WLEN7;
  234. break;
  235. default:
  236. case CS8:
  237. cval = UART_LCR_WLEN8;
  238. break;
  239. }
  240. if (termios->c_cflag & CSTOPB)
  241. cval |= UART_LCR_STOP;
  242. if (termios->c_cflag & PARENB)
  243. cval |= UART_LCR_PARITY;
  244. if (!(termios->c_cflag & PARODD))
  245. cval |= UART_LCR_EPAR;
  246. for (;;) {
  247. baud = tty_termios_baud_rate(termios);
  248. if (baud == 0)
  249. baud = 9600; /* Special case: B0 rate. */
  250. if (baud <= port->uartclk)
  251. break;
  252. /*
  253. * Oops, the quotient was zero. Try again with the old
  254. * baud rate if possible, otherwise default to 9600.
  255. */
  256. termios->c_cflag &= ~CBAUD;
  257. if (old) {
  258. termios->c_cflag |= old->c_cflag & CBAUD;
  259. old = NULL;
  260. } else
  261. termios->c_cflag |= B9600;
  262. }
  263. quot = (2 * port->uartclk + baud) / (2 * baud);
  264. if (baud < 2400)
  265. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
  266. else
  267. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
  268. port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  269. if (termios->c_iflag & INPCK)
  270. port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  271. if (termios->c_iflag & (BRKINT | PARMRK))
  272. port->read_status_mask |= UART_LSR_BI;
  273. /*
  274. * Characters to ignore
  275. */
  276. port->ignore_status_mask = 0;
  277. if (termios->c_iflag & IGNPAR)
  278. port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  279. if (termios->c_iflag & IGNBRK) {
  280. port->ignore_status_mask |= UART_LSR_BI;
  281. /*
  282. * If we're ignoring parity and break indicators,
  283. * ignore overruns too (for real raw support).
  284. */
  285. if (termios->c_iflag & IGNPAR)
  286. port->ignore_status_mask |= UART_LSR_OE;
  287. }
  288. /*
  289. * ignore all characters if CREAD is not set
  290. */
  291. if ((termios->c_cflag & CREAD) == 0)
  292. port->ignore_status_mask |= UART_LSR_DR;
  293. /*
  294. * CTS flow control flag and modem status interrupts
  295. */
  296. port->ier &= ~UART_IER_MSI;
  297. if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
  298. port->ier |= UART_IER_MSI;
  299. port->lcr = cval;
  300. sdio_out(port, UART_IER, port->ier);
  301. sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
  302. sdio_out(port, UART_DLL, quot & 0xff);
  303. sdio_out(port, UART_DLM, quot >> 8);
  304. sdio_out(port, UART_LCR, cval);
  305. sdio_out(port, UART_FCR, fcr);
  306. sdio_uart_write_mctrl(port, port->mctrl);
  307. }
  308. static void sdio_uart_start_tx(struct sdio_uart_port *port)
  309. {
  310. if (!(port->ier & UART_IER_THRI)) {
  311. port->ier |= UART_IER_THRI;
  312. sdio_out(port, UART_IER, port->ier);
  313. }
  314. }
  315. static void sdio_uart_stop_tx(struct sdio_uart_port *port)
  316. {
  317. if (port->ier & UART_IER_THRI) {
  318. port->ier &= ~UART_IER_THRI;
  319. sdio_out(port, UART_IER, port->ier);
  320. }
  321. }
  322. static void sdio_uart_stop_rx(struct sdio_uart_port *port)
  323. {
  324. port->ier &= ~UART_IER_RLSI;
  325. port->read_status_mask &= ~UART_LSR_DR;
  326. sdio_out(port, UART_IER, port->ier);
  327. }
  328. static void sdio_uart_receive_chars(struct sdio_uart_port *port,
  329. unsigned int *status)
  330. {
  331. unsigned int ch, flag;
  332. int max_count = 256;
  333. do {
  334. ch = sdio_in(port, UART_RX);
  335. flag = TTY_NORMAL;
  336. port->icount.rx++;
  337. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  338. UART_LSR_FE | UART_LSR_OE))) {
  339. /*
  340. * For statistics only
  341. */
  342. if (*status & UART_LSR_BI) {
  343. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  344. port->icount.brk++;
  345. } else if (*status & UART_LSR_PE)
  346. port->icount.parity++;
  347. else if (*status & UART_LSR_FE)
  348. port->icount.frame++;
  349. if (*status & UART_LSR_OE)
  350. port->icount.overrun++;
  351. /*
  352. * Mask off conditions which should be ignored.
  353. */
  354. *status &= port->read_status_mask;
  355. if (*status & UART_LSR_BI)
  356. flag = TTY_BREAK;
  357. else if (*status & UART_LSR_PE)
  358. flag = TTY_PARITY;
  359. else if (*status & UART_LSR_FE)
  360. flag = TTY_FRAME;
  361. }
  362. if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
  363. tty_insert_flip_char(&port->port, ch, flag);
  364. /*
  365. * Overrun is special. Since it's reported immediately,
  366. * it doesn't affect the current character.
  367. */
  368. if (*status & ~port->ignore_status_mask & UART_LSR_OE)
  369. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  370. *status = sdio_in(port, UART_LSR);
  371. } while ((*status & UART_LSR_DR) && (max_count-- > 0));
  372. tty_flip_buffer_push(&port->port);
  373. }
  374. static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
  375. {
  376. struct kfifo *xmit = &port->xmit_fifo;
  377. int count;
  378. struct tty_struct *tty;
  379. u8 iobuf[16];
  380. int len;
  381. if (port->x_char) {
  382. sdio_out(port, UART_TX, port->x_char);
  383. port->icount.tx++;
  384. port->x_char = 0;
  385. return;
  386. }
  387. tty = tty_port_tty_get(&port->port);
  388. if (tty == NULL || !kfifo_len(xmit) ||
  389. tty->stopped || tty->hw_stopped) {
  390. sdio_uart_stop_tx(port);
  391. tty_kref_put(tty);
  392. return;
  393. }
  394. len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock);
  395. for (count = 0; count < len; count++) {
  396. sdio_out(port, UART_TX, iobuf[count]);
  397. port->icount.tx++;
  398. }
  399. len = kfifo_len(xmit);
  400. if (len < WAKEUP_CHARS) {
  401. tty_wakeup(tty);
  402. if (len == 0)
  403. sdio_uart_stop_tx(port);
  404. }
  405. tty_kref_put(tty);
  406. }
  407. static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
  408. {
  409. int status;
  410. struct tty_struct *tty;
  411. status = sdio_in(port, UART_MSR);
  412. if ((status & UART_MSR_ANY_DELTA) == 0)
  413. return;
  414. if (status & UART_MSR_TERI)
  415. port->icount.rng++;
  416. if (status & UART_MSR_DDSR)
  417. port->icount.dsr++;
  418. if (status & UART_MSR_DDCD) {
  419. port->icount.dcd++;
  420. /* DCD raise - wake for open */
  421. if (status & UART_MSR_DCD)
  422. wake_up_interruptible(&port->port.open_wait);
  423. else {
  424. /* DCD drop - hang up if tty attached */
  425. tty = tty_port_tty_get(&port->port);
  426. if (tty) {
  427. tty_hangup(tty);
  428. tty_kref_put(tty);
  429. }
  430. }
  431. }
  432. if (status & UART_MSR_DCTS) {
  433. port->icount.cts++;
  434. tty = tty_port_tty_get(&port->port);
  435. if (tty && (tty->termios.c_cflag & CRTSCTS)) {
  436. int cts = (status & UART_MSR_CTS);
  437. if (tty->hw_stopped) {
  438. if (cts) {
  439. tty->hw_stopped = 0;
  440. sdio_uart_start_tx(port);
  441. tty_wakeup(tty);
  442. }
  443. } else {
  444. if (!cts) {
  445. tty->hw_stopped = 1;
  446. sdio_uart_stop_tx(port);
  447. }
  448. }
  449. }
  450. tty_kref_put(tty);
  451. }
  452. }
  453. /*
  454. * This handles the interrupt from one port.
  455. */
  456. static void sdio_uart_irq(struct sdio_func *func)
  457. {
  458. struct sdio_uart_port *port = sdio_get_drvdata(func);
  459. unsigned int iir, lsr;
  460. /*
  461. * In a few places sdio_uart_irq() is called directly instead of
  462. * waiting for the actual interrupt to be raised and the SDIO IRQ
  463. * thread scheduled in order to reduce latency. However, some
  464. * interaction with the tty core may end up calling us back
  465. * (serial echo, flow control, etc.) through those same places
  466. * causing undesirable effects. Let's stop the recursion here.
  467. */
  468. if (unlikely(port->in_sdio_uart_irq == current))
  469. return;
  470. iir = sdio_in(port, UART_IIR);
  471. if (iir & UART_IIR_NO_INT)
  472. return;
  473. port->in_sdio_uart_irq = current;
  474. lsr = sdio_in(port, UART_LSR);
  475. if (lsr & UART_LSR_DR)
  476. sdio_uart_receive_chars(port, &lsr);
  477. sdio_uart_check_modem_status(port);
  478. if (lsr & UART_LSR_THRE)
  479. sdio_uart_transmit_chars(port);
  480. port->in_sdio_uart_irq = NULL;
  481. }
  482. static int uart_carrier_raised(struct tty_port *tport)
  483. {
  484. struct sdio_uart_port *port =
  485. container_of(tport, struct sdio_uart_port, port);
  486. unsigned int ret = sdio_uart_claim_func(port);
  487. if (ret) /* Missing hardware shouldn't block for carrier */
  488. return 1;
  489. ret = sdio_uart_get_mctrl(port);
  490. sdio_uart_release_func(port);
  491. if (ret & TIOCM_CAR)
  492. return 1;
  493. return 0;
  494. }
  495. /**
  496. * uart_dtr_rts - port helper to set uart signals
  497. * @tport: tty port to be updated
  498. * @onoff: set to turn on DTR/RTS
  499. *
  500. * Called by the tty port helpers when the modem signals need to be
  501. * adjusted during an open, close and hangup.
  502. */
  503. static void uart_dtr_rts(struct tty_port *tport, int onoff)
  504. {
  505. struct sdio_uart_port *port =
  506. container_of(tport, struct sdio_uart_port, port);
  507. int ret = sdio_uart_claim_func(port);
  508. if (ret)
  509. return;
  510. if (onoff == 0)
  511. sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  512. else
  513. sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  514. sdio_uart_release_func(port);
  515. }
  516. /**
  517. * sdio_uart_activate - start up hardware
  518. * @tport: tty port to activate
  519. * @tty: tty bound to this port
  520. *
  521. * Activate a tty port. The port locking guarantees us this will be
  522. * run exactly once per set of opens, and if successful will see the
  523. * shutdown method run exactly once to match. Start up and shutdown are
  524. * protected from each other by the internal locking and will not run
  525. * at the same time even during a hangup event.
  526. *
  527. * If we successfully start up the port we take an extra kref as we
  528. * will keep it around until shutdown when the kref is dropped.
  529. */
  530. static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
  531. {
  532. struct sdio_uart_port *port =
  533. container_of(tport, struct sdio_uart_port, port);
  534. int ret;
  535. /*
  536. * Set the TTY IO error marker - we will only clear this
  537. * once we have successfully opened the port.
  538. */
  539. set_bit(TTY_IO_ERROR, &tty->flags);
  540. kfifo_reset(&port->xmit_fifo);
  541. ret = sdio_uart_claim_func(port);
  542. if (ret)
  543. return ret;
  544. ret = sdio_enable_func(port->func);
  545. if (ret)
  546. goto err1;
  547. ret = sdio_claim_irq(port->func, sdio_uart_irq);
  548. if (ret)
  549. goto err2;
  550. /*
  551. * Clear the FIFO buffers and disable them.
  552. * (they will be reenabled in sdio_change_speed())
  553. */
  554. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
  555. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
  556. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  557. sdio_out(port, UART_FCR, 0);
  558. /*
  559. * Clear the interrupt registers.
  560. */
  561. (void) sdio_in(port, UART_LSR);
  562. (void) sdio_in(port, UART_RX);
  563. (void) sdio_in(port, UART_IIR);
  564. (void) sdio_in(port, UART_MSR);
  565. /*
  566. * Now, initialize the UART
  567. */
  568. sdio_out(port, UART_LCR, UART_LCR_WLEN8);
  569. port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
  570. port->mctrl = TIOCM_OUT2;
  571. sdio_uart_change_speed(port, &tty->termios, NULL);
  572. if (tty->termios.c_cflag & CBAUD)
  573. sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
  574. if (tty->termios.c_cflag & CRTSCTS)
  575. if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
  576. tty->hw_stopped = 1;
  577. clear_bit(TTY_IO_ERROR, &tty->flags);
  578. /* Kick the IRQ handler once while we're still holding the host lock */
  579. sdio_uart_irq(port->func);
  580. sdio_uart_release_func(port);
  581. return 0;
  582. err2:
  583. sdio_disable_func(port->func);
  584. err1:
  585. sdio_uart_release_func(port);
  586. return ret;
  587. }
  588. /**
  589. * sdio_uart_shutdown - stop hardware
  590. * @tport: tty port to shut down
  591. *
  592. * Deactivate a tty port. The port locking guarantees us this will be
  593. * run only if a successful matching activate already ran. The two are
  594. * protected from each other by the internal locking and will not run
  595. * at the same time even during a hangup event.
  596. */
  597. static void sdio_uart_shutdown(struct tty_port *tport)
  598. {
  599. struct sdio_uart_port *port =
  600. container_of(tport, struct sdio_uart_port, port);
  601. int ret;
  602. ret = sdio_uart_claim_func(port);
  603. if (ret)
  604. return;
  605. sdio_uart_stop_rx(port);
  606. /* Disable interrupts from this port */
  607. sdio_release_irq(port->func);
  608. port->ier = 0;
  609. sdio_out(port, UART_IER, 0);
  610. sdio_uart_clear_mctrl(port, TIOCM_OUT2);
  611. /* Disable break condition and FIFOs. */
  612. port->lcr &= ~UART_LCR_SBC;
  613. sdio_out(port, UART_LCR, port->lcr);
  614. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
  615. UART_FCR_CLEAR_RCVR |
  616. UART_FCR_CLEAR_XMIT);
  617. sdio_out(port, UART_FCR, 0);
  618. sdio_disable_func(port->func);
  619. sdio_uart_release_func(port);
  620. }
  621. static void sdio_uart_port_destroy(struct tty_port *tport)
  622. {
  623. struct sdio_uart_port *port =
  624. container_of(tport, struct sdio_uart_port, port);
  625. kfifo_free(&port->xmit_fifo);
  626. kfree(port);
  627. }
  628. /**
  629. * sdio_uart_install - install method
  630. * @driver: the driver in use (sdio_uart in our case)
  631. * @tty: the tty being bound
  632. *
  633. * Look up and bind the tty and the driver together. Initialize
  634. * any needed private data (in our case the termios)
  635. */
  636. static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
  637. {
  638. int idx = tty->index;
  639. struct sdio_uart_port *port = sdio_uart_port_get(idx);
  640. int ret = tty_standard_install(driver, tty);
  641. if (ret == 0)
  642. /* This is the ref sdio_uart_port get provided */
  643. tty->driver_data = port;
  644. else
  645. sdio_uart_port_put(port);
  646. return ret;
  647. }
  648. /**
  649. * sdio_uart_cleanup - called on the last tty kref drop
  650. * @tty: the tty being destroyed
  651. *
  652. * Called asynchronously when the last reference to the tty is dropped.
  653. * We cannot destroy the tty->driver_data port kref until this point
  654. */
  655. static void sdio_uart_cleanup(struct tty_struct *tty)
  656. {
  657. struct sdio_uart_port *port = tty->driver_data;
  658. tty->driver_data = NULL; /* Bug trap */
  659. sdio_uart_port_put(port);
  660. }
  661. /*
  662. * Open/close/hangup is now entirely boilerplate
  663. */
  664. static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
  665. {
  666. struct sdio_uart_port *port = tty->driver_data;
  667. return tty_port_open(&port->port, tty, filp);
  668. }
  669. static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
  670. {
  671. struct sdio_uart_port *port = tty->driver_data;
  672. tty_port_close(&port->port, tty, filp);
  673. }
  674. static void sdio_uart_hangup(struct tty_struct *tty)
  675. {
  676. struct sdio_uart_port *port = tty->driver_data;
  677. tty_port_hangup(&port->port);
  678. }
  679. static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
  680. int count)
  681. {
  682. struct sdio_uart_port *port = tty->driver_data;
  683. int ret;
  684. if (!port->func)
  685. return -ENODEV;
  686. ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
  687. if (!(port->ier & UART_IER_THRI)) {
  688. int err = sdio_uart_claim_func(port);
  689. if (!err) {
  690. sdio_uart_start_tx(port);
  691. sdio_uart_irq(port->func);
  692. sdio_uart_release_func(port);
  693. } else
  694. ret = err;
  695. }
  696. return ret;
  697. }
  698. static int sdio_uart_write_room(struct tty_struct *tty)
  699. {
  700. struct sdio_uart_port *port = tty->driver_data;
  701. return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
  702. }
  703. static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
  704. {
  705. struct sdio_uart_port *port = tty->driver_data;
  706. return kfifo_len(&port->xmit_fifo);
  707. }
  708. static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
  709. {
  710. struct sdio_uart_port *port = tty->driver_data;
  711. port->x_char = ch;
  712. if (ch && !(port->ier & UART_IER_THRI)) {
  713. if (sdio_uart_claim_func(port) != 0)
  714. return;
  715. sdio_uart_start_tx(port);
  716. sdio_uart_irq(port->func);
  717. sdio_uart_release_func(port);
  718. }
  719. }
  720. static void sdio_uart_throttle(struct tty_struct *tty)
  721. {
  722. struct sdio_uart_port *port = tty->driver_data;
  723. if (!I_IXOFF(tty) && !(tty->termios.c_cflag & CRTSCTS))
  724. return;
  725. if (sdio_uart_claim_func(port) != 0)
  726. return;
  727. if (I_IXOFF(tty)) {
  728. port->x_char = STOP_CHAR(tty);
  729. sdio_uart_start_tx(port);
  730. }
  731. if (tty->termios.c_cflag & CRTSCTS)
  732. sdio_uart_clear_mctrl(port, TIOCM_RTS);
  733. sdio_uart_irq(port->func);
  734. sdio_uart_release_func(port);
  735. }
  736. static void sdio_uart_unthrottle(struct tty_struct *tty)
  737. {
  738. struct sdio_uart_port *port = tty->driver_data;
  739. if (!I_IXOFF(tty) && !(tty->termios.c_cflag & CRTSCTS))
  740. return;
  741. if (sdio_uart_claim_func(port) != 0)
  742. return;
  743. if (I_IXOFF(tty)) {
  744. if (port->x_char) {
  745. port->x_char = 0;
  746. } else {
  747. port->x_char = START_CHAR(tty);
  748. sdio_uart_start_tx(port);
  749. }
  750. }
  751. if (tty->termios.c_cflag & CRTSCTS)
  752. sdio_uart_set_mctrl(port, TIOCM_RTS);
  753. sdio_uart_irq(port->func);
  754. sdio_uart_release_func(port);
  755. }
  756. static void sdio_uart_set_termios(struct tty_struct *tty,
  757. struct ktermios *old_termios)
  758. {
  759. struct sdio_uart_port *port = tty->driver_data;
  760. unsigned int cflag = tty->termios.c_cflag;
  761. if (sdio_uart_claim_func(port) != 0)
  762. return;
  763. sdio_uart_change_speed(port, &tty->termios, old_termios);
  764. /* Handle transition to B0 status */
  765. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
  766. sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
  767. /* Handle transition away from B0 status */
  768. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  769. unsigned int mask = TIOCM_DTR;
  770. if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
  771. mask |= TIOCM_RTS;
  772. sdio_uart_set_mctrl(port, mask);
  773. }
  774. /* Handle turning off CRTSCTS */
  775. if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
  776. tty->hw_stopped = 0;
  777. sdio_uart_start_tx(port);
  778. }
  779. /* Handle turning on CRTSCTS */
  780. if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
  781. if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
  782. tty->hw_stopped = 1;
  783. sdio_uart_stop_tx(port);
  784. }
  785. }
  786. sdio_uart_release_func(port);
  787. }
  788. static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
  789. {
  790. struct sdio_uart_port *port = tty->driver_data;
  791. int result;
  792. result = sdio_uart_claim_func(port);
  793. if (result != 0)
  794. return result;
  795. if (break_state == -1)
  796. port->lcr |= UART_LCR_SBC;
  797. else
  798. port->lcr &= ~UART_LCR_SBC;
  799. sdio_out(port, UART_LCR, port->lcr);
  800. sdio_uart_release_func(port);
  801. return 0;
  802. }
  803. static int sdio_uart_tiocmget(struct tty_struct *tty)
  804. {
  805. struct sdio_uart_port *port = tty->driver_data;
  806. int result;
  807. result = sdio_uart_claim_func(port);
  808. if (!result) {
  809. result = port->mctrl | sdio_uart_get_mctrl(port);
  810. sdio_uart_release_func(port);
  811. }
  812. return result;
  813. }
  814. static int sdio_uart_tiocmset(struct tty_struct *tty,
  815. unsigned int set, unsigned int clear)
  816. {
  817. struct sdio_uart_port *port = tty->driver_data;
  818. int result;
  819. result = sdio_uart_claim_func(port);
  820. if (!result) {
  821. sdio_uart_update_mctrl(port, set, clear);
  822. sdio_uart_release_func(port);
  823. }
  824. return result;
  825. }
  826. static int sdio_uart_proc_show(struct seq_file *m, void *v)
  827. {
  828. int i;
  829. seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
  830. "", "", "");
  831. for (i = 0; i < UART_NR; i++) {
  832. struct sdio_uart_port *port = sdio_uart_port_get(i);
  833. if (port) {
  834. seq_printf(m, "%d: uart:SDIO", i);
  835. if (capable(CAP_SYS_ADMIN)) {
  836. seq_printf(m, " tx:%d rx:%d",
  837. port->icount.tx, port->icount.rx);
  838. if (port->icount.frame)
  839. seq_printf(m, " fe:%d",
  840. port->icount.frame);
  841. if (port->icount.parity)
  842. seq_printf(m, " pe:%d",
  843. port->icount.parity);
  844. if (port->icount.brk)
  845. seq_printf(m, " brk:%d",
  846. port->icount.brk);
  847. if (port->icount.overrun)
  848. seq_printf(m, " oe:%d",
  849. port->icount.overrun);
  850. if (port->icount.cts)
  851. seq_printf(m, " cts:%d",
  852. port->icount.cts);
  853. if (port->icount.dsr)
  854. seq_printf(m, " dsr:%d",
  855. port->icount.dsr);
  856. if (port->icount.rng)
  857. seq_printf(m, " rng:%d",
  858. port->icount.rng);
  859. if (port->icount.dcd)
  860. seq_printf(m, " dcd:%d",
  861. port->icount.dcd);
  862. }
  863. sdio_uart_port_put(port);
  864. seq_putc(m, '\n');
  865. }
  866. }
  867. return 0;
  868. }
  869. static int sdio_uart_proc_open(struct inode *inode, struct file *file)
  870. {
  871. return single_open(file, sdio_uart_proc_show, NULL);
  872. }
  873. static const struct file_operations sdio_uart_proc_fops = {
  874. .owner = THIS_MODULE,
  875. .open = sdio_uart_proc_open,
  876. .read = seq_read,
  877. .llseek = seq_lseek,
  878. .release = single_release,
  879. };
  880. static const struct tty_port_operations sdio_uart_port_ops = {
  881. .dtr_rts = uart_dtr_rts,
  882. .carrier_raised = uart_carrier_raised,
  883. .shutdown = sdio_uart_shutdown,
  884. .activate = sdio_uart_activate,
  885. .destruct = sdio_uart_port_destroy,
  886. };
  887. static const struct tty_operations sdio_uart_ops = {
  888. .open = sdio_uart_open,
  889. .close = sdio_uart_close,
  890. .write = sdio_uart_write,
  891. .write_room = sdio_uart_write_room,
  892. .chars_in_buffer = sdio_uart_chars_in_buffer,
  893. .send_xchar = sdio_uart_send_xchar,
  894. .throttle = sdio_uart_throttle,
  895. .unthrottle = sdio_uart_unthrottle,
  896. .set_termios = sdio_uart_set_termios,
  897. .hangup = sdio_uart_hangup,
  898. .break_ctl = sdio_uart_break_ctl,
  899. .tiocmget = sdio_uart_tiocmget,
  900. .tiocmset = sdio_uart_tiocmset,
  901. .install = sdio_uart_install,
  902. .cleanup = sdio_uart_cleanup,
  903. .proc_fops = &sdio_uart_proc_fops,
  904. };
  905. static struct tty_driver *sdio_uart_tty_driver;
  906. static int sdio_uart_probe(struct sdio_func *func,
  907. const struct sdio_device_id *id)
  908. {
  909. struct sdio_uart_port *port;
  910. int ret;
  911. port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
  912. if (!port)
  913. return -ENOMEM;
  914. if (func->class == SDIO_CLASS_UART) {
  915. pr_warning("%s: need info on UART class basic setup\n",
  916. sdio_func_id(func));
  917. kfree(port);
  918. return -ENOSYS;
  919. } else if (func->class == SDIO_CLASS_GPS) {
  920. /*
  921. * We need tuple 0x91. It contains SUBTPL_SIOREG
  922. * and SUBTPL_RCVCAPS.
  923. */
  924. struct sdio_func_tuple *tpl;
  925. for (tpl = func->tuples; tpl; tpl = tpl->next) {
  926. if (tpl->code != 0x91)
  927. continue;
  928. if (tpl->size < 10)
  929. continue;
  930. if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
  931. break;
  932. }
  933. if (!tpl) {
  934. pr_warning(
  935. "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
  936. sdio_func_id(func));
  937. kfree(port);
  938. return -EINVAL;
  939. }
  940. pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
  941. sdio_func_id(func), tpl->data[2], tpl->data[3]);
  942. port->regs_offset = (tpl->data[4] << 0) |
  943. (tpl->data[5] << 8) |
  944. (tpl->data[6] << 16);
  945. pr_debug("%s: regs offset = 0x%x\n",
  946. sdio_func_id(func), port->regs_offset);
  947. port->uartclk = tpl->data[7] * 115200;
  948. if (port->uartclk == 0)
  949. port->uartclk = 115200;
  950. pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
  951. sdio_func_id(func), port->uartclk,
  952. tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
  953. } else {
  954. kfree(port);
  955. return -EINVAL;
  956. }
  957. port->func = func;
  958. sdio_set_drvdata(func, port);
  959. tty_port_init(&port->port);
  960. port->port.ops = &sdio_uart_port_ops;
  961. ret = sdio_uart_add_port(port);
  962. if (ret) {
  963. kfree(port);
  964. } else {
  965. struct device *dev;
  966. dev = tty_port_register_device(&port->port,
  967. sdio_uart_tty_driver, port->index, &func->dev);
  968. if (IS_ERR(dev)) {
  969. sdio_uart_port_remove(port);
  970. ret = PTR_ERR(dev);
  971. }
  972. }
  973. return ret;
  974. }
  975. static void sdio_uart_remove(struct sdio_func *func)
  976. {
  977. struct sdio_uart_port *port = sdio_get_drvdata(func);
  978. tty_unregister_device(sdio_uart_tty_driver, port->index);
  979. sdio_uart_port_remove(port);
  980. }
  981. static const struct sdio_device_id sdio_uart_ids[] = {
  982. { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
  983. { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
  984. { /* end: all zeroes */ },
  985. };
  986. MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
  987. static struct sdio_driver sdio_uart_driver = {
  988. .probe = sdio_uart_probe,
  989. .remove = sdio_uart_remove,
  990. .name = "sdio_uart",
  991. .id_table = sdio_uart_ids,
  992. };
  993. static int __init sdio_uart_init(void)
  994. {
  995. int ret;
  996. struct tty_driver *tty_drv;
  997. sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
  998. if (!tty_drv)
  999. return -ENOMEM;
  1000. tty_drv->driver_name = "sdio_uart";
  1001. tty_drv->name = "ttySDIO";
  1002. tty_drv->major = 0; /* dynamically allocated */
  1003. tty_drv->minor_start = 0;
  1004. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  1005. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  1006. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1007. tty_drv->init_termios = tty_std_termios;
  1008. tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
  1009. tty_drv->init_termios.c_ispeed = 4800;
  1010. tty_drv->init_termios.c_ospeed = 4800;
  1011. tty_set_operations(tty_drv, &sdio_uart_ops);
  1012. ret = tty_register_driver(tty_drv);
  1013. if (ret)
  1014. goto err1;
  1015. ret = sdio_register_driver(&sdio_uart_driver);
  1016. if (ret)
  1017. goto err2;
  1018. return 0;
  1019. err2:
  1020. tty_unregister_driver(tty_drv);
  1021. err1:
  1022. put_tty_driver(tty_drv);
  1023. return ret;
  1024. }
  1025. static void __exit sdio_uart_exit(void)
  1026. {
  1027. sdio_unregister_driver(&sdio_uart_driver);
  1028. tty_unregister_driver(sdio_uart_tty_driver);
  1029. put_tty_driver(sdio_uart_tty_driver);
  1030. }
  1031. module_init(sdio_uart_init);
  1032. module_exit(sdio_uart_exit);
  1033. MODULE_AUTHOR("Nicolas Pitre");
  1034. MODULE_LICENSE("GPL");