sdio_uart.c 27 KB

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