Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 *  linux/drivers/net/irda/sa1100_ir.c
   3 *
   4 *  Copyright (C) 2000-2001 Russell King
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 *
  10 *  Infra-red driver for the StrongARM SA1100 embedded microprocessor
  11 *
  12 *  Note that we don't have to worry about the SA1111's DMA bugs in here,
  13 *  so we use the straight forward dma_map_* functions with a null pointer.
  14 *
  15 *  This driver takes one kernel command line parameter, sa1100ir=, with
  16 *  the following options:
  17 *	max_rate:baudrate	- set the maximum baud rate
  18 *	power_leve:level	- set the transmitter power level
  19 *	tx_lpm:0|1		- set transmit low power mode
  20 */
  21#include <linux/module.h>
  22#include <linux/moduleparam.h>
  23#include <linux/types.h>
  24#include <linux/init.h>
  25#include <linux/errno.h>
  26#include <linux/netdevice.h>
  27#include <linux/slab.h>
  28#include <linux/rtnetlink.h>
  29#include <linux/interrupt.h>
  30#include <linux/delay.h>
  31#include <linux/platform_device.h>
  32#include <linux/dma-mapping.h>
 
 
  33
  34#include <net/irda/irda.h>
  35#include <net/irda/wrapper.h>
  36#include <net/irda/irda_device.h>
  37
  38#include <asm/irq.h>
  39#include <mach/dma.h>
  40#include <mach/hardware.h>
  41#include <asm/mach/irda.h>
  42
  43static int power_level = 3;
  44static int tx_lpm;
  45static int max_rate = 4000000;
  46
 
 
 
 
 
 
 
 
  47struct sa1100_irda {
  48	unsigned char		hscr0;
  49	unsigned char		utcr4;
  50	unsigned char		power;
  51	unsigned char		open;
  52
  53	int			speed;
  54	int			newspeed;
  55
  56	struct sk_buff		*txskb;
  57	struct sk_buff		*rxskb;
  58	dma_addr_t		txbuf_dma;
  59	dma_addr_t		rxbuf_dma;
  60	dma_regs_t		*txdma;
  61	dma_regs_t		*rxdma;
  62
  63	struct device		*dev;
  64	struct irda_platform_data *pdata;
  65	struct irlap_cb		*irlap;
  66	struct qos_info		qos;
  67
  68	iobuff_t		tx_buff;
  69	iobuff_t		rx_buff;
 
 
 
  70};
  71
 
 
  72#define IS_FIR(si)		((si)->speed >= 4000000)
  73
  74#define HPSIR_MAX_RXLEN		2047
  75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  76/*
  77 * Allocate and map the receive buffer, unless it is already allocated.
  78 */
  79static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
  80{
  81	if (si->rxskb)
  82		return 0;
  83
  84	si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
  85
  86	if (!si->rxskb) {
  87		printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
  88		return -ENOMEM;
  89	}
  90
  91	/*
  92	 * Align any IP headers that may be contained
  93	 * within the frame.
  94	 */
  95	skb_reserve(si->rxskb, 1);
 
 
 
 
 
 
  96
  97	si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
  98					HPSIR_MAX_RXLEN,
  99					DMA_FROM_DEVICE);
 100	return 0;
 101}
 102
 103/*
 104 * We want to get here as soon as possible, and get the receiver setup.
 105 * We use the existing buffer.
 106 */
 107static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
 108{
 109	if (!si->rxskb) {
 110		printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
 111		return;
 112	}
 113
 114	/*
 115	 * First empty receive FIFO
 116	 */
 117	Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
 118
 119	/*
 120	 * Enable the DMA, receiver and receive interrupt.
 121	 */
 122	sa1100_clear_dma(si->rxdma);
 123	sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN);
 124	Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
 
 125}
 126
 127/*
 128 * Set the IrDA communications speed.
 129 */
 130static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
 131{
 132	unsigned long flags;
 133	int brd, ret = -EINVAL;
 134
 135	switch (speed) {
 136	case 9600:	case 19200:	case 38400:
 137	case 57600:	case 115200:
 138		brd = 3686400 / (16 * speed) - 1;
 139
 140		/*
 141		 * Stop the receive DMA.
 142		 */
 143		if (IS_FIR(si))
 144			sa1100_stop_dma(si->rxdma);
 145
 146		local_irq_save(flags);
 147
 148		Ser2UTCR3 = 0;
 149		Ser2HSCR0 = HSCR0_UART;
 150
 151		Ser2UTCR1 = brd >> 8;
 152		Ser2UTCR2 = brd;
 153
 154		/*
 155		 * Clear status register
 156		 */
 157		Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
 158		Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
 159
 160		if (si->pdata->set_speed)
 161			si->pdata->set_speed(si->dev, speed);
 162
 163		si->speed = speed;
 164
 165		local_irq_restore(flags);
 166		ret = 0;
 167		break;
 168
 169	case 4000000:
 170		local_irq_save(flags);
 171
 172		si->hscr0 = 0;
 173
 174		Ser2HSSR0 = 0xff;
 175		Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
 176		Ser2UTCR3 = 0;
 177
 178		si->speed = speed;
 179
 180		if (si->pdata->set_speed)
 181			si->pdata->set_speed(si->dev, speed);
 182
 183		sa1100_irda_rx_alloc(si);
 184		sa1100_irda_rx_dma_start(si);
 185
 186		local_irq_restore(flags);
 187
 188		break;
 189
 190	default:
 191		break;
 192	}
 193
 194	return ret;
 195}
 196
 197/*
 198 * Control the power state of the IrDA transmitter.
 199 * State:
 200 *  0 - off
 201 *  1 - short range, lowest power
 202 *  2 - medium range, medium power
 203 *  3 - maximum range, high power
 204 *
 205 * Currently, only assabet is known to support this.
 206 */
 207static int
 208__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
 209{
 210	int ret = 0;
 211	if (si->pdata->set_power)
 212		ret = si->pdata->set_power(si->dev, state);
 213	return ret;
 214}
 215
 216static inline int
 217sa1100_set_power(struct sa1100_irda *si, unsigned int state)
 218{
 219	int ret;
 220
 221	ret = __sa1100_irda_set_power(si, state);
 222	if (ret == 0)
 223		si->power = state;
 224
 225	return ret;
 226}
 227
 228static int sa1100_irda_startup(struct sa1100_irda *si)
 229{
 230	int ret;
 231
 232	/*
 233	 * Ensure that the ports for this device are setup correctly.
 234	 */
 235	if (si->pdata->startup)	{
 236		ret = si->pdata->startup(si->dev);
 237		if (ret)
 238			return ret;
 239	}
 240
 241	/*
 242	 * Configure PPC for IRDA - we want to drive TXD2 low.
 243	 * We also want to drive this pin low during sleep.
 244	 */
 245	PPSR &= ~PPC_TXD2;
 246	PSDR &= ~PPC_TXD2;
 247	PPDR |= PPC_TXD2;
 248
 249	/*
 250	 * Enable HP-SIR modulation, and ensure that the port is disabled.
 251	 */
 252	Ser2UTCR3 = 0;
 253	Ser2HSCR0 = HSCR0_UART;
 254	Ser2UTCR4 = si->utcr4;
 255	Ser2UTCR0 = UTCR0_8BitData;
 256	Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
 257
 258	/*
 259	 * Clear status register
 
 260	 */
 261	Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
 
 262
 263	ret = sa1100_irda_set_speed(si, si->speed = 9600);
 264	if (ret) {
 265		Ser2UTCR3 = 0;
 266		Ser2HSCR0 = 0;
 267
 268		if (si->pdata->shutdown)
 269			si->pdata->shutdown(si->dev);
 270	}
 271
 272	return ret;
 273}
 274
 275static void sa1100_irda_shutdown(struct sa1100_irda *si)
 276{
 277	/*
 278	 * Stop all DMA activity.
 279	 */
 280	sa1100_stop_dma(si->rxdma);
 281	sa1100_stop_dma(si->txdma);
 282
 283	/* Disable the port. */
 284	Ser2UTCR3 = 0;
 285	Ser2HSCR0 = 0;
 286
 287	if (si->pdata->shutdown)
 288		si->pdata->shutdown(si->dev);
 289}
 290
 291#ifdef CONFIG_PM
 292/*
 293 * Suspend the IrDA interface.
 294 */
 295static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
 296{
 297	struct net_device *dev = platform_get_drvdata(pdev);
 298	struct sa1100_irda *si;
 299
 300	if (!dev)
 301		return 0;
 302
 303	si = netdev_priv(dev);
 304	if (si->open) {
 305		/*
 306		 * Stop the transmit queue
 307		 */
 308		netif_device_detach(dev);
 309		disable_irq(dev->irq);
 310		sa1100_irda_shutdown(si);
 311		__sa1100_irda_set_power(si, 0);
 312	}
 313
 314	return 0;
 315}
 316
 317/*
 318 * Resume the IrDA interface.
 319 */
 320static int sa1100_irda_resume(struct platform_device *pdev)
 321{
 322	struct net_device *dev = platform_get_drvdata(pdev);
 323	struct sa1100_irda *si;
 324
 325	if (!dev)
 326		return 0;
 327
 328	si = netdev_priv(dev);
 329	if (si->open) {
 330		/*
 331		 * If we missed a speed change, initialise at the new speed
 332		 * directly.  It is debatable whether this is actually
 333		 * required, but in the interests of continuing from where
 334		 * we left off it is desirable.  The converse argument is
 335		 * that we should re-negotiate at 9600 baud again.
 336		 */
 337		if (si->newspeed) {
 338			si->speed = si->newspeed;
 339			si->newspeed = 0;
 340		}
 341
 342		sa1100_irda_startup(si);
 343		__sa1100_irda_set_power(si, si->power);
 344		enable_irq(dev->irq);
 345
 346		/*
 347		 * This automatically wakes up the queue
 348		 */
 349		netif_device_attach(dev);
 350	}
 351
 352	return 0;
 353}
 354#else
 355#define sa1100_irda_suspend	NULL
 356#define sa1100_irda_resume	NULL
 357#endif
 358
 359/*
 360 * HP-SIR format interrupt service routines.
 361 */
 362static void sa1100_irda_hpsir_irq(struct net_device *dev)
 363{
 364	struct sa1100_irda *si = netdev_priv(dev);
 365	int status;
 366
 367	status = Ser2UTSR0;
 368
 369	/*
 370	 * Deal with any receive errors first.  The bytes in error may be
 371	 * the only bytes in the receive FIFO, so we do this first.
 372	 */
 373	while (status & UTSR0_EIF) {
 374		int stat, data;
 375
 376		stat = Ser2UTSR1;
 377		data = Ser2UTDR;
 378
 379		if (stat & (UTSR1_FRE | UTSR1_ROR)) {
 380			dev->stats.rx_errors++;
 381			if (stat & UTSR1_FRE)
 382				dev->stats.rx_frame_errors++;
 383			if (stat & UTSR1_ROR)
 384				dev->stats.rx_fifo_errors++;
 385		} else
 386			async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
 387
 388		status = Ser2UTSR0;
 389	}
 390
 391	/*
 392	 * We must clear certain bits.
 393	 */
 394	Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
 395
 396	if (status & UTSR0_RFS) {
 397		/*
 398		 * There are at least 4 bytes in the FIFO.  Read 3 bytes
 399		 * and leave the rest to the block below.
 400		 */
 401		async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
 402		async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
 403		async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
 404	}
 405
 406	if (status & (UTSR0_RFS | UTSR0_RID)) {
 407		/*
 408		 * Fifo contains more than 1 character.
 409		 */
 410		do {
 411			async_unwrap_char(dev, &dev->stats, &si->rx_buff,
 412					  Ser2UTDR);
 413		} while (Ser2UTSR1 & UTSR1_RNE);
 414
 415	}
 416
 417	if (status & UTSR0_TFS && si->tx_buff.len) {
 418		/*
 419		 * Transmitter FIFO is not full
 420		 */
 421		do {
 422			Ser2UTDR = *si->tx_buff.data++;
 423			si->tx_buff.len -= 1;
 424		} while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
 425
 426		if (si->tx_buff.len == 0) {
 427			dev->stats.tx_packets++;
 428			dev->stats.tx_bytes += si->tx_buff.data -
 429					      si->tx_buff.head;
 430
 431			/*
 432			 * We need to ensure that the transmitter has
 433			 * finished.
 434			 */
 435			do
 436				rmb();
 437			while (Ser2UTSR1 & UTSR1_TBY);
 
 438
 439			/*
 440			 * Ok, we've finished transmitting.  Now enable
 441			 * the receiver.  Sometimes we get a receive IRQ
 442			 * immediately after a transmit...
 443			 */
 444			Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
 445			Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
 
 446
 447			if (si->newspeed) {
 448				sa1100_irda_set_speed(si, si->newspeed);
 449				si->newspeed = 0;
 450			}
 451
 452			/* I'm hungry! */
 453			netif_wake_queue(dev);
 454		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 455	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 456}
 457
 458static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
 459{
 460	struct sk_buff *skb = si->rxskb;
 461	dma_addr_t dma_addr;
 462	unsigned int len, stat, data;
 463
 464	if (!skb) {
 465		printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
 466		return;
 467	}
 468
 469	/*
 470	 * Get the current data position.
 471	 */
 472	dma_addr = sa1100_get_dma_pos(si->rxdma);
 473	len = dma_addr - si->rxbuf_dma;
 474	if (len > HPSIR_MAX_RXLEN)
 475		len = HPSIR_MAX_RXLEN;
 476	dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE);
 477
 478	do {
 479		/*
 480		 * Read Status, and then Data.
 481		 */
 482		stat = Ser2HSSR1;
 483		rmb();
 484		data = Ser2HSDR;
 485
 486		if (stat & (HSSR1_CRE | HSSR1_ROR)) {
 487			dev->stats.rx_errors++;
 488			if (stat & HSSR1_CRE)
 489				dev->stats.rx_crc_errors++;
 490			if (stat & HSSR1_ROR)
 491				dev->stats.rx_frame_errors++;
 492		} else
 493			skb->data[len++] = data;
 494
 495		/*
 496		 * If we hit the end of frame, there's
 497		 * no point in continuing.
 498		 */
 499		if (stat & HSSR1_EOF)
 500			break;
 501	} while (Ser2HSSR0 & HSSR0_EIF);
 502
 503	if (stat & HSSR1_EOF) {
 504		si->rxskb = NULL;
 505
 506		skb_put(skb, len);
 507		skb->dev = dev;
 508		skb_reset_mac_header(skb);
 509		skb->protocol = htons(ETH_P_IRDA);
 510		dev->stats.rx_packets++;
 511		dev->stats.rx_bytes += len;
 512
 513		/*
 514		 * Before we pass the buffer up, allocate a new one.
 515		 */
 516		sa1100_irda_rx_alloc(si);
 517
 518		netif_rx(skb);
 519	} else {
 520		/*
 521		 * Remap the buffer.
 
 522		 */
 523		si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
 524						HPSIR_MAX_RXLEN,
 525						DMA_FROM_DEVICE);
 526	}
 527}
 528
 529/*
 530 * FIR format interrupt service routine.  We only have to
 531 * handle RX events; transmit events go via the TX DMA handler.
 532 *
 533 * No matter what, we disable RX, process, and the restart RX.
 534 */
 535static void sa1100_irda_fir_irq(struct net_device *dev)
 536{
 537	struct sa1100_irda *si = netdev_priv(dev);
 538
 539	/*
 540	 * Stop RX DMA
 541	 */
 542	sa1100_stop_dma(si->rxdma);
 543
 544	/*
 545	 * Framing error - we throw away the packet completely.
 546	 * Clearing RXE flushes the error conditions and data
 547	 * from the fifo.
 548	 */
 549	if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
 550		dev->stats.rx_errors++;
 551
 552		if (Ser2HSSR0 & HSSR0_FRE)
 553			dev->stats.rx_frame_errors++;
 554
 555		/*
 556		 * Clear out the DMA...
 557		 */
 558		Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
 559
 560		/*
 561		 * Clear selected status bits now, so we
 562		 * don't miss them next time around.
 563		 */
 564		Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
 565	}
 566
 567	/*
 568	 * Deal with any receive errors.  The any of the lowest
 569	 * 8 bytes in the FIFO may contain an error.  We must read
 570	 * them one by one.  The "error" could even be the end of
 571	 * packet!
 572	 */
 573	if (Ser2HSSR0 & HSSR0_EIF)
 574		sa1100_irda_fir_error(si, dev);
 575
 576	/*
 577	 * No matter what happens, we must restart reception.
 578	 */
 579	sa1100_irda_rx_dma_start(si);
 580}
 581
 582static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
 583{
 584	struct net_device *dev = dev_id;
 585	if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
 586		sa1100_irda_fir_irq(dev);
 587	else
 588		sa1100_irda_hpsir_irq(dev);
 589	return IRQ_HANDLED;
 590}
 591
 592/*
 593 * TX DMA completion handler.
 594 */
 595static void sa1100_irda_txdma_irq(void *id)
 596{
 597	struct net_device *dev = id;
 598	struct sa1100_irda *si = netdev_priv(dev);
 599	struct sk_buff *skb = si->txskb;
 600
 601	si->txskb = NULL;
 
 
 
 602
 603	/*
 604	 * Wait for the transmission to complete.  Unfortunately,
 605	 * the hardware doesn't give us an interrupt to indicate
 606	 * "end of frame".
 607	 */
 608	do
 609		rmb();
 610	while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
 611
 612	/*
 613	 * Clear the transmit underrun bit.
 614	 */
 615	Ser2HSSR0 = HSSR0_TUR;
 616
 617	/*
 618	 * Do we need to change speed?  Note that we're lazy
 619	 * here - we don't free the old rxskb.  We don't need
 620	 * to allocate a buffer either.
 621	 */
 622	if (si->newspeed) {
 623		sa1100_irda_set_speed(si, si->newspeed);
 624		si->newspeed = 0;
 625	}
 626
 627	/*
 628	 * Start reception.  This disables the transmitter for
 629	 * us.  This will be using the existing RX buffer.
 630	 */
 631	sa1100_irda_rx_dma_start(si);
 632
 633	/*
 634	 * Account and free the packet.
 635	 */
 636	if (skb) {
 637		dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
 638		dev->stats.tx_packets ++;
 639		dev->stats.tx_bytes += skb->len;
 640		dev_kfree_skb_irq(skb);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 641	}
 642
 643	/*
 644	 * Make sure that the TX queue is available for sending
 645	 * (for retries).  TX has priority over RX at all times.
 646	 */
 647	netif_wake_queue(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 648}
 649
 650static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
 651{
 652	struct sa1100_irda *si = netdev_priv(dev);
 653	int speed = irda_get_next_speed(skb);
 654
 655	/*
 656	 * Does this packet contain a request to change the interface
 657	 * speed?  If so, remember it until we complete the transmission
 658	 * of this frame.
 659	 */
 660	if (speed != si->speed && speed != -1)
 661		si->newspeed = speed;
 662
 663	/*
 664	 * If this is an empty frame, we can bypass a lot.
 665	 */
 666	if (skb->len == 0) {
 667		if (si->newspeed) {
 668			si->newspeed = 0;
 669			sa1100_irda_set_speed(si, speed);
 670		}
 671		dev_kfree_skb(skb);
 672		return NETDEV_TX_OK;
 673	}
 674
 675	if (!IS_FIR(si)) {
 676		netif_stop_queue(dev);
 677
 678		si->tx_buff.data = si->tx_buff.head;
 679		si->tx_buff.len  = async_wrap_skb(skb, si->tx_buff.data,
 680						  si->tx_buff.truesize);
 681
 682		/*
 683		 * Set the transmit interrupt enable.  This will fire
 684		 * off an interrupt immediately.  Note that we disable
 685		 * the receiver so we won't get spurious characteres
 686		 * received.
 687		 */
 688		Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
 689
 690		dev_kfree_skb(skb);
 691	} else {
 692		int mtt = irda_get_mtt(skb);
 693
 694		/*
 695		 * We must not be transmitting...
 696		 */
 697		BUG_ON(si->txskb);
 698
 699		netif_stop_queue(dev);
 700
 701		si->txskb = skb;
 702		si->txbuf_dma = dma_map_single(si->dev, skb->data,
 703					 skb->len, DMA_TO_DEVICE);
 704
 705		sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
 706
 707		/*
 708		 * If we have a mean turn-around time, impose the specified
 709		 * specified delay.  We could shorten this by timing from
 710		 * the point we received the packet.
 711		 */
 712		if (mtt)
 713			udelay(mtt);
 714
 715		Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
 716	}
 717
 718	return NETDEV_TX_OK;
 719}
 720
 721static int
 722sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
 723{
 724	struct if_irda_req *rq = (struct if_irda_req *)ifreq;
 725	struct sa1100_irda *si = netdev_priv(dev);
 726	int ret = -EOPNOTSUPP;
 727
 728	switch (cmd) {
 729	case SIOCSBANDWIDTH:
 730		if (capable(CAP_NET_ADMIN)) {
 731			/*
 732			 * We are unable to set the speed if the
 733			 * device is not running.
 734			 */
 735			if (si->open) {
 736				ret = sa1100_irda_set_speed(si,
 737						rq->ifr_baudrate);
 738			} else {
 739				printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
 740				ret = 0;
 741			}
 742		}
 743		break;
 744
 745	case SIOCSMEDIABUSY:
 746		ret = -EPERM;
 747		if (capable(CAP_NET_ADMIN)) {
 748			irda_device_set_media_busy(dev, TRUE);
 749			ret = 0;
 750		}
 751		break;
 752
 753	case SIOCGRECEIVING:
 754		rq->ifr_receiving = IS_FIR(si) ? 0
 755					: si->rx_buff.state != OUTSIDE_FRAME;
 756		break;
 757
 758	default:
 759		break;
 760	}
 761		
 762	return ret;
 763}
 764
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 765static int sa1100_irda_start(struct net_device *dev)
 766{
 767	struct sa1100_irda *si = netdev_priv(dev);
 768	int err;
 769
 770	si->speed = 9600;
 771
 772	err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
 773	if (err)
 774		goto err_irq;
 775
 776	err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
 777				 NULL, NULL, &si->rxdma);
 778	if (err)
 779		goto err_rx_dma;
 780
 781	err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
 782				 sa1100_irda_txdma_irq, dev, &si->txdma);
 783	if (err)
 784		goto err_tx_dma;
 785
 786	/*
 787	 * The interrupt must remain disabled for now.
 788	 */
 789	disable_irq(dev->irq);
 790
 791	/*
 792	 * Setup the serial port for the specified speed.
 793	 */
 794	err = sa1100_irda_startup(si);
 795	if (err)
 796		goto err_startup;
 797
 798	/*
 799	 * Open a new IrLAP layer instance.
 800	 */
 801	si->irlap = irlap_open(dev, &si->qos, "sa1100");
 802	err = -ENOMEM;
 803	if (!si->irlap)
 804		goto err_irlap;
 805
 
 
 
 
 806	/*
 807	 * Now enable the interrupt and start the queue
 808	 */
 809	si->open = 1;
 810	sa1100_set_power(si, power_level); /* low power mode */
 811	enable_irq(dev->irq);
 812	netif_start_queue(dev);
 813	return 0;
 814
 
 
 815err_irlap:
 816	si->open = 0;
 817	sa1100_irda_shutdown(si);
 818err_startup:
 819	sa1100_free_dma(si->txdma);
 820err_tx_dma:
 821	sa1100_free_dma(si->rxdma);
 822err_rx_dma:
 823	free_irq(dev->irq, dev);
 824err_irq:
 825	return err;
 826}
 827
 828static int sa1100_irda_stop(struct net_device *dev)
 829{
 830	struct sa1100_irda *si = netdev_priv(dev);
 
 831
 832	disable_irq(dev->irq);
 
 
 833	sa1100_irda_shutdown(si);
 834
 835	/*
 836	 * If we have been doing DMA receive, make sure we
 837	 * tidy that up cleanly.
 838	 */
 839	if (si->rxskb) {
 840		dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
 841				 DMA_FROM_DEVICE);
 842		dev_kfree_skb(si->rxskb);
 843		si->rxskb = NULL;
 
 
 
 
 
 
 
 
 
 844	}
 845
 846	/* Stop IrLAP */
 847	if (si->irlap) {
 848		irlap_close(si->irlap);
 849		si->irlap = NULL;
 850	}
 851
 852	netif_stop_queue(dev);
 853	si->open = 0;
 854
 855	/*
 856	 * Free resources
 857	 */
 858	sa1100_free_dma(si->txdma);
 859	sa1100_free_dma(si->rxdma);
 860	free_irq(dev->irq, dev);
 861
 862	sa1100_set_power(si, 0);
 863
 864	return 0;
 865}
 866
 867static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
 868{
 869	io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
 870	if (io->head != NULL) {
 871		io->truesize = size;
 872		io->in_frame = FALSE;
 873		io->state    = OUTSIDE_FRAME;
 874		io->data     = io->head;
 875	}
 876	return io->head ? 0 : -ENOMEM;
 877}
 878
 879static const struct net_device_ops sa1100_irda_netdev_ops = {
 880	.ndo_open		= sa1100_irda_start,
 881	.ndo_stop		= sa1100_irda_stop,
 882	.ndo_start_xmit		= sa1100_irda_hard_xmit,
 883	.ndo_do_ioctl		= sa1100_irda_ioctl,
 884};
 885
 886static int sa1100_irda_probe(struct platform_device *pdev)
 887{
 888	struct net_device *dev;
 889	struct sa1100_irda *si;
 890	unsigned int baudrate_mask;
 891	int err;
 892
 893	if (!pdev->dev.platform_data)
 894		return -EINVAL;
 895
 
 
 
 
 896	err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
 897	if (err)
 898		goto err_mem_1;
 899	err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
 900	if (err)
 901		goto err_mem_2;
 902	err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
 903	if (err)
 904		goto err_mem_3;
 905
 906	dev = alloc_irdadev(sizeof(struct sa1100_irda));
 907	if (!dev)
 
 908		goto err_mem_4;
 
 
 
 909
 910	si = netdev_priv(dev);
 911	si->dev = &pdev->dev;
 912	si->pdata = pdev->dev.platform_data;
 913
 
 
 
 914	/*
 915	 * Initialise the HP-SIR buffers
 916	 */
 917	err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
 918	if (err)
 919		goto err_mem_5;
 920	err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
 921	if (err)
 922		goto err_mem_5;
 923
 924	dev->netdev_ops	= &sa1100_irda_netdev_ops;
 925	dev->irq	= IRQ_Ser2ICP;
 926
 927	irda_init_max_qos_capabilies(&si->qos);
 928
 929	/*
 930	 * We support original IRDA up to 115k2. (we don't currently
 931	 * support 4Mbps).  Min Turn Time set to 1ms or greater.
 932	 */
 933	baudrate_mask = IR_9600;
 934
 935	switch (max_rate) {
 936	case 4000000:		baudrate_mask |= IR_4000000 << 8;
 937	case 115200:		baudrate_mask |= IR_115200;
 938	case 57600:		baudrate_mask |= IR_57600;
 939	case 38400:		baudrate_mask |= IR_38400;
 940	case 19200:		baudrate_mask |= IR_19200;
 941	}
 942		
 943	si->qos.baud_rate.bits &= baudrate_mask;
 944	si->qos.min_turn_time.bits = 7;
 945
 946	irda_qos_bits_to_value(&si->qos);
 947
 948	si->utcr4 = UTCR4_HPSIR;
 949	if (tx_lpm)
 950		si->utcr4 |= UTCR4_Z1_6us;
 951
 952	/*
 953	 * Initially enable HP-SIR modulation, and ensure that the port
 954	 * is disabled.
 955	 */
 956	Ser2UTCR3 = 0;
 957	Ser2UTCR4 = si->utcr4;
 958	Ser2HSCR0 = HSCR0_UART;
 959
 960	err = register_netdev(dev);
 961	if (err == 0)
 962		platform_set_drvdata(pdev, dev);
 963
 964	if (err) {
 965 err_mem_5:
 966		kfree(si->tx_buff.head);
 967		kfree(si->rx_buff.head);
 968		free_netdev(dev);
 969 err_mem_4:
 970		release_mem_region(__PREG(Ser2HSCR2), 0x04);
 971 err_mem_3:
 972		release_mem_region(__PREG(Ser2HSCR0), 0x1c);
 973 err_mem_2:
 974		release_mem_region(__PREG(Ser2UTCR0), 0x24);
 975	}
 976 err_mem_1:
 977	return err;
 978}
 979
 980static int sa1100_irda_remove(struct platform_device *pdev)
 981{
 982	struct net_device *dev = platform_get_drvdata(pdev);
 983
 984	if (dev) {
 985		struct sa1100_irda *si = netdev_priv(dev);
 986		unregister_netdev(dev);
 987		kfree(si->tx_buff.head);
 988		kfree(si->rx_buff.head);
 989		free_netdev(dev);
 990	}
 991
 992	release_mem_region(__PREG(Ser2HSCR2), 0x04);
 993	release_mem_region(__PREG(Ser2HSCR0), 0x1c);
 994	release_mem_region(__PREG(Ser2UTCR0), 0x24);
 995
 996	return 0;
 997}
 998
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 999static struct platform_driver sa1100ir_driver = {
1000	.probe		= sa1100_irda_probe,
1001	.remove		= sa1100_irda_remove,
1002	.suspend	= sa1100_irda_suspend,
1003	.resume		= sa1100_irda_resume,
1004	.driver		= {
1005		.name	= "sa11x0-ir",
1006		.owner	= THIS_MODULE,
1007	},
1008};
1009
1010static int __init sa1100_irda_init(void)
1011{
1012	/*
1013	 * Limit power level a sensible range.
1014	 */
1015	if (power_level < 1)
1016		power_level = 1;
1017	if (power_level > 3)
1018		power_level = 3;
1019
1020	return platform_driver_register(&sa1100ir_driver);
1021}
1022
1023static void __exit sa1100_irda_exit(void)
1024{
1025	platform_driver_unregister(&sa1100ir_driver);
1026}
1027
1028module_init(sa1100_irda_init);
1029module_exit(sa1100_irda_exit);
1030module_param(power_level, int, 0);
1031module_param(tx_lpm, int, 0);
1032module_param(max_rate, int, 0);
1033
1034MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1035MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1036MODULE_LICENSE("GPL");
1037MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1038MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1039MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
1040MODULE_ALIAS("platform:sa11x0-ir");
v4.6
   1/*
   2 *  linux/drivers/net/irda/sa1100_ir.c
   3 *
   4 *  Copyright (C) 2000-2001 Russell King
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 *
  10 *  Infra-red driver for the StrongARM SA1100 embedded microprocessor
  11 *
  12 *  Note that we don't have to worry about the SA1111's DMA bugs in here,
  13 *  so we use the straight forward dma_map_* functions with a null pointer.
  14 *
  15 *  This driver takes one kernel command line parameter, sa1100ir=, with
  16 *  the following options:
  17 *	max_rate:baudrate	- set the maximum baud rate
  18 *	power_level:level	- set the transmitter power level
  19 *	tx_lpm:0|1		- set transmit low power mode
  20 */
  21#include <linux/module.h>
  22#include <linux/moduleparam.h>
  23#include <linux/types.h>
  24#include <linux/init.h>
  25#include <linux/errno.h>
  26#include <linux/netdevice.h>
  27#include <linux/slab.h>
  28#include <linux/rtnetlink.h>
  29#include <linux/interrupt.h>
  30#include <linux/delay.h>
  31#include <linux/platform_device.h>
  32#include <linux/dma-mapping.h>
  33#include <linux/dmaengine.h>
  34#include <linux/sa11x0-dma.h>
  35
  36#include <net/irda/irda.h>
  37#include <net/irda/wrapper.h>
  38#include <net/irda/irda_device.h>
  39
 
 
  40#include <mach/hardware.h>
  41#include <linux/platform_data/irda-sa11x0.h>
  42
  43static int power_level = 3;
  44static int tx_lpm;
  45static int max_rate = 4000000;
  46
  47struct sa1100_buf {
  48	struct device		*dev;
  49	struct sk_buff		*skb;
  50	struct scatterlist	sg;
  51	struct dma_chan		*chan;
  52	dma_cookie_t		cookie;
  53};
  54
  55struct sa1100_irda {
 
  56	unsigned char		utcr4;
  57	unsigned char		power;
  58	unsigned char		open;
  59
  60	int			speed;
  61	int			newspeed;
  62
  63	struct sa1100_buf	dma_rx;
  64	struct sa1100_buf	dma_tx;
 
 
 
 
  65
  66	struct device		*dev;
  67	struct irda_platform_data *pdata;
  68	struct irlap_cb		*irlap;
  69	struct qos_info		qos;
  70
  71	iobuff_t		tx_buff;
  72	iobuff_t		rx_buff;
  73
  74	int (*tx_start)(struct sk_buff *, struct net_device *, struct sa1100_irda *);
  75	irqreturn_t (*irq)(struct net_device *, struct sa1100_irda *);
  76};
  77
  78static int sa1100_irda_set_speed(struct sa1100_irda *, int);
  79
  80#define IS_FIR(si)		((si)->speed >= 4000000)
  81
  82#define HPSIR_MAX_RXLEN		2047
  83
  84static struct dma_slave_config sa1100_irda_sir_tx = {
  85	.direction	= DMA_TO_DEVICE,
  86	.dst_addr	= __PREG(Ser2UTDR),
  87	.dst_addr_width	= DMA_SLAVE_BUSWIDTH_1_BYTE,
  88	.dst_maxburst	= 4,
  89};
  90
  91static struct dma_slave_config sa1100_irda_fir_rx = {
  92	.direction	= DMA_FROM_DEVICE,
  93	.src_addr	= __PREG(Ser2HSDR),
  94	.src_addr_width	= DMA_SLAVE_BUSWIDTH_1_BYTE,
  95	.src_maxburst	= 8,
  96};
  97
  98static struct dma_slave_config sa1100_irda_fir_tx = {
  99	.direction	= DMA_TO_DEVICE,
 100	.dst_addr	= __PREG(Ser2HSDR),
 101	.dst_addr_width	= DMA_SLAVE_BUSWIDTH_1_BYTE,
 102	.dst_maxburst	= 8,
 103};
 104
 105static unsigned sa1100_irda_dma_xferred(struct sa1100_buf *buf)
 106{
 107	struct dma_chan *chan = buf->chan;
 108	struct dma_tx_state state;
 109	enum dma_status status;
 110
 111	status = chan->device->device_tx_status(chan, buf->cookie, &state);
 112	if (status != DMA_PAUSED)
 113		return 0;
 114
 115	return sg_dma_len(&buf->sg) - state.residue;
 116}
 117
 118static int sa1100_irda_dma_request(struct device *dev, struct sa1100_buf *buf,
 119	const char *name, struct dma_slave_config *cfg)
 120{
 121	dma_cap_mask_t m;
 122	int ret;
 123
 124	dma_cap_zero(m);
 125	dma_cap_set(DMA_SLAVE, m);
 126
 127	buf->chan = dma_request_channel(m, sa11x0_dma_filter_fn, (void *)name);
 128	if (!buf->chan) {
 129		dev_err(dev, "unable to request DMA channel for %s\n",
 130			name);
 131		return -ENOENT;
 132	}
 133
 134	ret = dmaengine_slave_config(buf->chan, cfg);
 135	if (ret)
 136		dev_warn(dev, "DMA slave_config for %s returned %d\n",
 137			name, ret);
 138
 139	buf->dev = buf->chan->device->dev;
 140
 141	return 0;
 142}
 143
 144static void sa1100_irda_dma_start(struct sa1100_buf *buf,
 145	enum dma_transfer_direction dir, dma_async_tx_callback cb, void *cb_p)
 146{
 147	struct dma_async_tx_descriptor *desc;
 148	struct dma_chan *chan = buf->chan;
 149
 150	desc = dmaengine_prep_slave_sg(chan, &buf->sg, 1, dir,
 151			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 152	if (desc) {
 153		desc->callback = cb;
 154		desc->callback_param = cb_p;
 155		buf->cookie = dmaengine_submit(desc);
 156		dma_async_issue_pending(chan);
 157	}
 158}
 159
 160/*
 161 * Allocate and map the receive buffer, unless it is already allocated.
 162 */
 163static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
 164{
 165	if (si->dma_rx.skb)
 166		return 0;
 167
 168	si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
 169	if (!si->dma_rx.skb) {
 
 170		printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
 171		return -ENOMEM;
 172	}
 173
 174	/*
 175	 * Align any IP headers that may be contained
 176	 * within the frame.
 177	 */
 178	skb_reserve(si->dma_rx.skb, 1);
 179
 180	sg_set_buf(&si->dma_rx.sg, si->dma_rx.skb->data, HPSIR_MAX_RXLEN);
 181	if (dma_map_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE) == 0) {
 182		dev_kfree_skb_any(si->dma_rx.skb);
 183		return -ENOMEM;
 184	}
 185
 
 
 
 186	return 0;
 187}
 188
 189/*
 190 * We want to get here as soon as possible, and get the receiver setup.
 191 * We use the existing buffer.
 192 */
 193static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
 194{
 195	if (!si->dma_rx.skb) {
 196		printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
 197		return;
 198	}
 199
 200	/*
 201	 * First empty receive FIFO
 202	 */
 203	Ser2HSCR0 = HSCR0_HSSP;
 204
 205	/*
 206	 * Enable the DMA, receiver and receive interrupt.
 207	 */
 208	dmaengine_terminate_all(si->dma_rx.chan);
 209	sa1100_irda_dma_start(&si->dma_rx, DMA_DEV_TO_MEM, NULL, NULL);
 210
 211	Ser2HSCR0 = HSCR0_HSSP | HSCR0_RXE;
 212}
 213
 214static void sa1100_irda_check_speed(struct sa1100_irda *si)
 
 
 
 215{
 216	if (si->newspeed) {
 217		sa1100_irda_set_speed(si, si->newspeed);
 218		si->newspeed = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 219	}
 
 
 220}
 221
 222/*
 223 * HP-SIR format support.
 
 
 
 
 
 
 
 224 */
 225static void sa1100_irda_sirtxdma_irq(void *id)
 
 226{
 227	struct net_device *dev = id;
 228	struct sa1100_irda *si = netdev_priv(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 229
 230	dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1, DMA_TO_DEVICE);
 231	dev_kfree_skb(si->dma_tx.skb);
 232	si->dma_tx.skb = NULL;
 233
 234	dev->stats.tx_packets++;
 235	dev->stats.tx_bytes += sg_dma_len(&si->dma_tx.sg);
 
 
 
 
 
 
 236
 237	/* We need to ensure that the transmitter has finished. */
 238	do
 239		rmb();
 240	while (Ser2UTSR1 & UTSR1_TBY);
 
 
 
 
 
 
 
 
 
 
 
 
 241
 242	/*
 243	 * Ok, we've finished transmitting.  Now enable the receiver.
 244	 * Sometimes we get a receive IRQ immediately after a transmit...
 245	 */
 246	Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
 247	Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
 248
 249	sa1100_irda_check_speed(si);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 250
 251	/* I'm hungry! */
 252	netif_wake_queue(dev);
 253}
 254
 255static int sa1100_irda_sir_tx_start(struct sk_buff *skb, struct net_device *dev,
 256	struct sa1100_irda *si)
 
 
 
 257{
 258	si->tx_buff.data = si->tx_buff.head;
 259	si->tx_buff.len  = async_wrap_skb(skb, si->tx_buff.data,
 260					  si->tx_buff.truesize);
 261
 262	si->dma_tx.skb = skb;
 263	sg_set_buf(&si->dma_tx.sg, si->tx_buff.data, si->tx_buff.len);
 264	if (dma_map_sg(si->dma_tx.dev, &si->dma_tx.sg, 1, DMA_TO_DEVICE) == 0) {
 265		si->dma_tx.skb = NULL;
 266		netif_wake_queue(dev);
 267		dev->stats.tx_dropped++;
 268		return NETDEV_TX_OK;
 
 
 
 
 269	}
 270
 271	sa1100_irda_dma_start(&si->dma_tx, DMA_MEM_TO_DEV, sa1100_irda_sirtxdma_irq, dev);
 
 
 
 
 
 
 
 
 
 
 
 
 272
 273	/*
 274	 * The mean turn-around time is enforced by XBOF padding,
 275	 * so we don't have to do anything special here.
 276	 */
 277	Ser2UTCR3 = UTCR3_TXE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 278
 279	return NETDEV_TX_OK;
 280}
 
 
 
 
 281
 282static irqreturn_t sa1100_irda_sir_irq(struct net_device *dev, struct sa1100_irda *si)
 
 
 
 283{
 
 284	int status;
 285
 286	status = Ser2UTSR0;
 287
 288	/*
 289	 * Deal with any receive errors first.  The bytes in error may be
 290	 * the only bytes in the receive FIFO, so we do this first.
 291	 */
 292	while (status & UTSR0_EIF) {
 293		int stat, data;
 294
 295		stat = Ser2UTSR1;
 296		data = Ser2UTDR;
 297
 298		if (stat & (UTSR1_FRE | UTSR1_ROR)) {
 299			dev->stats.rx_errors++;
 300			if (stat & UTSR1_FRE)
 301				dev->stats.rx_frame_errors++;
 302			if (stat & UTSR1_ROR)
 303				dev->stats.rx_fifo_errors++;
 304		} else
 305			async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
 306
 307		status = Ser2UTSR0;
 308	}
 309
 310	/*
 311	 * We must clear certain bits.
 312	 */
 313	Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
 314
 315	if (status & UTSR0_RFS) {
 316		/*
 317		 * There are at least 4 bytes in the FIFO.  Read 3 bytes
 318		 * and leave the rest to the block below.
 319		 */
 320		async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
 321		async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
 322		async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
 323	}
 324
 325	if (status & (UTSR0_RFS | UTSR0_RID)) {
 326		/*
 327		 * Fifo contains more than 1 character.
 328		 */
 329		do {
 330			async_unwrap_char(dev, &dev->stats, &si->rx_buff,
 331					  Ser2UTDR);
 332		} while (Ser2UTSR1 & UTSR1_RNE);
 333
 334	}
 335
 336	return IRQ_HANDLED;
 337}
 
 
 
 
 
 
 
 
 
 
 
 338
 339/*
 340 * FIR format support.
 341 */
 342static void sa1100_irda_firtxdma_irq(void *id)
 343{
 344	struct net_device *dev = id;
 345	struct sa1100_irda *si = netdev_priv(dev);
 346	struct sk_buff *skb;
 347
 348	/*
 349	 * Wait for the transmission to complete.  Unfortunately,
 350	 * the hardware doesn't give us an interrupt to indicate
 351	 * "end of frame".
 352	 */
 353	do
 354		rmb();
 355	while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
 356
 357	/*
 358	 * Clear the transmit underrun bit.
 359	 */
 360	Ser2HSSR0 = HSSR0_TUR;
 361
 362	/*
 363	 * Do we need to change speed?  Note that we're lazy
 364	 * here - we don't free the old dma_rx.skb.  We don't need
 365	 * to allocate a buffer either.
 366	 */
 367	sa1100_irda_check_speed(si);
 368
 369	/*
 370	 * Start reception.  This disables the transmitter for
 371	 * us.  This will be using the existing RX buffer.
 372	 */
 373	sa1100_irda_rx_dma_start(si);
 374
 375	/* Account and free the packet. */
 376	skb = si->dma_tx.skb;
 377	if (skb) {
 378		dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1,
 379			     DMA_TO_DEVICE);
 380		dev->stats.tx_packets ++;
 381		dev->stats.tx_bytes += skb->len;
 382		dev_kfree_skb_irq(skb);
 383		si->dma_tx.skb = NULL;
 384	}
 385
 386	/*
 387	 * Make sure that the TX queue is available for sending
 388	 * (for retries).  TX has priority over RX at all times.
 389	 */
 390	netif_wake_queue(dev);
 391}
 392
 393static int sa1100_irda_fir_tx_start(struct sk_buff *skb, struct net_device *dev,
 394	struct sa1100_irda *si)
 395{
 396	int mtt = irda_get_mtt(skb);
 397
 398	si->dma_tx.skb = skb;
 399	sg_set_buf(&si->dma_tx.sg, skb->data, skb->len);
 400	if (dma_map_sg(si->dma_tx.dev, &si->dma_tx.sg, 1, DMA_TO_DEVICE) == 0) {
 401		si->dma_tx.skb = NULL;
 402		netif_wake_queue(dev);
 403		dev->stats.tx_dropped++;
 404		dev_kfree_skb(skb);
 405		return NETDEV_TX_OK;
 406	}
 407
 408	sa1100_irda_dma_start(&si->dma_tx, DMA_MEM_TO_DEV, sa1100_irda_firtxdma_irq, dev);
 409
 410	/*
 411	 * If we have a mean turn-around time, impose the specified
 412	 * specified delay.  We could shorten this by timing from
 413	 * the point we received the packet.
 414	 */
 415	if (mtt)
 416		udelay(mtt);
 417
 418	Ser2HSCR0 = HSCR0_HSSP | HSCR0_TXE;
 419
 420	return NETDEV_TX_OK;
 421}
 422
 423static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
 424{
 425	struct sk_buff *skb = si->dma_rx.skb;
 
 426	unsigned int len, stat, data;
 427
 428	if (!skb) {
 429		printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
 430		return;
 431	}
 432
 433	/*
 434	 * Get the current data position.
 435	 */
 436	len = sa1100_irda_dma_xferred(&si->dma_rx);
 
 437	if (len > HPSIR_MAX_RXLEN)
 438		len = HPSIR_MAX_RXLEN;
 439	dma_unmap_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
 440
 441	do {
 442		/*
 443		 * Read Status, and then Data.
 444		 */
 445		stat = Ser2HSSR1;
 446		rmb();
 447		data = Ser2HSDR;
 448
 449		if (stat & (HSSR1_CRE | HSSR1_ROR)) {
 450			dev->stats.rx_errors++;
 451			if (stat & HSSR1_CRE)
 452				dev->stats.rx_crc_errors++;
 453			if (stat & HSSR1_ROR)
 454				dev->stats.rx_frame_errors++;
 455		} else
 456			skb->data[len++] = data;
 457
 458		/*
 459		 * If we hit the end of frame, there's
 460		 * no point in continuing.
 461		 */
 462		if (stat & HSSR1_EOF)
 463			break;
 464	} while (Ser2HSSR0 & HSSR0_EIF);
 465
 466	if (stat & HSSR1_EOF) {
 467		si->dma_rx.skb = NULL;
 468
 469		skb_put(skb, len);
 470		skb->dev = dev;
 471		skb_reset_mac_header(skb);
 472		skb->protocol = htons(ETH_P_IRDA);
 473		dev->stats.rx_packets++;
 474		dev->stats.rx_bytes += len;
 475
 476		/*
 477		 * Before we pass the buffer up, allocate a new one.
 478		 */
 479		sa1100_irda_rx_alloc(si);
 480
 481		netif_rx(skb);
 482	} else {
 483		/*
 484		 * Remap the buffer - it was previously mapped, and we
 485		 * hope that this succeeds.
 486		 */
 487		dma_map_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
 
 
 488	}
 489}
 490
 491/*
 492 * We only have to handle RX events here; transmit events go via the TX
 493 * DMA handler. We disable RX, process, and the restart RX.
 
 
 494 */
 495static irqreturn_t sa1100_irda_fir_irq(struct net_device *dev, struct sa1100_irda *si)
 496{
 
 
 497	/*
 498	 * Stop RX DMA
 499	 */
 500	dmaengine_pause(si->dma_rx.chan);
 501
 502	/*
 503	 * Framing error - we throw away the packet completely.
 504	 * Clearing RXE flushes the error conditions and data
 505	 * from the fifo.
 506	 */
 507	if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
 508		dev->stats.rx_errors++;
 509
 510		if (Ser2HSSR0 & HSSR0_FRE)
 511			dev->stats.rx_frame_errors++;
 512
 513		/*
 514		 * Clear out the DMA...
 515		 */
 516		Ser2HSCR0 = HSCR0_HSSP;
 517
 518		/*
 519		 * Clear selected status bits now, so we
 520		 * don't miss them next time around.
 521		 */
 522		Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
 523	}
 524
 525	/*
 526	 * Deal with any receive errors.  The any of the lowest
 527	 * 8 bytes in the FIFO may contain an error.  We must read
 528	 * them one by one.  The "error" could even be the end of
 529	 * packet!
 530	 */
 531	if (Ser2HSSR0 & HSSR0_EIF)
 532		sa1100_irda_fir_error(si, dev);
 533
 534	/*
 535	 * No matter what happens, we must restart reception.
 536	 */
 537	sa1100_irda_rx_dma_start(si);
 
 538
 
 
 
 
 
 
 
 539	return IRQ_HANDLED;
 540}
 541
 542/*
 543 * Set the IrDA communications speed.
 544 */
 545static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
 546{
 547	unsigned long flags;
 548	int brd, ret = -EINVAL;
 
 549
 550	switch (speed) {
 551	case 9600:	case 19200:	case 38400:
 552	case 57600:	case 115200:
 553		brd = 3686400 / (16 * speed) - 1;
 554
 555		/* Stop the receive DMA, and configure transmit. */
 556		if (IS_FIR(si)) {
 557			dmaengine_terminate_all(si->dma_rx.chan);
 558			dmaengine_slave_config(si->dma_tx.chan,
 559						&sa1100_irda_sir_tx);
 560		}
 
 
 561
 562		local_irq_save(flags);
 
 
 
 563
 564		Ser2UTCR3 = 0;
 565		Ser2HSCR0 = HSCR0_UART;
 
 
 
 
 
 
 
 566
 567		Ser2UTCR1 = brd >> 8;
 568		Ser2UTCR2 = brd;
 
 
 
 569
 570		/*
 571		 * Clear status register
 572		 */
 573		Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
 574		Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
 575
 576		if (si->pdata->set_speed)
 577			si->pdata->set_speed(si->dev, speed);
 578
 579		si->speed = speed;
 580		si->tx_start = sa1100_irda_sir_tx_start;
 581		si->irq = sa1100_irda_sir_irq;
 582
 583		local_irq_restore(flags);
 584		ret = 0;
 585		break;
 586
 587	case 4000000:
 588		if (!IS_FIR(si))
 589			dmaengine_slave_config(si->dma_tx.chan,
 590						&sa1100_irda_fir_tx);
 591
 592		local_irq_save(flags);
 593
 594		Ser2HSSR0 = 0xff;
 595		Ser2HSCR0 = HSCR0_HSSP;
 596		Ser2UTCR3 = 0;
 597
 598		si->speed = speed;
 599		si->tx_start = sa1100_irda_fir_tx_start;
 600		si->irq = sa1100_irda_fir_irq;
 601
 602		if (si->pdata->set_speed)
 603			si->pdata->set_speed(si->dev, speed);
 604
 605		sa1100_irda_rx_alloc(si);
 606		sa1100_irda_rx_dma_start(si);
 607
 608		local_irq_restore(flags);
 609
 610		break;
 611
 612	default:
 613		break;
 614	}
 615
 616	return ret;
 617}
 618
 619/*
 620 * Control the power state of the IrDA transmitter.
 621 * State:
 622 *  0 - off
 623 *  1 - short range, lowest power
 624 *  2 - medium range, medium power
 625 *  3 - maximum range, high power
 626 *
 627 * Currently, only assabet is known to support this.
 628 */
 629static int
 630__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
 631{
 632	int ret = 0;
 633	if (si->pdata->set_power)
 634		ret = si->pdata->set_power(si->dev, state);
 635	return ret;
 636}
 637
 638static inline int
 639sa1100_set_power(struct sa1100_irda *si, unsigned int state)
 640{
 641	int ret;
 642
 643	ret = __sa1100_irda_set_power(si, state);
 644	if (ret == 0)
 645		si->power = state;
 646
 647	return ret;
 648}
 649
 650static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
 651{
 652	struct net_device *dev = dev_id;
 653	struct sa1100_irda *si = netdev_priv(dev);
 654
 655	return si->irq(dev, si);
 656}
 657
 658static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
 659{
 660	struct sa1100_irda *si = netdev_priv(dev);
 661	int speed = irda_get_next_speed(skb);
 662
 663	/*
 664	 * Does this packet contain a request to change the interface
 665	 * speed?  If so, remember it until we complete the transmission
 666	 * of this frame.
 667	 */
 668	if (speed != si->speed && speed != -1)
 669		si->newspeed = speed;
 670
 671	/* If this is an empty frame, we can bypass a lot. */
 
 
 672	if (skb->len == 0) {
 673		sa1100_irda_check_speed(si);
 
 
 
 674		dev_kfree_skb(skb);
 675		return NETDEV_TX_OK;
 676	}
 677
 678	netif_stop_queue(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 679
 680	/* We must not already have a skb to transmit... */
 681	BUG_ON(si->dma_tx.skb);
 682
 683	return si->tx_start(skb, dev, si);
 684}
 685
 686static int
 687sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
 688{
 689	struct if_irda_req *rq = (struct if_irda_req *)ifreq;
 690	struct sa1100_irda *si = netdev_priv(dev);
 691	int ret = -EOPNOTSUPP;
 692
 693	switch (cmd) {
 694	case SIOCSBANDWIDTH:
 695		if (capable(CAP_NET_ADMIN)) {
 696			/*
 697			 * We are unable to set the speed if the
 698			 * device is not running.
 699			 */
 700			if (si->open) {
 701				ret = sa1100_irda_set_speed(si,
 702						rq->ifr_baudrate);
 703			} else {
 704				printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
 705				ret = 0;
 706			}
 707		}
 708		break;
 709
 710	case SIOCSMEDIABUSY:
 711		ret = -EPERM;
 712		if (capable(CAP_NET_ADMIN)) {
 713			irda_device_set_media_busy(dev, TRUE);
 714			ret = 0;
 715		}
 716		break;
 717
 718	case SIOCGRECEIVING:
 719		rq->ifr_receiving = IS_FIR(si) ? 0
 720					: si->rx_buff.state != OUTSIDE_FRAME;
 721		break;
 722
 723	default:
 724		break;
 725	}
 726		
 727	return ret;
 728}
 729
 730static int sa1100_irda_startup(struct sa1100_irda *si)
 731{
 732	int ret;
 733
 734	/*
 735	 * Ensure that the ports for this device are setup correctly.
 736	 */
 737	if (si->pdata->startup)	{
 738		ret = si->pdata->startup(si->dev);
 739		if (ret)
 740			return ret;
 741	}
 742
 743	/*
 744	 * Configure PPC for IRDA - we want to drive TXD2 low.
 745	 * We also want to drive this pin low during sleep.
 746	 */
 747	PPSR &= ~PPC_TXD2;
 748	PSDR &= ~PPC_TXD2;
 749	PPDR |= PPC_TXD2;
 750
 751	/*
 752	 * Enable HP-SIR modulation, and ensure that the port is disabled.
 753	 */
 754	Ser2UTCR3 = 0;
 755	Ser2HSCR0 = HSCR0_UART;
 756	Ser2UTCR4 = si->utcr4;
 757	Ser2UTCR0 = UTCR0_8BitData;
 758	Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
 759
 760	/*
 761	 * Clear status register
 762	 */
 763	Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
 764
 765	ret = sa1100_irda_set_speed(si, si->speed = 9600);
 766	if (ret) {
 767		Ser2UTCR3 = 0;
 768		Ser2HSCR0 = 0;
 769
 770		if (si->pdata->shutdown)
 771			si->pdata->shutdown(si->dev);
 772	}
 773
 774	return ret;
 775}
 776
 777static void sa1100_irda_shutdown(struct sa1100_irda *si)
 778{
 779	/*
 780	 * Stop all DMA activity.
 781	 */
 782	dmaengine_terminate_all(si->dma_rx.chan);
 783	dmaengine_terminate_all(si->dma_tx.chan);
 784
 785	/* Disable the port. */
 786	Ser2UTCR3 = 0;
 787	Ser2HSCR0 = 0;
 788
 789	if (si->pdata->shutdown)
 790		si->pdata->shutdown(si->dev);
 791}
 792
 793static int sa1100_irda_start(struct net_device *dev)
 794{
 795	struct sa1100_irda *si = netdev_priv(dev);
 796	int err;
 797
 798	si->speed = 9600;
 799
 800	err = sa1100_irda_dma_request(si->dev, &si->dma_rx, "Ser2ICPRc",
 801				&sa1100_irda_fir_rx);
 
 
 
 
 802	if (err)
 803		goto err_rx_dma;
 804
 805	err = sa1100_irda_dma_request(si->dev, &si->dma_tx, "Ser2ICPTr",
 806				&sa1100_irda_sir_tx);
 807	if (err)
 808		goto err_tx_dma;
 809
 810	/*
 
 
 
 
 
 811	 * Setup the serial port for the specified speed.
 812	 */
 813	err = sa1100_irda_startup(si);
 814	if (err)
 815		goto err_startup;
 816
 817	/*
 818	 * Open a new IrLAP layer instance.
 819	 */
 820	si->irlap = irlap_open(dev, &si->qos, "sa1100");
 821	err = -ENOMEM;
 822	if (!si->irlap)
 823		goto err_irlap;
 824
 825	err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
 826	if (err)
 827		goto err_irq;
 828
 829	/*
 830	 * Now enable the interrupt and start the queue
 831	 */
 832	si->open = 1;
 833	sa1100_set_power(si, power_level); /* low power mode */
 834
 835	netif_start_queue(dev);
 836	return 0;
 837
 838err_irq:
 839	irlap_close(si->irlap);
 840err_irlap:
 841	si->open = 0;
 842	sa1100_irda_shutdown(si);
 843err_startup:
 844	dma_release_channel(si->dma_tx.chan);
 845err_tx_dma:
 846	dma_release_channel(si->dma_rx.chan);
 847err_rx_dma:
 
 
 848	return err;
 849}
 850
 851static int sa1100_irda_stop(struct net_device *dev)
 852{
 853	struct sa1100_irda *si = netdev_priv(dev);
 854	struct sk_buff *skb;
 855
 856	netif_stop_queue(dev);
 857
 858	si->open = 0;
 859	sa1100_irda_shutdown(si);
 860
 861	/*
 862	 * If we have been doing any DMA activity, make sure we
 863	 * tidy that up cleanly.
 864	 */
 865	skb = si->dma_rx.skb;
 866	if (skb) {
 867		dma_unmap_sg(si->dma_rx.dev, &si->dma_rx.sg, 1,
 868			     DMA_FROM_DEVICE);
 869		dev_kfree_skb(skb);
 870		si->dma_rx.skb = NULL;
 871	}
 872
 873	skb = si->dma_tx.skb;
 874	if (skb) {
 875		dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1,
 876			     DMA_TO_DEVICE);
 877		dev_kfree_skb(skb);
 878		si->dma_tx.skb = NULL;
 879	}
 880
 881	/* Stop IrLAP */
 882	if (si->irlap) {
 883		irlap_close(si->irlap);
 884		si->irlap = NULL;
 885	}
 886
 
 
 
 887	/*
 888	 * Free resources
 889	 */
 890	dma_release_channel(si->dma_tx.chan);
 891	dma_release_channel(si->dma_rx.chan);
 892	free_irq(dev->irq, dev);
 893
 894	sa1100_set_power(si, 0);
 895
 896	return 0;
 897}
 898
 899static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
 900{
 901	io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
 902	if (io->head != NULL) {
 903		io->truesize = size;
 904		io->in_frame = FALSE;
 905		io->state    = OUTSIDE_FRAME;
 906		io->data     = io->head;
 907	}
 908	return io->head ? 0 : -ENOMEM;
 909}
 910
 911static const struct net_device_ops sa1100_irda_netdev_ops = {
 912	.ndo_open		= sa1100_irda_start,
 913	.ndo_stop		= sa1100_irda_stop,
 914	.ndo_start_xmit		= sa1100_irda_hard_xmit,
 915	.ndo_do_ioctl		= sa1100_irda_ioctl,
 916};
 917
 918static int sa1100_irda_probe(struct platform_device *pdev)
 919{
 920	struct net_device *dev;
 921	struct sa1100_irda *si;
 922	unsigned int baudrate_mask;
 923	int err, irq;
 924
 925	if (!pdev->dev.platform_data)
 926		return -EINVAL;
 927
 928	irq = platform_get_irq(pdev, 0);
 929	if (irq <= 0)
 930		return irq < 0 ? irq : -ENXIO;
 931
 932	err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
 933	if (err)
 934		goto err_mem_1;
 935	err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
 936	if (err)
 937		goto err_mem_2;
 938	err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
 939	if (err)
 940		goto err_mem_3;
 941
 942	dev = alloc_irdadev(sizeof(struct sa1100_irda));
 943	if (!dev) {
 944		err = -ENOMEM;
 945		goto err_mem_4;
 946	}
 947
 948	SET_NETDEV_DEV(dev, &pdev->dev);
 949
 950	si = netdev_priv(dev);
 951	si->dev = &pdev->dev;
 952	si->pdata = pdev->dev.platform_data;
 953
 954	sg_init_table(&si->dma_rx.sg, 1);
 955	sg_init_table(&si->dma_tx.sg, 1);
 956
 957	/*
 958	 * Initialise the HP-SIR buffers
 959	 */
 960	err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
 961	if (err)
 962		goto err_mem_5;
 963	err = sa1100_irda_init_iobuf(&si->tx_buff, IRDA_SIR_MAX_FRAME);
 964	if (err)
 965		goto err_mem_5;
 966
 967	dev->netdev_ops	= &sa1100_irda_netdev_ops;
 968	dev->irq	= irq;
 969
 970	irda_init_max_qos_capabilies(&si->qos);
 971
 972	/*
 973	 * We support original IRDA up to 115k2. (we don't currently
 974	 * support 4Mbps).  Min Turn Time set to 1ms or greater.
 975	 */
 976	baudrate_mask = IR_9600;
 977
 978	switch (max_rate) {
 979	case 4000000:		baudrate_mask |= IR_4000000 << 8;
 980	case 115200:		baudrate_mask |= IR_115200;
 981	case 57600:		baudrate_mask |= IR_57600;
 982	case 38400:		baudrate_mask |= IR_38400;
 983	case 19200:		baudrate_mask |= IR_19200;
 984	}
 985		
 986	si->qos.baud_rate.bits &= baudrate_mask;
 987	si->qos.min_turn_time.bits = 7;
 988
 989	irda_qos_bits_to_value(&si->qos);
 990
 991	si->utcr4 = UTCR4_HPSIR;
 992	if (tx_lpm)
 993		si->utcr4 |= UTCR4_Z1_6us;
 994
 995	/*
 996	 * Initially enable HP-SIR modulation, and ensure that the port
 997	 * is disabled.
 998	 */
 999	Ser2UTCR3 = 0;
1000	Ser2UTCR4 = si->utcr4;
1001	Ser2HSCR0 = HSCR0_UART;
1002
1003	err = register_netdev(dev);
1004	if (err == 0)
1005		platform_set_drvdata(pdev, dev);
1006
1007	if (err) {
1008 err_mem_5:
1009		kfree(si->tx_buff.head);
1010		kfree(si->rx_buff.head);
1011		free_netdev(dev);
1012 err_mem_4:
1013		release_mem_region(__PREG(Ser2HSCR2), 0x04);
1014 err_mem_3:
1015		release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1016 err_mem_2:
1017		release_mem_region(__PREG(Ser2UTCR0), 0x24);
1018	}
1019 err_mem_1:
1020	return err;
1021}
1022
1023static int sa1100_irda_remove(struct platform_device *pdev)
1024{
1025	struct net_device *dev = platform_get_drvdata(pdev);
1026
1027	if (dev) {
1028		struct sa1100_irda *si = netdev_priv(dev);
1029		unregister_netdev(dev);
1030		kfree(si->tx_buff.head);
1031		kfree(si->rx_buff.head);
1032		free_netdev(dev);
1033	}
1034
1035	release_mem_region(__PREG(Ser2HSCR2), 0x04);
1036	release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1037	release_mem_region(__PREG(Ser2UTCR0), 0x24);
1038
1039	return 0;
1040}
1041
1042#ifdef CONFIG_PM
1043/*
1044 * Suspend the IrDA interface.
1045 */
1046static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
1047{
1048	struct net_device *dev = platform_get_drvdata(pdev);
1049	struct sa1100_irda *si;
1050
1051	if (!dev)
1052		return 0;
1053
1054	si = netdev_priv(dev);
1055	if (si->open) {
1056		/*
1057		 * Stop the transmit queue
1058		 */
1059		netif_device_detach(dev);
1060		disable_irq(dev->irq);
1061		sa1100_irda_shutdown(si);
1062		__sa1100_irda_set_power(si, 0);
1063	}
1064
1065	return 0;
1066}
1067
1068/*
1069 * Resume the IrDA interface.
1070 */
1071static int sa1100_irda_resume(struct platform_device *pdev)
1072{
1073	struct net_device *dev = platform_get_drvdata(pdev);
1074	struct sa1100_irda *si;
1075
1076	if (!dev)
1077		return 0;
1078
1079	si = netdev_priv(dev);
1080	if (si->open) {
1081		/*
1082		 * If we missed a speed change, initialise at the new speed
1083		 * directly.  It is debatable whether this is actually
1084		 * required, but in the interests of continuing from where
1085		 * we left off it is desirable.  The converse argument is
1086		 * that we should re-negotiate at 9600 baud again.
1087		 */
1088		if (si->newspeed) {
1089			si->speed = si->newspeed;
1090			si->newspeed = 0;
1091		}
1092
1093		sa1100_irda_startup(si);
1094		__sa1100_irda_set_power(si, si->power);
1095		enable_irq(dev->irq);
1096
1097		/*
1098		 * This automatically wakes up the queue
1099		 */
1100		netif_device_attach(dev);
1101	}
1102
1103	return 0;
1104}
1105#else
1106#define sa1100_irda_suspend	NULL
1107#define sa1100_irda_resume	NULL
1108#endif
1109
1110static struct platform_driver sa1100ir_driver = {
1111	.probe		= sa1100_irda_probe,
1112	.remove		= sa1100_irda_remove,
1113	.suspend	= sa1100_irda_suspend,
1114	.resume		= sa1100_irda_resume,
1115	.driver		= {
1116		.name	= "sa11x0-ir",
 
1117	},
1118};
1119
1120static int __init sa1100_irda_init(void)
1121{
1122	/*
1123	 * Limit power level a sensible range.
1124	 */
1125	if (power_level < 1)
1126		power_level = 1;
1127	if (power_level > 3)
1128		power_level = 3;
1129
1130	return platform_driver_register(&sa1100ir_driver);
1131}
1132
1133static void __exit sa1100_irda_exit(void)
1134{
1135	platform_driver_unregister(&sa1100ir_driver);
1136}
1137
1138module_init(sa1100_irda_init);
1139module_exit(sa1100_irda_exit);
1140module_param(power_level, int, 0);
1141module_param(tx_lpm, int, 0);
1142module_param(max_rate, int, 0);
1143
1144MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1145MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1146MODULE_LICENSE("GPL");
1147MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1148MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1149MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
1150MODULE_ALIAS("platform:sa11x0-ir");