sa1100_ir.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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/config.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/types.h>
  25. #include <linux/init.h>
  26. #include <linux/errno.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/slab.h>
  29. #include <linux/rtnetlink.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/delay.h>
  32. #include <linux/device.h>
  33. #include <linux/dma-mapping.h>
  34. #include <net/irda/irda.h>
  35. #include <net/irda/wrapper.h>
  36. #include <net/irda/irda_device.h>
  37. #include <asm/irq.h>
  38. #include <asm/dma.h>
  39. #include <asm/hardware.h>
  40. #include <asm/mach/irda.h>
  41. static int power_level = 3;
  42. static int tx_lpm;
  43. static int max_rate = 4000000;
  44. struct sa1100_irda {
  45. unsigned char hscr0;
  46. unsigned char utcr4;
  47. unsigned char power;
  48. unsigned char open;
  49. int speed;
  50. int newspeed;
  51. struct sk_buff *txskb;
  52. struct sk_buff *rxskb;
  53. dma_addr_t txbuf_dma;
  54. dma_addr_t rxbuf_dma;
  55. dma_regs_t *txdma;
  56. dma_regs_t *rxdma;
  57. struct net_device_stats stats;
  58. struct device *dev;
  59. struct irda_platform_data *pdata;
  60. struct irlap_cb *irlap;
  61. struct qos_info qos;
  62. iobuff_t tx_buff;
  63. iobuff_t rx_buff;
  64. };
  65. #define IS_FIR(si) ((si)->speed >= 4000000)
  66. #define HPSIR_MAX_RXLEN 2047
  67. /*
  68. * Allocate and map the receive buffer, unless it is already allocated.
  69. */
  70. static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
  71. {
  72. if (si->rxskb)
  73. return 0;
  74. si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
  75. if (!si->rxskb) {
  76. printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
  77. return -ENOMEM;
  78. }
  79. /*
  80. * Align any IP headers that may be contained
  81. * within the frame.
  82. */
  83. skb_reserve(si->rxskb, 1);
  84. si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
  85. HPSIR_MAX_RXLEN,
  86. DMA_FROM_DEVICE);
  87. return 0;
  88. }
  89. /*
  90. * We want to get here as soon as possible, and get the receiver setup.
  91. * We use the existing buffer.
  92. */
  93. static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
  94. {
  95. if (!si->rxskb) {
  96. printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
  97. return;
  98. }
  99. /*
  100. * First empty receive FIFO
  101. */
  102. Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
  103. /*
  104. * Enable the DMA, receiver and receive interrupt.
  105. */
  106. sa1100_clear_dma(si->rxdma);
  107. sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN);
  108. Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
  109. }
  110. /*
  111. * Set the IrDA communications speed.
  112. */
  113. static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
  114. {
  115. unsigned long flags;
  116. int brd, ret = -EINVAL;
  117. switch (speed) {
  118. case 9600: case 19200: case 38400:
  119. case 57600: case 115200:
  120. brd = 3686400 / (16 * speed) - 1;
  121. /*
  122. * Stop the receive DMA.
  123. */
  124. if (IS_FIR(si))
  125. sa1100_stop_dma(si->rxdma);
  126. local_irq_save(flags);
  127. Ser2UTCR3 = 0;
  128. Ser2HSCR0 = HSCR0_UART;
  129. Ser2UTCR1 = brd >> 8;
  130. Ser2UTCR2 = brd;
  131. /*
  132. * Clear status register
  133. */
  134. Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
  135. Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
  136. if (si->pdata->set_speed)
  137. si->pdata->set_speed(si->dev, speed);
  138. si->speed = speed;
  139. local_irq_restore(flags);
  140. ret = 0;
  141. break;
  142. case 4000000:
  143. local_irq_save(flags);
  144. si->hscr0 = 0;
  145. Ser2HSSR0 = 0xff;
  146. Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
  147. Ser2UTCR3 = 0;
  148. si->speed = speed;
  149. if (si->pdata->set_speed)
  150. si->pdata->set_speed(si->dev, speed);
  151. sa1100_irda_rx_alloc(si);
  152. sa1100_irda_rx_dma_start(si);
  153. local_irq_restore(flags);
  154. break;
  155. default:
  156. break;
  157. }
  158. return ret;
  159. }
  160. /*
  161. * Control the power state of the IrDA transmitter.
  162. * State:
  163. * 0 - off
  164. * 1 - short range, lowest power
  165. * 2 - medium range, medium power
  166. * 3 - maximum range, high power
  167. *
  168. * Currently, only assabet is known to support this.
  169. */
  170. static int
  171. __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
  172. {
  173. int ret = 0;
  174. if (si->pdata->set_power)
  175. ret = si->pdata->set_power(si->dev, state);
  176. return ret;
  177. }
  178. static inline int
  179. sa1100_set_power(struct sa1100_irda *si, unsigned int state)
  180. {
  181. int ret;
  182. ret = __sa1100_irda_set_power(si, state);
  183. if (ret == 0)
  184. si->power = state;
  185. return ret;
  186. }
  187. static int sa1100_irda_startup(struct sa1100_irda *si)
  188. {
  189. int ret;
  190. /*
  191. * Ensure that the ports for this device are setup correctly.
  192. */
  193. if (si->pdata->startup)
  194. si->pdata->startup(si->dev);
  195. /*
  196. * Configure PPC for IRDA - we want to drive TXD2 low.
  197. * We also want to drive this pin low during sleep.
  198. */
  199. PPSR &= ~PPC_TXD2;
  200. PSDR &= ~PPC_TXD2;
  201. PPDR |= PPC_TXD2;
  202. /*
  203. * Enable HP-SIR modulation, and ensure that the port is disabled.
  204. */
  205. Ser2UTCR3 = 0;
  206. Ser2HSCR0 = HSCR0_UART;
  207. Ser2UTCR4 = si->utcr4;
  208. Ser2UTCR0 = UTCR0_8BitData;
  209. Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
  210. /*
  211. * Clear status register
  212. */
  213. Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
  214. ret = sa1100_irda_set_speed(si, si->speed = 9600);
  215. if (ret) {
  216. Ser2UTCR3 = 0;
  217. Ser2HSCR0 = 0;
  218. if (si->pdata->shutdown)
  219. si->pdata->shutdown(si->dev);
  220. }
  221. return ret;
  222. }
  223. static void sa1100_irda_shutdown(struct sa1100_irda *si)
  224. {
  225. /*
  226. * Stop all DMA activity.
  227. */
  228. sa1100_stop_dma(si->rxdma);
  229. sa1100_stop_dma(si->txdma);
  230. /* Disable the port. */
  231. Ser2UTCR3 = 0;
  232. Ser2HSCR0 = 0;
  233. if (si->pdata->shutdown)
  234. si->pdata->shutdown(si->dev);
  235. }
  236. #ifdef CONFIG_PM
  237. /*
  238. * Suspend the IrDA interface.
  239. */
  240. static int sa1100_irda_suspend(struct device *_dev, pm_message_t state, u32 level)
  241. {
  242. struct net_device *dev = dev_get_drvdata(_dev);
  243. struct sa1100_irda *si;
  244. if (!dev || level != SUSPEND_DISABLE)
  245. return 0;
  246. si = dev->priv;
  247. if (si->open) {
  248. /*
  249. * Stop the transmit queue
  250. */
  251. netif_device_detach(dev);
  252. disable_irq(dev->irq);
  253. sa1100_irda_shutdown(si);
  254. __sa1100_irda_set_power(si, 0);
  255. }
  256. return 0;
  257. }
  258. /*
  259. * Resume the IrDA interface.
  260. */
  261. static int sa1100_irda_resume(struct device *_dev, u32 level)
  262. {
  263. struct net_device *dev = dev_get_drvdata(_dev);
  264. struct sa1100_irda *si;
  265. if (!dev || level != RESUME_ENABLE)
  266. return 0;
  267. si = dev->priv;
  268. if (si->open) {
  269. /*
  270. * If we missed a speed change, initialise at the new speed
  271. * directly. It is debatable whether this is actually
  272. * required, but in the interests of continuing from where
  273. * we left off it is desireable. The converse argument is
  274. * that we should re-negotiate at 9600 baud again.
  275. */
  276. if (si->newspeed) {
  277. si->speed = si->newspeed;
  278. si->newspeed = 0;
  279. }
  280. sa1100_irda_startup(si);
  281. __sa1100_irda_set_power(si, si->power);
  282. enable_irq(dev->irq);
  283. /*
  284. * This automatically wakes up the queue
  285. */
  286. netif_device_attach(dev);
  287. }
  288. return 0;
  289. }
  290. #else
  291. #define sa1100_irda_suspend NULL
  292. #define sa1100_irda_resume NULL
  293. #endif
  294. /*
  295. * HP-SIR format interrupt service routines.
  296. */
  297. static void sa1100_irda_hpsir_irq(struct net_device *dev)
  298. {
  299. struct sa1100_irda *si = dev->priv;
  300. int status;
  301. status = Ser2UTSR0;
  302. /*
  303. * Deal with any receive errors first. The bytes in error may be
  304. * the only bytes in the receive FIFO, so we do this first.
  305. */
  306. while (status & UTSR0_EIF) {
  307. int stat, data;
  308. stat = Ser2UTSR1;
  309. data = Ser2UTDR;
  310. if (stat & (UTSR1_FRE | UTSR1_ROR)) {
  311. si->stats.rx_errors++;
  312. if (stat & UTSR1_FRE)
  313. si->stats.rx_frame_errors++;
  314. if (stat & UTSR1_ROR)
  315. si->stats.rx_fifo_errors++;
  316. } else
  317. async_unwrap_char(dev, &si->stats, &si->rx_buff, data);
  318. status = Ser2UTSR0;
  319. }
  320. /*
  321. * We must clear certain bits.
  322. */
  323. Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
  324. if (status & UTSR0_RFS) {
  325. /*
  326. * There are at least 4 bytes in the FIFO. Read 3 bytes
  327. * and leave the rest to the block below.
  328. */
  329. async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
  330. async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
  331. async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
  332. }
  333. if (status & (UTSR0_RFS | UTSR0_RID)) {
  334. /*
  335. * Fifo contains more than 1 character.
  336. */
  337. do {
  338. async_unwrap_char(dev, &si->stats, &si->rx_buff,
  339. Ser2UTDR);
  340. } while (Ser2UTSR1 & UTSR1_RNE);
  341. dev->last_rx = jiffies;
  342. }
  343. if (status & UTSR0_TFS && si->tx_buff.len) {
  344. /*
  345. * Transmitter FIFO is not full
  346. */
  347. do {
  348. Ser2UTDR = *si->tx_buff.data++;
  349. si->tx_buff.len -= 1;
  350. } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
  351. if (si->tx_buff.len == 0) {
  352. si->stats.tx_packets++;
  353. si->stats.tx_bytes += si->tx_buff.data -
  354. si->tx_buff.head;
  355. /*
  356. * We need to ensure that the transmitter has
  357. * finished.
  358. */
  359. do
  360. rmb();
  361. while (Ser2UTSR1 & UTSR1_TBY);
  362. /*
  363. * Ok, we've finished transmitting. Now enable
  364. * the receiver. Sometimes we get a receive IRQ
  365. * immediately after a transmit...
  366. */
  367. Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
  368. Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
  369. if (si->newspeed) {
  370. sa1100_irda_set_speed(si, si->newspeed);
  371. si->newspeed = 0;
  372. }
  373. /* I'm hungry! */
  374. netif_wake_queue(dev);
  375. }
  376. }
  377. }
  378. static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
  379. {
  380. struct sk_buff *skb = si->rxskb;
  381. dma_addr_t dma_addr;
  382. unsigned int len, stat, data;
  383. if (!skb) {
  384. printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
  385. return;
  386. }
  387. /*
  388. * Get the current data position.
  389. */
  390. dma_addr = sa1100_get_dma_pos(si->rxdma);
  391. len = dma_addr - si->rxbuf_dma;
  392. if (len > HPSIR_MAX_RXLEN)
  393. len = HPSIR_MAX_RXLEN;
  394. dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE);
  395. do {
  396. /*
  397. * Read Status, and then Data.
  398. */
  399. stat = Ser2HSSR1;
  400. rmb();
  401. data = Ser2HSDR;
  402. if (stat & (HSSR1_CRE | HSSR1_ROR)) {
  403. si->stats.rx_errors++;
  404. if (stat & HSSR1_CRE)
  405. si->stats.rx_crc_errors++;
  406. if (stat & HSSR1_ROR)
  407. si->stats.rx_frame_errors++;
  408. } else
  409. skb->data[len++] = data;
  410. /*
  411. * If we hit the end of frame, there's
  412. * no point in continuing.
  413. */
  414. if (stat & HSSR1_EOF)
  415. break;
  416. } while (Ser2HSSR0 & HSSR0_EIF);
  417. if (stat & HSSR1_EOF) {
  418. si->rxskb = NULL;
  419. skb_put(skb, len);
  420. skb->dev = dev;
  421. skb->mac.raw = skb->data;
  422. skb->protocol = htons(ETH_P_IRDA);
  423. si->stats.rx_packets++;
  424. si->stats.rx_bytes += len;
  425. /*
  426. * Before we pass the buffer up, allocate a new one.
  427. */
  428. sa1100_irda_rx_alloc(si);
  429. netif_rx(skb);
  430. dev->last_rx = jiffies;
  431. } else {
  432. /*
  433. * Remap the buffer.
  434. */
  435. si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
  436. HPSIR_MAX_RXLEN,
  437. DMA_FROM_DEVICE);
  438. }
  439. }
  440. /*
  441. * FIR format interrupt service routine. We only have to
  442. * handle RX events; transmit events go via the TX DMA handler.
  443. *
  444. * No matter what, we disable RX, process, and the restart RX.
  445. */
  446. static void sa1100_irda_fir_irq(struct net_device *dev)
  447. {
  448. struct sa1100_irda *si = dev->priv;
  449. /*
  450. * Stop RX DMA
  451. */
  452. sa1100_stop_dma(si->rxdma);
  453. /*
  454. * Framing error - we throw away the packet completely.
  455. * Clearing RXE flushes the error conditions and data
  456. * from the fifo.
  457. */
  458. if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
  459. si->stats.rx_errors++;
  460. if (Ser2HSSR0 & HSSR0_FRE)
  461. si->stats.rx_frame_errors++;
  462. /*
  463. * Clear out the DMA...
  464. */
  465. Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
  466. /*
  467. * Clear selected status bits now, so we
  468. * don't miss them next time around.
  469. */
  470. Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
  471. }
  472. /*
  473. * Deal with any receive errors. The any of the lowest
  474. * 8 bytes in the FIFO may contain an error. We must read
  475. * them one by one. The "error" could even be the end of
  476. * packet!
  477. */
  478. if (Ser2HSSR0 & HSSR0_EIF)
  479. sa1100_irda_fir_error(si, dev);
  480. /*
  481. * No matter what happens, we must restart reception.
  482. */
  483. sa1100_irda_rx_dma_start(si);
  484. }
  485. static irqreturn_t sa1100_irda_irq(int irq, void *dev_id, struct pt_regs *regs)
  486. {
  487. struct net_device *dev = dev_id;
  488. if (IS_FIR(((struct sa1100_irda *)dev->priv)))
  489. sa1100_irda_fir_irq(dev);
  490. else
  491. sa1100_irda_hpsir_irq(dev);
  492. return IRQ_HANDLED;
  493. }
  494. /*
  495. * TX DMA completion handler.
  496. */
  497. static void sa1100_irda_txdma_irq(void *id)
  498. {
  499. struct net_device *dev = id;
  500. struct sa1100_irda *si = dev->priv;
  501. struct sk_buff *skb = si->txskb;
  502. si->txskb = NULL;
  503. /*
  504. * Wait for the transmission to complete. Unfortunately,
  505. * the hardware doesn't give us an interrupt to indicate
  506. * "end of frame".
  507. */
  508. do
  509. rmb();
  510. while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
  511. /*
  512. * Clear the transmit underrun bit.
  513. */
  514. Ser2HSSR0 = HSSR0_TUR;
  515. /*
  516. * Do we need to change speed? Note that we're lazy
  517. * here - we don't free the old rxskb. We don't need
  518. * to allocate a buffer either.
  519. */
  520. if (si->newspeed) {
  521. sa1100_irda_set_speed(si, si->newspeed);
  522. si->newspeed = 0;
  523. }
  524. /*
  525. * Start reception. This disables the transmitter for
  526. * us. This will be using the existing RX buffer.
  527. */
  528. sa1100_irda_rx_dma_start(si);
  529. /*
  530. * Account and free the packet.
  531. */
  532. if (skb) {
  533. dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
  534. si->stats.tx_packets ++;
  535. si->stats.tx_bytes += skb->len;
  536. dev_kfree_skb_irq(skb);
  537. }
  538. /*
  539. * Make sure that the TX queue is available for sending
  540. * (for retries). TX has priority over RX at all times.
  541. */
  542. netif_wake_queue(dev);
  543. }
  544. static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  545. {
  546. struct sa1100_irda *si = dev->priv;
  547. int speed = irda_get_next_speed(skb);
  548. /*
  549. * Does this packet contain a request to change the interface
  550. * speed? If so, remember it until we complete the transmission
  551. * of this frame.
  552. */
  553. if (speed != si->speed && speed != -1)
  554. si->newspeed = speed;
  555. /*
  556. * If this is an empty frame, we can bypass a lot.
  557. */
  558. if (skb->len == 0) {
  559. if (si->newspeed) {
  560. si->newspeed = 0;
  561. sa1100_irda_set_speed(si, speed);
  562. }
  563. dev_kfree_skb(skb);
  564. return 0;
  565. }
  566. if (!IS_FIR(si)) {
  567. netif_stop_queue(dev);
  568. si->tx_buff.data = si->tx_buff.head;
  569. si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
  570. si->tx_buff.truesize);
  571. /*
  572. * Set the transmit interrupt enable. This will fire
  573. * off an interrupt immediately. Note that we disable
  574. * the receiver so we won't get spurious characteres
  575. * received.
  576. */
  577. Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
  578. dev_kfree_skb(skb);
  579. } else {
  580. int mtt = irda_get_mtt(skb);
  581. /*
  582. * We must not be transmitting...
  583. */
  584. if (si->txskb)
  585. BUG();
  586. netif_stop_queue(dev);
  587. si->txskb = skb;
  588. si->txbuf_dma = dma_map_single(si->dev, skb->data,
  589. skb->len, DMA_TO_DEVICE);
  590. sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
  591. /*
  592. * If we have a mean turn-around time, impose the specified
  593. * specified delay. We could shorten this by timing from
  594. * the point we received the packet.
  595. */
  596. if (mtt)
  597. udelay(mtt);
  598. Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
  599. }
  600. dev->trans_start = jiffies;
  601. return 0;
  602. }
  603. static int
  604. sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  605. {
  606. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  607. struct sa1100_irda *si = dev->priv;
  608. int ret = -EOPNOTSUPP;
  609. switch (cmd) {
  610. case SIOCSBANDWIDTH:
  611. if (capable(CAP_NET_ADMIN)) {
  612. /*
  613. * We are unable to set the speed if the
  614. * device is not running.
  615. */
  616. if (si->open) {
  617. ret = sa1100_irda_set_speed(si,
  618. rq->ifr_baudrate);
  619. } else {
  620. printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
  621. ret = 0;
  622. }
  623. }
  624. break;
  625. case SIOCSMEDIABUSY:
  626. ret = -EPERM;
  627. if (capable(CAP_NET_ADMIN)) {
  628. irda_device_set_media_busy(dev, TRUE);
  629. ret = 0;
  630. }
  631. break;
  632. case SIOCGRECEIVING:
  633. rq->ifr_receiving = IS_FIR(si) ? 0
  634. : si->rx_buff.state != OUTSIDE_FRAME;
  635. break;
  636. default:
  637. break;
  638. }
  639. return ret;
  640. }
  641. static struct net_device_stats *sa1100_irda_stats(struct net_device *dev)
  642. {
  643. struct sa1100_irda *si = dev->priv;
  644. return &si->stats;
  645. }
  646. static int sa1100_irda_start(struct net_device *dev)
  647. {
  648. struct sa1100_irda *si = dev->priv;
  649. int err;
  650. si->speed = 9600;
  651. err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
  652. if (err)
  653. goto err_irq;
  654. err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
  655. NULL, NULL, &si->rxdma);
  656. if (err)
  657. goto err_rx_dma;
  658. err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
  659. sa1100_irda_txdma_irq, dev, &si->txdma);
  660. if (err)
  661. goto err_tx_dma;
  662. /*
  663. * The interrupt must remain disabled for now.
  664. */
  665. disable_irq(dev->irq);
  666. /*
  667. * Setup the serial port for the specified speed.
  668. */
  669. err = sa1100_irda_startup(si);
  670. if (err)
  671. goto err_startup;
  672. /*
  673. * Open a new IrLAP layer instance.
  674. */
  675. si->irlap = irlap_open(dev, &si->qos, "sa1100");
  676. err = -ENOMEM;
  677. if (!si->irlap)
  678. goto err_irlap;
  679. /*
  680. * Now enable the interrupt and start the queue
  681. */
  682. si->open = 1;
  683. sa1100_set_power(si, power_level); /* low power mode */
  684. enable_irq(dev->irq);
  685. netif_start_queue(dev);
  686. return 0;
  687. err_irlap:
  688. si->open = 0;
  689. sa1100_irda_shutdown(si);
  690. err_startup:
  691. sa1100_free_dma(si->txdma);
  692. err_tx_dma:
  693. sa1100_free_dma(si->rxdma);
  694. err_rx_dma:
  695. free_irq(dev->irq, dev);
  696. err_irq:
  697. return err;
  698. }
  699. static int sa1100_irda_stop(struct net_device *dev)
  700. {
  701. struct sa1100_irda *si = dev->priv;
  702. disable_irq(dev->irq);
  703. sa1100_irda_shutdown(si);
  704. /*
  705. * If we have been doing DMA receive, make sure we
  706. * tidy that up cleanly.
  707. */
  708. if (si->rxskb) {
  709. dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
  710. DMA_FROM_DEVICE);
  711. dev_kfree_skb(si->rxskb);
  712. si->rxskb = NULL;
  713. }
  714. /* Stop IrLAP */
  715. if (si->irlap) {
  716. irlap_close(si->irlap);
  717. si->irlap = NULL;
  718. }
  719. netif_stop_queue(dev);
  720. si->open = 0;
  721. /*
  722. * Free resources
  723. */
  724. sa1100_free_dma(si->txdma);
  725. sa1100_free_dma(si->rxdma);
  726. free_irq(dev->irq, dev);
  727. sa1100_set_power(si, 0);
  728. return 0;
  729. }
  730. static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
  731. {
  732. io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
  733. if (io->head != NULL) {
  734. io->truesize = size;
  735. io->in_frame = FALSE;
  736. io->state = OUTSIDE_FRAME;
  737. io->data = io->head;
  738. }
  739. return io->head ? 0 : -ENOMEM;
  740. }
  741. static int sa1100_irda_probe(struct device *_dev)
  742. {
  743. struct platform_device *pdev = to_platform_device(_dev);
  744. struct net_device *dev;
  745. struct sa1100_irda *si;
  746. unsigned int baudrate_mask;
  747. int err;
  748. if (!pdev->dev.platform_data)
  749. return -EINVAL;
  750. err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
  751. if (err)
  752. goto err_mem_1;
  753. err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
  754. if (err)
  755. goto err_mem_2;
  756. err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
  757. if (err)
  758. goto err_mem_3;
  759. dev = alloc_irdadev(sizeof(struct sa1100_irda));
  760. if (!dev)
  761. goto err_mem_4;
  762. si = dev->priv;
  763. si->dev = &pdev->dev;
  764. si->pdata = pdev->dev.platform_data;
  765. /*
  766. * Initialise the HP-SIR buffers
  767. */
  768. err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
  769. if (err)
  770. goto err_mem_5;
  771. err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
  772. if (err)
  773. goto err_mem_5;
  774. dev->hard_start_xmit = sa1100_irda_hard_xmit;
  775. dev->open = sa1100_irda_start;
  776. dev->stop = sa1100_irda_stop;
  777. dev->do_ioctl = sa1100_irda_ioctl;
  778. dev->get_stats = sa1100_irda_stats;
  779. dev->irq = IRQ_Ser2ICP;
  780. irda_init_max_qos_capabilies(&si->qos);
  781. /*
  782. * We support original IRDA up to 115k2. (we don't currently
  783. * support 4Mbps). Min Turn Time set to 1ms or greater.
  784. */
  785. baudrate_mask = IR_9600;
  786. switch (max_rate) {
  787. case 4000000: baudrate_mask |= IR_4000000 << 8;
  788. case 115200: baudrate_mask |= IR_115200;
  789. case 57600: baudrate_mask |= IR_57600;
  790. case 38400: baudrate_mask |= IR_38400;
  791. case 19200: baudrate_mask |= IR_19200;
  792. }
  793. si->qos.baud_rate.bits &= baudrate_mask;
  794. si->qos.min_turn_time.bits = 7;
  795. irda_qos_bits_to_value(&si->qos);
  796. si->utcr4 = UTCR4_HPSIR;
  797. if (tx_lpm)
  798. si->utcr4 |= UTCR4_Z1_6us;
  799. /*
  800. * Initially enable HP-SIR modulation, and ensure that the port
  801. * is disabled.
  802. */
  803. Ser2UTCR3 = 0;
  804. Ser2UTCR4 = si->utcr4;
  805. Ser2HSCR0 = HSCR0_UART;
  806. err = register_netdev(dev);
  807. if (err == 0)
  808. dev_set_drvdata(&pdev->dev, dev);
  809. if (err) {
  810. err_mem_5:
  811. kfree(si->tx_buff.head);
  812. kfree(si->rx_buff.head);
  813. free_netdev(dev);
  814. err_mem_4:
  815. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  816. err_mem_3:
  817. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  818. err_mem_2:
  819. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  820. }
  821. err_mem_1:
  822. return err;
  823. }
  824. static int sa1100_irda_remove(struct device *_dev)
  825. {
  826. struct net_device *dev = dev_get_drvdata(_dev);
  827. if (dev) {
  828. struct sa1100_irda *si = dev->priv;
  829. unregister_netdev(dev);
  830. kfree(si->tx_buff.head);
  831. kfree(si->rx_buff.head);
  832. free_netdev(dev);
  833. }
  834. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  835. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  836. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  837. return 0;
  838. }
  839. static struct device_driver sa1100ir_driver = {
  840. .name = "sa11x0-ir",
  841. .bus = &platform_bus_type,
  842. .probe = sa1100_irda_probe,
  843. .remove = sa1100_irda_remove,
  844. .suspend = sa1100_irda_suspend,
  845. .resume = sa1100_irda_resume,
  846. };
  847. static int __init sa1100_irda_init(void)
  848. {
  849. /*
  850. * Limit power level a sensible range.
  851. */
  852. if (power_level < 1)
  853. power_level = 1;
  854. if (power_level > 3)
  855. power_level = 3;
  856. return driver_register(&sa1100ir_driver);
  857. }
  858. static void __exit sa1100_irda_exit(void)
  859. {
  860. driver_unregister(&sa1100ir_driver);
  861. }
  862. module_init(sa1100_irda_init);
  863. module_exit(sa1100_irda_exit);
  864. module_param(power_level, int, 0);
  865. module_param(tx_lpm, int, 0);
  866. module_param(max_rate, int, 0);
  867. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  868. MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
  869. MODULE_LICENSE("GPL");
  870. MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
  871. MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
  872. MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");