sdio_uart.c 29 KB

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