sdio_uart.c 28 KB

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