sdio_uart.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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_init_termios(tty);
  650. if (ret == 0) {
  651. tty_driver_kref_get(driver);
  652. tty->count++;
  653. /* This is the ref sdio_uart_port get provided */
  654. tty->driver_data = port;
  655. driver->ttys[idx] = tty;
  656. } else
  657. sdio_uart_port_put(port);
  658. return ret;
  659. }
  660. /**
  661. * sdio_uart_cleanup - called on the last tty kref drop
  662. * @tty: the tty being destroyed
  663. *
  664. * Called asynchronously when the last reference to the tty is dropped.
  665. * We cannot destroy the tty->driver_data port kref until this point
  666. */
  667. static void sdio_uart_cleanup(struct tty_struct *tty)
  668. {
  669. struct sdio_uart_port *port = tty->driver_data;
  670. tty->driver_data = NULL; /* Bug trap */
  671. sdio_uart_port_put(port);
  672. }
  673. /*
  674. * Open/close/hangup is now entirely boilerplate
  675. */
  676. static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
  677. {
  678. struct sdio_uart_port *port = tty->driver_data;
  679. return tty_port_open(&port->port, tty, filp);
  680. }
  681. static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
  682. {
  683. struct sdio_uart_port *port = tty->driver_data;
  684. tty_port_close(&port->port, tty, filp);
  685. }
  686. static void sdio_uart_hangup(struct tty_struct *tty)
  687. {
  688. struct sdio_uart_port *port = tty->driver_data;
  689. tty_port_hangup(&port->port);
  690. }
  691. static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
  692. int count)
  693. {
  694. struct sdio_uart_port *port = tty->driver_data;
  695. int ret;
  696. if (!port->func)
  697. return -ENODEV;
  698. ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
  699. if (!(port->ier & UART_IER_THRI)) {
  700. int err = sdio_uart_claim_func(port);
  701. if (!err) {
  702. sdio_uart_start_tx(port);
  703. sdio_uart_irq(port->func);
  704. sdio_uart_release_func(port);
  705. } else
  706. ret = err;
  707. }
  708. return ret;
  709. }
  710. static int sdio_uart_write_room(struct tty_struct *tty)
  711. {
  712. struct sdio_uart_port *port = tty->driver_data;
  713. return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
  714. }
  715. static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
  716. {
  717. struct sdio_uart_port *port = tty->driver_data;
  718. return kfifo_len(&port->xmit_fifo);
  719. }
  720. static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
  721. {
  722. struct sdio_uart_port *port = tty->driver_data;
  723. port->x_char = ch;
  724. if (ch && !(port->ier & UART_IER_THRI)) {
  725. if (sdio_uart_claim_func(port) != 0)
  726. return;
  727. sdio_uart_start_tx(port);
  728. sdio_uart_irq(port->func);
  729. sdio_uart_release_func(port);
  730. }
  731. }
  732. static void sdio_uart_throttle(struct tty_struct *tty)
  733. {
  734. struct sdio_uart_port *port = tty->driver_data;
  735. if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
  736. return;
  737. if (sdio_uart_claim_func(port) != 0)
  738. return;
  739. if (I_IXOFF(tty)) {
  740. port->x_char = STOP_CHAR(tty);
  741. sdio_uart_start_tx(port);
  742. }
  743. if (tty->termios->c_cflag & CRTSCTS)
  744. sdio_uart_clear_mctrl(port, TIOCM_RTS);
  745. sdio_uart_irq(port->func);
  746. sdio_uart_release_func(port);
  747. }
  748. static void sdio_uart_unthrottle(struct tty_struct *tty)
  749. {
  750. struct sdio_uart_port *port = tty->driver_data;
  751. if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
  752. return;
  753. if (sdio_uart_claim_func(port) != 0)
  754. return;
  755. if (I_IXOFF(tty)) {
  756. if (port->x_char) {
  757. port->x_char = 0;
  758. } else {
  759. port->x_char = START_CHAR(tty);
  760. sdio_uart_start_tx(port);
  761. }
  762. }
  763. if (tty->termios->c_cflag & CRTSCTS)
  764. sdio_uart_set_mctrl(port, TIOCM_RTS);
  765. sdio_uart_irq(port->func);
  766. sdio_uart_release_func(port);
  767. }
  768. static void sdio_uart_set_termios(struct tty_struct *tty,
  769. struct ktermios *old_termios)
  770. {
  771. struct sdio_uart_port *port = tty->driver_data;
  772. unsigned int cflag = tty->termios->c_cflag;
  773. if (sdio_uart_claim_func(port) != 0)
  774. return;
  775. sdio_uart_change_speed(port, tty->termios, old_termios);
  776. /* Handle transition to B0 status */
  777. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
  778. sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
  779. /* Handle transition away from B0 status */
  780. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  781. unsigned int mask = TIOCM_DTR;
  782. if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
  783. mask |= TIOCM_RTS;
  784. sdio_uart_set_mctrl(port, mask);
  785. }
  786. /* Handle turning off CRTSCTS */
  787. if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
  788. tty->hw_stopped = 0;
  789. sdio_uart_start_tx(port);
  790. }
  791. /* Handle turning on CRTSCTS */
  792. if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
  793. if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
  794. tty->hw_stopped = 1;
  795. sdio_uart_stop_tx(port);
  796. }
  797. }
  798. sdio_uart_release_func(port);
  799. }
  800. static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
  801. {
  802. struct sdio_uart_port *port = tty->driver_data;
  803. int result;
  804. result = sdio_uart_claim_func(port);
  805. if (result != 0)
  806. return result;
  807. if (break_state == -1)
  808. port->lcr |= UART_LCR_SBC;
  809. else
  810. port->lcr &= ~UART_LCR_SBC;
  811. sdio_out(port, UART_LCR, port->lcr);
  812. sdio_uart_release_func(port);
  813. return 0;
  814. }
  815. static int sdio_uart_tiocmget(struct tty_struct *tty)
  816. {
  817. struct sdio_uart_port *port = tty->driver_data;
  818. int result;
  819. result = sdio_uart_claim_func(port);
  820. if (!result) {
  821. result = port->mctrl | sdio_uart_get_mctrl(port);
  822. sdio_uart_release_func(port);
  823. }
  824. return result;
  825. }
  826. static int sdio_uart_tiocmset(struct tty_struct *tty,
  827. unsigned int set, unsigned int clear)
  828. {
  829. struct sdio_uart_port *port = tty->driver_data;
  830. int result;
  831. result = sdio_uart_claim_func(port);
  832. if (!result) {
  833. sdio_uart_update_mctrl(port, set, clear);
  834. sdio_uart_release_func(port);
  835. }
  836. return result;
  837. }
  838. static int sdio_uart_proc_show(struct seq_file *m, void *v)
  839. {
  840. int i;
  841. seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
  842. "", "", "");
  843. for (i = 0; i < UART_NR; i++) {
  844. struct sdio_uart_port *port = sdio_uart_port_get(i);
  845. if (port) {
  846. seq_printf(m, "%d: uart:SDIO", i);
  847. if (capable(CAP_SYS_ADMIN)) {
  848. seq_printf(m, " tx:%d rx:%d",
  849. port->icount.tx, port->icount.rx);
  850. if (port->icount.frame)
  851. seq_printf(m, " fe:%d",
  852. port->icount.frame);
  853. if (port->icount.parity)
  854. seq_printf(m, " pe:%d",
  855. port->icount.parity);
  856. if (port->icount.brk)
  857. seq_printf(m, " brk:%d",
  858. port->icount.brk);
  859. if (port->icount.overrun)
  860. seq_printf(m, " oe:%d",
  861. port->icount.overrun);
  862. if (port->icount.cts)
  863. seq_printf(m, " cts:%d",
  864. port->icount.cts);
  865. if (port->icount.dsr)
  866. seq_printf(m, " dsr:%d",
  867. port->icount.dsr);
  868. if (port->icount.rng)
  869. seq_printf(m, " rng:%d",
  870. port->icount.rng);
  871. if (port->icount.dcd)
  872. seq_printf(m, " dcd:%d",
  873. port->icount.dcd);
  874. }
  875. sdio_uart_port_put(port);
  876. seq_putc(m, '\n');
  877. }
  878. }
  879. return 0;
  880. }
  881. static int sdio_uart_proc_open(struct inode *inode, struct file *file)
  882. {
  883. return single_open(file, sdio_uart_proc_show, NULL);
  884. }
  885. static const struct file_operations sdio_uart_proc_fops = {
  886. .owner = THIS_MODULE,
  887. .open = sdio_uart_proc_open,
  888. .read = seq_read,
  889. .llseek = seq_lseek,
  890. .release = single_release,
  891. };
  892. static const struct tty_port_operations sdio_uart_port_ops = {
  893. .dtr_rts = uart_dtr_rts,
  894. .carrier_raised = uart_carrier_raised,
  895. .shutdown = sdio_uart_shutdown,
  896. .activate = sdio_uart_activate,
  897. };
  898. static const struct tty_operations sdio_uart_ops = {
  899. .open = sdio_uart_open,
  900. .close = sdio_uart_close,
  901. .write = sdio_uart_write,
  902. .write_room = sdio_uart_write_room,
  903. .chars_in_buffer = sdio_uart_chars_in_buffer,
  904. .send_xchar = sdio_uart_send_xchar,
  905. .throttle = sdio_uart_throttle,
  906. .unthrottle = sdio_uart_unthrottle,
  907. .set_termios = sdio_uart_set_termios,
  908. .hangup = sdio_uart_hangup,
  909. .break_ctl = sdio_uart_break_ctl,
  910. .tiocmget = sdio_uart_tiocmget,
  911. .tiocmset = sdio_uart_tiocmset,
  912. .install = sdio_uart_install,
  913. .cleanup = sdio_uart_cleanup,
  914. .proc_fops = &sdio_uart_proc_fops,
  915. };
  916. static struct tty_driver *sdio_uart_tty_driver;
  917. static int sdio_uart_probe(struct sdio_func *func,
  918. const struct sdio_device_id *id)
  919. {
  920. struct sdio_uart_port *port;
  921. int ret;
  922. port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
  923. if (!port)
  924. return -ENOMEM;
  925. if (func->class == SDIO_CLASS_UART) {
  926. printk(KERN_WARNING "%s: need info on UART class basic setup\n",
  927. sdio_func_id(func));
  928. kfree(port);
  929. return -ENOSYS;
  930. } else if (func->class == SDIO_CLASS_GPS) {
  931. /*
  932. * We need tuple 0x91. It contains SUBTPL_SIOREG
  933. * and SUBTPL_RCVCAPS.
  934. */
  935. struct sdio_func_tuple *tpl;
  936. for (tpl = func->tuples; tpl; tpl = tpl->next) {
  937. if (tpl->code != 0x91)
  938. continue;
  939. if (tpl->size < 10)
  940. continue;
  941. if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
  942. break;
  943. }
  944. if (!tpl) {
  945. printk(KERN_WARNING
  946. "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
  947. sdio_func_id(func));
  948. kfree(port);
  949. return -EINVAL;
  950. }
  951. printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
  952. sdio_func_id(func), tpl->data[2], tpl->data[3]);
  953. port->regs_offset = (tpl->data[4] << 0) |
  954. (tpl->data[5] << 8) |
  955. (tpl->data[6] << 16);
  956. printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
  957. sdio_func_id(func), port->regs_offset);
  958. port->uartclk = tpl->data[7] * 115200;
  959. if (port->uartclk == 0)
  960. port->uartclk = 115200;
  961. printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
  962. sdio_func_id(func), port->uartclk,
  963. tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
  964. } else {
  965. kfree(port);
  966. return -EINVAL;
  967. }
  968. port->func = func;
  969. sdio_set_drvdata(func, port);
  970. tty_port_init(&port->port);
  971. port->port.ops = &sdio_uart_port_ops;
  972. ret = sdio_uart_add_port(port);
  973. if (ret) {
  974. kfree(port);
  975. } else {
  976. struct device *dev;
  977. dev = tty_register_device(sdio_uart_tty_driver,
  978. port->index, &func->dev);
  979. if (IS_ERR(dev)) {
  980. sdio_uart_port_remove(port);
  981. ret = PTR_ERR(dev);
  982. }
  983. }
  984. return ret;
  985. }
  986. static void sdio_uart_remove(struct sdio_func *func)
  987. {
  988. struct sdio_uart_port *port = sdio_get_drvdata(func);
  989. tty_unregister_device(sdio_uart_tty_driver, port->index);
  990. sdio_uart_port_remove(port);
  991. }
  992. static const struct sdio_device_id sdio_uart_ids[] = {
  993. { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
  994. { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
  995. { /* end: all zeroes */ },
  996. };
  997. MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
  998. static struct sdio_driver sdio_uart_driver = {
  999. .probe = sdio_uart_probe,
  1000. .remove = sdio_uart_remove,
  1001. .name = "sdio_uart",
  1002. .id_table = sdio_uart_ids,
  1003. };
  1004. static int __init sdio_uart_init(void)
  1005. {
  1006. int ret;
  1007. struct tty_driver *tty_drv;
  1008. sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
  1009. if (!tty_drv)
  1010. return -ENOMEM;
  1011. tty_drv->owner = THIS_MODULE;
  1012. tty_drv->driver_name = "sdio_uart";
  1013. tty_drv->name = "ttySDIO";
  1014. tty_drv->major = 0; /* dynamically allocated */
  1015. tty_drv->minor_start = 0;
  1016. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  1017. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  1018. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1019. tty_drv->init_termios = tty_std_termios;
  1020. tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
  1021. tty_drv->init_termios.c_ispeed = 4800;
  1022. tty_drv->init_termios.c_ospeed = 4800;
  1023. tty_set_operations(tty_drv, &sdio_uart_ops);
  1024. ret = tty_register_driver(tty_drv);
  1025. if (ret)
  1026. goto err1;
  1027. ret = sdio_register_driver(&sdio_uart_driver);
  1028. if (ret)
  1029. goto err2;
  1030. return 0;
  1031. err2:
  1032. tty_unregister_driver(tty_drv);
  1033. err1:
  1034. put_tty_driver(tty_drv);
  1035. return ret;
  1036. }
  1037. static void __exit sdio_uart_exit(void)
  1038. {
  1039. sdio_unregister_driver(&sdio_uart_driver);
  1040. tty_unregister_driver(sdio_uart_tty_driver);
  1041. put_tty_driver(sdio_uart_tty_driver);
  1042. }
  1043. module_init(sdio_uart_init);
  1044. module_exit(sdio_uart_exit);
  1045. MODULE_AUTHOR("Nicolas Pitre");
  1046. MODULE_LICENSE("GPL");