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