sdio_uart.c 25 KB

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