sdio_uart.c 27 KB

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