sa1100_ir.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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/platform_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 platform_device *pdev, pm_message_t state)
  241. {
  242. struct net_device *dev = platform_get_drvdata(pdev);
  243. struct sa1100_irda *si;
  244. if (!dev)
  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 platform_device *pdev)
  262. {
  263. struct net_device *dev = platform_get_drvdata(pdev);
  264. struct sa1100_irda *si;
  265. if (!dev)
  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. BUG_ON(si->txskb);
  585. netif_stop_queue(dev);
  586. si->txskb = skb;
  587. si->txbuf_dma = dma_map_single(si->dev, skb->data,
  588. skb->len, DMA_TO_DEVICE);
  589. sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
  590. /*
  591. * If we have a mean turn-around time, impose the specified
  592. * specified delay. We could shorten this by timing from
  593. * the point we received the packet.
  594. */
  595. if (mtt)
  596. udelay(mtt);
  597. Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
  598. }
  599. dev->trans_start = jiffies;
  600. return 0;
  601. }
  602. static int
  603. sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  604. {
  605. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  606. struct sa1100_irda *si = dev->priv;
  607. int ret = -EOPNOTSUPP;
  608. switch (cmd) {
  609. case SIOCSBANDWIDTH:
  610. if (capable(CAP_NET_ADMIN)) {
  611. /*
  612. * We are unable to set the speed if the
  613. * device is not running.
  614. */
  615. if (si->open) {
  616. ret = sa1100_irda_set_speed(si,
  617. rq->ifr_baudrate);
  618. } else {
  619. printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
  620. ret = 0;
  621. }
  622. }
  623. break;
  624. case SIOCSMEDIABUSY:
  625. ret = -EPERM;
  626. if (capable(CAP_NET_ADMIN)) {
  627. irda_device_set_media_busy(dev, TRUE);
  628. ret = 0;
  629. }
  630. break;
  631. case SIOCGRECEIVING:
  632. rq->ifr_receiving = IS_FIR(si) ? 0
  633. : si->rx_buff.state != OUTSIDE_FRAME;
  634. break;
  635. default:
  636. break;
  637. }
  638. return ret;
  639. }
  640. static struct net_device_stats *sa1100_irda_stats(struct net_device *dev)
  641. {
  642. struct sa1100_irda *si = dev->priv;
  643. return &si->stats;
  644. }
  645. static int sa1100_irda_start(struct net_device *dev)
  646. {
  647. struct sa1100_irda *si = dev->priv;
  648. int err;
  649. si->speed = 9600;
  650. err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
  651. if (err)
  652. goto err_irq;
  653. err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
  654. NULL, NULL, &si->rxdma);
  655. if (err)
  656. goto err_rx_dma;
  657. err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
  658. sa1100_irda_txdma_irq, dev, &si->txdma);
  659. if (err)
  660. goto err_tx_dma;
  661. /*
  662. * The interrupt must remain disabled for now.
  663. */
  664. disable_irq(dev->irq);
  665. /*
  666. * Setup the serial port for the specified speed.
  667. */
  668. err = sa1100_irda_startup(si);
  669. if (err)
  670. goto err_startup;
  671. /*
  672. * Open a new IrLAP layer instance.
  673. */
  674. si->irlap = irlap_open(dev, &si->qos, "sa1100");
  675. err = -ENOMEM;
  676. if (!si->irlap)
  677. goto err_irlap;
  678. /*
  679. * Now enable the interrupt and start the queue
  680. */
  681. si->open = 1;
  682. sa1100_set_power(si, power_level); /* low power mode */
  683. enable_irq(dev->irq);
  684. netif_start_queue(dev);
  685. return 0;
  686. err_irlap:
  687. si->open = 0;
  688. sa1100_irda_shutdown(si);
  689. err_startup:
  690. sa1100_free_dma(si->txdma);
  691. err_tx_dma:
  692. sa1100_free_dma(si->rxdma);
  693. err_rx_dma:
  694. free_irq(dev->irq, dev);
  695. err_irq:
  696. return err;
  697. }
  698. static int sa1100_irda_stop(struct net_device *dev)
  699. {
  700. struct sa1100_irda *si = dev->priv;
  701. disable_irq(dev->irq);
  702. sa1100_irda_shutdown(si);
  703. /*
  704. * If we have been doing DMA receive, make sure we
  705. * tidy that up cleanly.
  706. */
  707. if (si->rxskb) {
  708. dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
  709. DMA_FROM_DEVICE);
  710. dev_kfree_skb(si->rxskb);
  711. si->rxskb = NULL;
  712. }
  713. /* Stop IrLAP */
  714. if (si->irlap) {
  715. irlap_close(si->irlap);
  716. si->irlap = NULL;
  717. }
  718. netif_stop_queue(dev);
  719. si->open = 0;
  720. /*
  721. * Free resources
  722. */
  723. sa1100_free_dma(si->txdma);
  724. sa1100_free_dma(si->rxdma);
  725. free_irq(dev->irq, dev);
  726. sa1100_set_power(si, 0);
  727. return 0;
  728. }
  729. static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
  730. {
  731. io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
  732. if (io->head != NULL) {
  733. io->truesize = size;
  734. io->in_frame = FALSE;
  735. io->state = OUTSIDE_FRAME;
  736. io->data = io->head;
  737. }
  738. return io->head ? 0 : -ENOMEM;
  739. }
  740. static int sa1100_irda_probe(struct platform_device *pdev)
  741. {
  742. struct net_device *dev;
  743. struct sa1100_irda *si;
  744. unsigned int baudrate_mask;
  745. int err;
  746. if (!pdev->dev.platform_data)
  747. return -EINVAL;
  748. err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
  749. if (err)
  750. goto err_mem_1;
  751. err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
  752. if (err)
  753. goto err_mem_2;
  754. err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
  755. if (err)
  756. goto err_mem_3;
  757. dev = alloc_irdadev(sizeof(struct sa1100_irda));
  758. if (!dev)
  759. goto err_mem_4;
  760. si = dev->priv;
  761. si->dev = &pdev->dev;
  762. si->pdata = pdev->dev.platform_data;
  763. /*
  764. * Initialise the HP-SIR buffers
  765. */
  766. err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
  767. if (err)
  768. goto err_mem_5;
  769. err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
  770. if (err)
  771. goto err_mem_5;
  772. dev->hard_start_xmit = sa1100_irda_hard_xmit;
  773. dev->open = sa1100_irda_start;
  774. dev->stop = sa1100_irda_stop;
  775. dev->do_ioctl = sa1100_irda_ioctl;
  776. dev->get_stats = sa1100_irda_stats;
  777. dev->irq = IRQ_Ser2ICP;
  778. irda_init_max_qos_capabilies(&si->qos);
  779. /*
  780. * We support original IRDA up to 115k2. (we don't currently
  781. * support 4Mbps). Min Turn Time set to 1ms or greater.
  782. */
  783. baudrate_mask = IR_9600;
  784. switch (max_rate) {
  785. case 4000000: baudrate_mask |= IR_4000000 << 8;
  786. case 115200: baudrate_mask |= IR_115200;
  787. case 57600: baudrate_mask |= IR_57600;
  788. case 38400: baudrate_mask |= IR_38400;
  789. case 19200: baudrate_mask |= IR_19200;
  790. }
  791. si->qos.baud_rate.bits &= baudrate_mask;
  792. si->qos.min_turn_time.bits = 7;
  793. irda_qos_bits_to_value(&si->qos);
  794. si->utcr4 = UTCR4_HPSIR;
  795. if (tx_lpm)
  796. si->utcr4 |= UTCR4_Z1_6us;
  797. /*
  798. * Initially enable HP-SIR modulation, and ensure that the port
  799. * is disabled.
  800. */
  801. Ser2UTCR3 = 0;
  802. Ser2UTCR4 = si->utcr4;
  803. Ser2HSCR0 = HSCR0_UART;
  804. err = register_netdev(dev);
  805. if (err == 0)
  806. platform_set_drvdata(pdev, dev);
  807. if (err) {
  808. err_mem_5:
  809. kfree(si->tx_buff.head);
  810. kfree(si->rx_buff.head);
  811. free_netdev(dev);
  812. err_mem_4:
  813. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  814. err_mem_3:
  815. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  816. err_mem_2:
  817. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  818. }
  819. err_mem_1:
  820. return err;
  821. }
  822. static int sa1100_irda_remove(struct platform_device *pdev)
  823. {
  824. struct net_device *dev = platform_get_drvdata(pdev);
  825. if (dev) {
  826. struct sa1100_irda *si = dev->priv;
  827. unregister_netdev(dev);
  828. kfree(si->tx_buff.head);
  829. kfree(si->rx_buff.head);
  830. free_netdev(dev);
  831. }
  832. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  833. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  834. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  835. return 0;
  836. }
  837. static struct platform_driver sa1100ir_driver = {
  838. .probe = sa1100_irda_probe,
  839. .remove = sa1100_irda_remove,
  840. .suspend = sa1100_irda_suspend,
  841. .resume = sa1100_irda_resume,
  842. .driver = {
  843. .name = "sa11x0-ir",
  844. },
  845. };
  846. static int __init sa1100_irda_init(void)
  847. {
  848. /*
  849. * Limit power level a sensible range.
  850. */
  851. if (power_level < 1)
  852. power_level = 1;
  853. if (power_level > 3)
  854. power_level = 3;
  855. return platform_driver_register(&sa1100ir_driver);
  856. }
  857. static void __exit sa1100_irda_exit(void)
  858. {
  859. platform_driver_unregister(&sa1100ir_driver);
  860. }
  861. module_init(sa1100_irda_init);
  862. module_exit(sa1100_irda_exit);
  863. module_param(power_level, int, 0);
  864. module_param(tx_lpm, int, 0);
  865. module_param(max_rate, int, 0);
  866. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  867. MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
  868. MODULE_LICENSE("GPL");
  869. MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
  870. MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
  871. MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");