sdio_uart.c 30 KB

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