sdio_uart.c 29 KB

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