sa1100_ir.c 21 KB

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