sdio_uart.c 29 KB

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