sdio_uart.c 30 KB

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