sdio_uart.c 28 KB

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