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