sdio_uart.c 28 KB

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