sa1100_ir.c 22 KB

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