sdio_uart.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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/sched.h>
  31. #include <linux/mutex.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/circ_buf.h>
  35. #include <linux/tty.h>
  36. #include <linux/tty_flip.h>
  37. #include <linux/kfifo.h>
  38. #include <linux/slab.h>
  39. #include <linux/mmc/core.h>
  40. #include <linux/mmc/card.h>
  41. #include <linux/mmc/sdio_func.h>
  42. #include <linux/mmc/sdio_ids.h>
  43. #define UART_NR 8 /* Number of UARTs this driver can handle */
  44. #define FIFO_SIZE PAGE_SIZE
  45. #define WAKEUP_CHARS 256
  46. struct uart_icount {
  47. __u32 cts;
  48. __u32 dsr;
  49. __u32 rng;
  50. __u32 dcd;
  51. __u32 rx;
  52. __u32 tx;
  53. __u32 frame;
  54. __u32 overrun;
  55. __u32 parity;
  56. __u32 brk;
  57. };
  58. struct sdio_uart_port {
  59. struct tty_port port;
  60. unsigned int index;
  61. struct sdio_func *func;
  62. struct mutex func_lock;
  63. struct task_struct *in_sdio_uart_irq;
  64. unsigned int regs_offset;
  65. struct kfifo xmit_fifo;
  66. spinlock_t write_lock;
  67. struct uart_icount icount;
  68. unsigned int uartclk;
  69. unsigned int mctrl;
  70. unsigned int rx_mctrl;
  71. unsigned int read_status_mask;
  72. unsigned int ignore_status_mask;
  73. unsigned char x_char;
  74. unsigned char ier;
  75. unsigned char lcr;
  76. };
  77. static struct sdio_uart_port *sdio_uart_table[UART_NR];
  78. static DEFINE_SPINLOCK(sdio_uart_table_lock);
  79. static int sdio_uart_add_port(struct sdio_uart_port *port)
  80. {
  81. int index, ret = -EBUSY;
  82. mutex_init(&port->func_lock);
  83. spin_lock_init(&port->write_lock);
  84. if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
  85. return -ENOMEM;
  86. spin_lock(&sdio_uart_table_lock);
  87. for (index = 0; index < UART_NR; index++) {
  88. if (!sdio_uart_table[index]) {
  89. port->index = index;
  90. sdio_uart_table[index] = port;
  91. ret = 0;
  92. break;
  93. }
  94. }
  95. spin_unlock(&sdio_uart_table_lock);
  96. return ret;
  97. }
  98. static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
  99. {
  100. struct sdio_uart_port *port;
  101. if (index >= UART_NR)
  102. return NULL;
  103. spin_lock(&sdio_uart_table_lock);
  104. port = sdio_uart_table[index];
  105. if (port)
  106. tty_port_get(&port->port);
  107. spin_unlock(&sdio_uart_table_lock);
  108. return port;
  109. }
  110. static void sdio_uart_port_put(struct sdio_uart_port *port)
  111. {
  112. tty_port_put(&port->port);
  113. }
  114. static void sdio_uart_port_remove(struct sdio_uart_port *port)
  115. {
  116. struct sdio_func *func;
  117. struct tty_struct *tty;
  118. BUG_ON(sdio_uart_table[port->index] != port);
  119. spin_lock(&sdio_uart_table_lock);
  120. sdio_uart_table[port->index] = NULL;
  121. spin_unlock(&sdio_uart_table_lock);
  122. /*
  123. * We're killing a port that potentially still is in use by
  124. * the tty layer. Be careful to prevent any further access
  125. * to the SDIO function and arrange for the tty layer to
  126. * give up on that port ASAP.
  127. * Beware: the lock ordering is critical.
  128. */
  129. mutex_lock(&port->port.mutex);
  130. mutex_lock(&port->func_lock);
  131. func = port->func;
  132. sdio_claim_host(func);
  133. port->func = NULL;
  134. mutex_unlock(&port->func_lock);
  135. tty = tty_port_tty_get(&port->port);
  136. /* tty_hangup is async so is this safe as is ?? */
  137. if (tty) {
  138. tty_hangup(tty);
  139. tty_kref_put(tty);
  140. }
  141. mutex_unlock(&port->port.mutex);
  142. sdio_release_irq(func);
  143. sdio_disable_func(func);
  144. sdio_release_host(func);
  145. sdio_uart_port_put(port);
  146. }
  147. static int sdio_uart_claim_func(struct sdio_uart_port *port)
  148. {
  149. mutex_lock(&port->func_lock);
  150. if (unlikely(!port->func)) {
  151. mutex_unlock(&port->func_lock);
  152. return -ENODEV;
  153. }
  154. if (likely(port->in_sdio_uart_irq != current))
  155. sdio_claim_host(port->func);
  156. mutex_unlock(&port->func_lock);
  157. return 0;
  158. }
  159. static inline void sdio_uart_release_func(struct sdio_uart_port *port)
  160. {
  161. if (likely(port->in_sdio_uart_irq != current))
  162. sdio_release_host(port->func);
  163. }
  164. static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
  165. {
  166. unsigned char c;
  167. c = sdio_readb(port->func, port->regs_offset + offset, NULL);
  168. return c;
  169. }
  170. static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
  171. {
  172. sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
  173. }
  174. static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
  175. {
  176. unsigned char status;
  177. unsigned int ret;
  178. /* FIXME: What stops this losing the delta bits and breaking
  179. sdio_uart_check_modem_status ? */
  180. status = sdio_in(port, UART_MSR);
  181. ret = 0;
  182. if (status & UART_MSR_DCD)
  183. ret |= TIOCM_CAR;
  184. if (status & UART_MSR_RI)
  185. ret |= TIOCM_RNG;
  186. if (status & UART_MSR_DSR)
  187. ret |= TIOCM_DSR;
  188. if (status & UART_MSR_CTS)
  189. ret |= TIOCM_CTS;
  190. return ret;
  191. }
  192. static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
  193. 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,
  329. unsigned int *status)
  330. {
  331. struct tty_struct *tty = tty_port_tty_get(&port->port);
  332. unsigned int ch, flag;
  333. int max_count = 256;
  334. do {
  335. ch = sdio_in(port, UART_RX);
  336. flag = TTY_NORMAL;
  337. port->icount.rx++;
  338. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  339. UART_LSR_FE | UART_LSR_OE))) {
  340. /*
  341. * For statistics only
  342. */
  343. if (*status & UART_LSR_BI) {
  344. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  345. port->icount.brk++;
  346. } else if (*status & UART_LSR_PE)
  347. port->icount.parity++;
  348. else if (*status & UART_LSR_FE)
  349. port->icount.frame++;
  350. if (*status & UART_LSR_OE)
  351. port->icount.overrun++;
  352. /*
  353. * Mask off conditions which should be ignored.
  354. */
  355. *status &= port->read_status_mask;
  356. if (*status & UART_LSR_BI)
  357. flag = TTY_BREAK;
  358. else if (*status & UART_LSR_PE)
  359. flag = TTY_PARITY;
  360. else if (*status & UART_LSR_FE)
  361. flag = TTY_FRAME;
  362. }
  363. if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
  364. if (tty)
  365. tty_insert_flip_char(tty, ch, flag);
  366. /*
  367. * Overrun is special. Since it's reported immediately,
  368. * it doesn't affect the current character.
  369. */
  370. if (*status & ~port->ignore_status_mask & UART_LSR_OE)
  371. if (tty)
  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. if (tty) {
  376. tty_flip_buffer_push(tty);
  377. tty_kref_put(tty);
  378. }
  379. }
  380. static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
  381. {
  382. struct kfifo *xmit = &port->xmit_fifo;
  383. int count;
  384. struct tty_struct *tty;
  385. u8 iobuf[16];
  386. int len;
  387. if (port->x_char) {
  388. sdio_out(port, UART_TX, port->x_char);
  389. port->icount.tx++;
  390. port->x_char = 0;
  391. return;
  392. }
  393. tty = tty_port_tty_get(&port->port);
  394. if (tty == NULL || !kfifo_len(xmit) ||
  395. tty->stopped || tty->hw_stopped) {
  396. sdio_uart_stop_tx(port);
  397. tty_kref_put(tty);
  398. return;
  399. }
  400. len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock);
  401. for (count = 0; count < len; count++) {
  402. sdio_out(port, UART_TX, iobuf[count]);
  403. port->icount.tx++;
  404. }
  405. len = kfifo_len(xmit);
  406. if (len < WAKEUP_CHARS) {
  407. tty_wakeup(tty);
  408. if (len == 0)
  409. sdio_uart_stop_tx(port);
  410. }
  411. tty_kref_put(tty);
  412. }
  413. static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
  414. {
  415. int status;
  416. struct tty_struct *tty;
  417. status = sdio_in(port, UART_MSR);
  418. if ((status & UART_MSR_ANY_DELTA) == 0)
  419. return;
  420. if (status & UART_MSR_TERI)
  421. port->icount.rng++;
  422. if (status & UART_MSR_DDSR)
  423. port->icount.dsr++;
  424. if (status & UART_MSR_DDCD) {
  425. port->icount.dcd++;
  426. /* DCD raise - wake for open */
  427. if (status & UART_MSR_DCD)
  428. wake_up_interruptible(&port->port.open_wait);
  429. else {
  430. /* DCD drop - hang up if tty attached */
  431. tty = tty_port_tty_get(&port->port);
  432. if (tty) {
  433. tty_hangup(tty);
  434. tty_kref_put(tty);
  435. }
  436. }
  437. }
  438. if (status & UART_MSR_DCTS) {
  439. port->icount.cts++;
  440. tty = tty_port_tty_get(&port->port);
  441. if (tty && (tty->termios.c_cflag & CRTSCTS)) {
  442. int cts = (status & UART_MSR_CTS);
  443. if (tty->hw_stopped) {
  444. if (cts) {
  445. tty->hw_stopped = 0;
  446. sdio_uart_start_tx(port);
  447. tty_wakeup(tty);
  448. }
  449. } else {
  450. if (!cts) {
  451. tty->hw_stopped = 1;
  452. sdio_uart_stop_tx(port);
  453. }
  454. }
  455. }
  456. tty_kref_put(tty);
  457. }
  458. }
  459. /*
  460. * This handles the interrupt from one port.
  461. */
  462. static void sdio_uart_irq(struct sdio_func *func)
  463. {
  464. struct sdio_uart_port *port = sdio_get_drvdata(func);
  465. unsigned int iir, lsr;
  466. /*
  467. * In a few places sdio_uart_irq() is called directly instead of
  468. * waiting for the actual interrupt to be raised and the SDIO IRQ
  469. * thread scheduled in order to reduce latency. However, some
  470. * interaction with the tty core may end up calling us back
  471. * (serial echo, flow control, etc.) through those same places
  472. * causing undesirable effects. Let's stop the recursion here.
  473. */
  474. if (unlikely(port->in_sdio_uart_irq == current))
  475. return;
  476. iir = sdio_in(port, UART_IIR);
  477. if (iir & UART_IIR_NO_INT)
  478. return;
  479. port->in_sdio_uart_irq = current;
  480. lsr = sdio_in(port, UART_LSR);
  481. if (lsr & UART_LSR_DR)
  482. sdio_uart_receive_chars(port, &lsr);
  483. sdio_uart_check_modem_status(port);
  484. if (lsr & UART_LSR_THRE)
  485. sdio_uart_transmit_chars(port);
  486. port->in_sdio_uart_irq = NULL;
  487. }
  488. static int uart_carrier_raised(struct tty_port *tport)
  489. {
  490. struct sdio_uart_port *port =
  491. container_of(tport, struct sdio_uart_port, port);
  492. unsigned int ret = sdio_uart_claim_func(port);
  493. if (ret) /* Missing hardware shouldn't block for carrier */
  494. return 1;
  495. ret = sdio_uart_get_mctrl(port);
  496. sdio_uart_release_func(port);
  497. if (ret & TIOCM_CAR)
  498. return 1;
  499. return 0;
  500. }
  501. /**
  502. * uart_dtr_rts - port helper to set uart signals
  503. * @tport: tty port to be updated
  504. * @onoff: set to turn on DTR/RTS
  505. *
  506. * Called by the tty port helpers when the modem signals need to be
  507. * adjusted during an open, close and hangup.
  508. */
  509. static void uart_dtr_rts(struct tty_port *tport, int onoff)
  510. {
  511. struct sdio_uart_port *port =
  512. container_of(tport, struct sdio_uart_port, port);
  513. int ret = sdio_uart_claim_func(port);
  514. if (ret)
  515. return;
  516. if (onoff == 0)
  517. sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  518. else
  519. sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  520. sdio_uart_release_func(port);
  521. }
  522. /**
  523. * sdio_uart_activate - start up hardware
  524. * @tport: tty port to activate
  525. * @tty: tty bound to this port
  526. *
  527. * Activate a tty port. The port locking guarantees us this will be
  528. * run exactly once per set of opens, and if successful will see the
  529. * shutdown method run exactly once to match. Start up and shutdown are
  530. * protected from each other by the internal locking and will not run
  531. * at the same time even during a hangup event.
  532. *
  533. * If we successfully start up the port we take an extra kref as we
  534. * will keep it around until shutdown when the kref is dropped.
  535. */
  536. static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
  537. {
  538. struct sdio_uart_port *port =
  539. container_of(tport, struct sdio_uart_port, port);
  540. int ret;
  541. /*
  542. * Set the TTY IO error marker - we will only clear this
  543. * once we have successfully opened the port.
  544. */
  545. set_bit(TTY_IO_ERROR, &tty->flags);
  546. kfifo_reset(&port->xmit_fifo);
  547. ret = sdio_uart_claim_func(port);
  548. if (ret)
  549. return ret;
  550. ret = sdio_enable_func(port->func);
  551. if (ret)
  552. goto err1;
  553. ret = sdio_claim_irq(port->func, sdio_uart_irq);
  554. if (ret)
  555. goto err2;
  556. /*
  557. * Clear the FIFO buffers and disable them.
  558. * (they will be reenabled in sdio_change_speed())
  559. */
  560. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
  561. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
  562. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  563. sdio_out(port, UART_FCR, 0);
  564. /*
  565. * Clear the interrupt registers.
  566. */
  567. (void) sdio_in(port, UART_LSR);
  568. (void) sdio_in(port, UART_RX);
  569. (void) sdio_in(port, UART_IIR);
  570. (void) sdio_in(port, UART_MSR);
  571. /*
  572. * Now, initialize the UART
  573. */
  574. sdio_out(port, UART_LCR, UART_LCR_WLEN8);
  575. port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
  576. port->mctrl = TIOCM_OUT2;
  577. sdio_uart_change_speed(port, &tty->termios, NULL);
  578. if (tty->termios.c_cflag & CBAUD)
  579. sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
  580. if (tty->termios.c_cflag & CRTSCTS)
  581. if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
  582. tty->hw_stopped = 1;
  583. clear_bit(TTY_IO_ERROR, &tty->flags);
  584. /* Kick the IRQ handler once while we're still holding the host lock */
  585. sdio_uart_irq(port->func);
  586. sdio_uart_release_func(port);
  587. return 0;
  588. err2:
  589. sdio_disable_func(port->func);
  590. err1:
  591. sdio_uart_release_func(port);
  592. return ret;
  593. }
  594. /**
  595. * sdio_uart_shutdown - stop hardware
  596. * @tport: tty port to shut down
  597. *
  598. * Deactivate a tty port. The port locking guarantees us this will be
  599. * run only if a successful matching activate already ran. The two are
  600. * protected from each other by the internal locking and will not run
  601. * at the same time even during a hangup event.
  602. */
  603. static void sdio_uart_shutdown(struct tty_port *tport)
  604. {
  605. struct sdio_uart_port *port =
  606. container_of(tport, struct sdio_uart_port, port);
  607. int ret;
  608. ret = sdio_uart_claim_func(port);
  609. if (ret)
  610. return;
  611. sdio_uart_stop_rx(port);
  612. /* Disable interrupts from this port */
  613. sdio_release_irq(port->func);
  614. port->ier = 0;
  615. sdio_out(port, UART_IER, 0);
  616. sdio_uart_clear_mctrl(port, TIOCM_OUT2);
  617. /* Disable break condition and FIFOs. */
  618. port->lcr &= ~UART_LCR_SBC;
  619. sdio_out(port, UART_LCR, port->lcr);
  620. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
  621. UART_FCR_CLEAR_RCVR |
  622. UART_FCR_CLEAR_XMIT);
  623. sdio_out(port, UART_FCR, 0);
  624. sdio_disable_func(port->func);
  625. sdio_uart_release_func(port);
  626. }
  627. static void sdio_uart_port_destroy(struct tty_port *tport)
  628. {
  629. struct sdio_uart_port *port =
  630. container_of(tport, struct sdio_uart_port, port);
  631. kfifo_free(&port->xmit_fifo);
  632. kfree(port);
  633. }
  634. /**
  635. * sdio_uart_install - install method
  636. * @driver: the driver in use (sdio_uart in our case)
  637. * @tty: the tty being bound
  638. *
  639. * Look up and bind the tty and the driver together. Initialize
  640. * any needed private data (in our case the termios)
  641. */
  642. static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
  643. {
  644. int idx = tty->index;
  645. struct sdio_uart_port *port = sdio_uart_port_get(idx);
  646. int ret = tty_standard_install(driver, tty);
  647. if (ret == 0)
  648. /* This is the ref sdio_uart_port get provided */
  649. tty->driver_data = port;
  650. else
  651. sdio_uart_port_put(port);
  652. return ret;
  653. }
  654. /**
  655. * sdio_uart_cleanup - called on the last tty kref drop
  656. * @tty: the tty being destroyed
  657. *
  658. * Called asynchronously when the last reference to the tty is dropped.
  659. * We cannot destroy the tty->driver_data port kref until this point
  660. */
  661. static void sdio_uart_cleanup(struct tty_struct *tty)
  662. {
  663. struct sdio_uart_port *port = tty->driver_data;
  664. tty->driver_data = NULL; /* Bug trap */
  665. sdio_uart_port_put(port);
  666. }
  667. /*
  668. * Open/close/hangup is now entirely boilerplate
  669. */
  670. static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
  671. {
  672. struct sdio_uart_port *port = tty->driver_data;
  673. return tty_port_open(&port->port, tty, filp);
  674. }
  675. static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
  676. {
  677. struct sdio_uart_port *port = tty->driver_data;
  678. tty_port_close(&port->port, tty, filp);
  679. }
  680. static void sdio_uart_hangup(struct tty_struct *tty)
  681. {
  682. struct sdio_uart_port *port = tty->driver_data;
  683. tty_port_hangup(&port->port);
  684. }
  685. static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
  686. int count)
  687. {
  688. struct sdio_uart_port *port = tty->driver_data;
  689. int ret;
  690. if (!port->func)
  691. return -ENODEV;
  692. ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
  693. if (!(port->ier & UART_IER_THRI)) {
  694. int err = sdio_uart_claim_func(port);
  695. if (!err) {
  696. sdio_uart_start_tx(port);
  697. sdio_uart_irq(port->func);
  698. sdio_uart_release_func(port);
  699. } else
  700. ret = err;
  701. }
  702. return ret;
  703. }
  704. static int sdio_uart_write_room(struct tty_struct *tty)
  705. {
  706. struct sdio_uart_port *port = tty->driver_data;
  707. return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
  708. }
  709. static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
  710. {
  711. struct sdio_uart_port *port = tty->driver_data;
  712. return kfifo_len(&port->xmit_fifo);
  713. }
  714. static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
  715. {
  716. struct sdio_uart_port *port = tty->driver_data;
  717. port->x_char = ch;
  718. if (ch && !(port->ier & UART_IER_THRI)) {
  719. if (sdio_uart_claim_func(port) != 0)
  720. return;
  721. sdio_uart_start_tx(port);
  722. sdio_uart_irq(port->func);
  723. sdio_uart_release_func(port);
  724. }
  725. }
  726. static void sdio_uart_throttle(struct tty_struct *tty)
  727. {
  728. struct sdio_uart_port *port = tty->driver_data;
  729. if (!I_IXOFF(tty) && !(tty->termios.c_cflag & CRTSCTS))
  730. return;
  731. if (sdio_uart_claim_func(port) != 0)
  732. return;
  733. if (I_IXOFF(tty)) {
  734. port->x_char = STOP_CHAR(tty);
  735. sdio_uart_start_tx(port);
  736. }
  737. if (tty->termios.c_cflag & CRTSCTS)
  738. sdio_uart_clear_mctrl(port, TIOCM_RTS);
  739. sdio_uart_irq(port->func);
  740. sdio_uart_release_func(port);
  741. }
  742. static void sdio_uart_unthrottle(struct tty_struct *tty)
  743. {
  744. struct sdio_uart_port *port = tty->driver_data;
  745. if (!I_IXOFF(tty) && !(tty->termios.c_cflag & CRTSCTS))
  746. return;
  747. if (sdio_uart_claim_func(port) != 0)
  748. return;
  749. if (I_IXOFF(tty)) {
  750. if (port->x_char) {
  751. port->x_char = 0;
  752. } else {
  753. port->x_char = START_CHAR(tty);
  754. sdio_uart_start_tx(port);
  755. }
  756. }
  757. if (tty->termios.c_cflag & CRTSCTS)
  758. sdio_uart_set_mctrl(port, TIOCM_RTS);
  759. sdio_uart_irq(port->func);
  760. sdio_uart_release_func(port);
  761. }
  762. static void sdio_uart_set_termios(struct tty_struct *tty,
  763. struct ktermios *old_termios)
  764. {
  765. struct sdio_uart_port *port = tty->driver_data;
  766. unsigned int cflag = tty->termios.c_cflag;
  767. if (sdio_uart_claim_func(port) != 0)
  768. return;
  769. sdio_uart_change_speed(port, &tty->termios, old_termios);
  770. /* Handle transition to B0 status */
  771. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
  772. sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
  773. /* Handle transition away from B0 status */
  774. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  775. unsigned int mask = TIOCM_DTR;
  776. if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
  777. mask |= TIOCM_RTS;
  778. sdio_uart_set_mctrl(port, mask);
  779. }
  780. /* Handle turning off CRTSCTS */
  781. if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
  782. tty->hw_stopped = 0;
  783. sdio_uart_start_tx(port);
  784. }
  785. /* Handle turning on CRTSCTS */
  786. if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
  787. if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
  788. tty->hw_stopped = 1;
  789. sdio_uart_stop_tx(port);
  790. }
  791. }
  792. sdio_uart_release_func(port);
  793. }
  794. static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
  795. {
  796. struct sdio_uart_port *port = tty->driver_data;
  797. int result;
  798. result = sdio_uart_claim_func(port);
  799. if (result != 0)
  800. return result;
  801. if (break_state == -1)
  802. port->lcr |= UART_LCR_SBC;
  803. else
  804. port->lcr &= ~UART_LCR_SBC;
  805. sdio_out(port, UART_LCR, port->lcr);
  806. sdio_uart_release_func(port);
  807. return 0;
  808. }
  809. static int sdio_uart_tiocmget(struct tty_struct *tty)
  810. {
  811. struct sdio_uart_port *port = tty->driver_data;
  812. int result;
  813. result = sdio_uart_claim_func(port);
  814. if (!result) {
  815. result = port->mctrl | sdio_uart_get_mctrl(port);
  816. sdio_uart_release_func(port);
  817. }
  818. return result;
  819. }
  820. static int sdio_uart_tiocmset(struct tty_struct *tty,
  821. unsigned int set, unsigned int clear)
  822. {
  823. struct sdio_uart_port *port = tty->driver_data;
  824. int result;
  825. result = sdio_uart_claim_func(port);
  826. if (!result) {
  827. sdio_uart_update_mctrl(port, set, clear);
  828. sdio_uart_release_func(port);
  829. }
  830. return result;
  831. }
  832. static int sdio_uart_proc_show(struct seq_file *m, void *v)
  833. {
  834. int i;
  835. seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
  836. "", "", "");
  837. for (i = 0; i < UART_NR; i++) {
  838. struct sdio_uart_port *port = sdio_uart_port_get(i);
  839. if (port) {
  840. seq_printf(m, "%d: uart:SDIO", i);
  841. if (capable(CAP_SYS_ADMIN)) {
  842. seq_printf(m, " tx:%d rx:%d",
  843. port->icount.tx, port->icount.rx);
  844. if (port->icount.frame)
  845. seq_printf(m, " fe:%d",
  846. port->icount.frame);
  847. if (port->icount.parity)
  848. seq_printf(m, " pe:%d",
  849. port->icount.parity);
  850. if (port->icount.brk)
  851. seq_printf(m, " brk:%d",
  852. port->icount.brk);
  853. if (port->icount.overrun)
  854. seq_printf(m, " oe:%d",
  855. port->icount.overrun);
  856. if (port->icount.cts)
  857. seq_printf(m, " cts:%d",
  858. port->icount.cts);
  859. if (port->icount.dsr)
  860. seq_printf(m, " dsr:%d",
  861. port->icount.dsr);
  862. if (port->icount.rng)
  863. seq_printf(m, " rng:%d",
  864. port->icount.rng);
  865. if (port->icount.dcd)
  866. seq_printf(m, " dcd:%d",
  867. port->icount.dcd);
  868. }
  869. sdio_uart_port_put(port);
  870. seq_putc(m, '\n');
  871. }
  872. }
  873. return 0;
  874. }
  875. static int sdio_uart_proc_open(struct inode *inode, struct file *file)
  876. {
  877. return single_open(file, sdio_uart_proc_show, NULL);
  878. }
  879. static const struct file_operations sdio_uart_proc_fops = {
  880. .owner = THIS_MODULE,
  881. .open = sdio_uart_proc_open,
  882. .read = seq_read,
  883. .llseek = seq_lseek,
  884. .release = single_release,
  885. };
  886. static const struct tty_port_operations sdio_uart_port_ops = {
  887. .dtr_rts = uart_dtr_rts,
  888. .carrier_raised = uart_carrier_raised,
  889. .shutdown = sdio_uart_shutdown,
  890. .activate = sdio_uart_activate,
  891. .destruct = sdio_uart_port_destroy,
  892. };
  893. static const struct tty_operations sdio_uart_ops = {
  894. .open = sdio_uart_open,
  895. .close = sdio_uart_close,
  896. .write = sdio_uart_write,
  897. .write_room = sdio_uart_write_room,
  898. .chars_in_buffer = sdio_uart_chars_in_buffer,
  899. .send_xchar = sdio_uart_send_xchar,
  900. .throttle = sdio_uart_throttle,
  901. .unthrottle = sdio_uart_unthrottle,
  902. .set_termios = sdio_uart_set_termios,
  903. .hangup = sdio_uart_hangup,
  904. .break_ctl = sdio_uart_break_ctl,
  905. .tiocmget = sdio_uart_tiocmget,
  906. .tiocmset = sdio_uart_tiocmset,
  907. .install = sdio_uart_install,
  908. .cleanup = sdio_uart_cleanup,
  909. .proc_fops = &sdio_uart_proc_fops,
  910. };
  911. static struct tty_driver *sdio_uart_tty_driver;
  912. static int sdio_uart_probe(struct sdio_func *func,
  913. const struct sdio_device_id *id)
  914. {
  915. struct sdio_uart_port *port;
  916. int ret;
  917. port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
  918. if (!port)
  919. return -ENOMEM;
  920. if (func->class == SDIO_CLASS_UART) {
  921. pr_warning("%s: need info on UART class basic setup\n",
  922. sdio_func_id(func));
  923. kfree(port);
  924. return -ENOSYS;
  925. } else if (func->class == SDIO_CLASS_GPS) {
  926. /*
  927. * We need tuple 0x91. It contains SUBTPL_SIOREG
  928. * and SUBTPL_RCVCAPS.
  929. */
  930. struct sdio_func_tuple *tpl;
  931. for (tpl = func->tuples; tpl; tpl = tpl->next) {
  932. if (tpl->code != 0x91)
  933. continue;
  934. if (tpl->size < 10)
  935. continue;
  936. if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
  937. break;
  938. }
  939. if (!tpl) {
  940. pr_warning(
  941. "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
  942. sdio_func_id(func));
  943. kfree(port);
  944. return -EINVAL;
  945. }
  946. pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
  947. sdio_func_id(func), tpl->data[2], tpl->data[3]);
  948. port->regs_offset = (tpl->data[4] << 0) |
  949. (tpl->data[5] << 8) |
  950. (tpl->data[6] << 16);
  951. pr_debug("%s: regs offset = 0x%x\n",
  952. sdio_func_id(func), port->regs_offset);
  953. port->uartclk = tpl->data[7] * 115200;
  954. if (port->uartclk == 0)
  955. port->uartclk = 115200;
  956. pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
  957. sdio_func_id(func), port->uartclk,
  958. tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
  959. } else {
  960. kfree(port);
  961. return -EINVAL;
  962. }
  963. port->func = func;
  964. sdio_set_drvdata(func, port);
  965. tty_port_init(&port->port);
  966. port->port.ops = &sdio_uart_port_ops;
  967. ret = sdio_uart_add_port(port);
  968. if (ret) {
  969. kfree(port);
  970. } else {
  971. struct device *dev;
  972. dev = tty_port_register_device(&port->port,
  973. sdio_uart_tty_driver, port->index, &func->dev);
  974. if (IS_ERR(dev)) {
  975. sdio_uart_port_remove(port);
  976. ret = PTR_ERR(dev);
  977. }
  978. }
  979. return ret;
  980. }
  981. static void sdio_uart_remove(struct sdio_func *func)
  982. {
  983. struct sdio_uart_port *port = sdio_get_drvdata(func);
  984. tty_unregister_device(sdio_uart_tty_driver, port->index);
  985. sdio_uart_port_remove(port);
  986. }
  987. static const struct sdio_device_id sdio_uart_ids[] = {
  988. { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
  989. { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
  990. { /* end: all zeroes */ },
  991. };
  992. MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
  993. static struct sdio_driver sdio_uart_driver = {
  994. .probe = sdio_uart_probe,
  995. .remove = sdio_uart_remove,
  996. .name = "sdio_uart",
  997. .id_table = sdio_uart_ids,
  998. };
  999. static int __init sdio_uart_init(void)
  1000. {
  1001. int ret;
  1002. struct tty_driver *tty_drv;
  1003. sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
  1004. if (!tty_drv)
  1005. return -ENOMEM;
  1006. tty_drv->driver_name = "sdio_uart";
  1007. tty_drv->name = "ttySDIO";
  1008. tty_drv->major = 0; /* dynamically allocated */
  1009. tty_drv->minor_start = 0;
  1010. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  1011. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  1012. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1013. tty_drv->init_termios = tty_std_termios;
  1014. tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
  1015. tty_drv->init_termios.c_ispeed = 4800;
  1016. tty_drv->init_termios.c_ospeed = 4800;
  1017. tty_set_operations(tty_drv, &sdio_uart_ops);
  1018. ret = tty_register_driver(tty_drv);
  1019. if (ret)
  1020. goto err1;
  1021. ret = sdio_register_driver(&sdio_uart_driver);
  1022. if (ret)
  1023. goto err2;
  1024. return 0;
  1025. err2:
  1026. tty_unregister_driver(tty_drv);
  1027. err1:
  1028. put_tty_driver(tty_drv);
  1029. return ret;
  1030. }
  1031. static void __exit sdio_uart_exit(void)
  1032. {
  1033. sdio_unregister_driver(&sdio_uart_driver);
  1034. tty_unregister_driver(sdio_uart_tty_driver);
  1035. put_tty_driver(sdio_uart_tty_driver);
  1036. }
  1037. module_init(sdio_uart_init);
  1038. module_exit(sdio_uart_exit);
  1039. MODULE_AUTHOR("Nicolas Pitre");
  1040. MODULE_LICENSE("GPL");