sa1100_ir.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. ret = si->pdata->startup(si->dev);
  193. if (ret)
  194. return ret;
  195. }
  196. /*
  197. * Configure PPC for IRDA - we want to drive TXD2 low.
  198. * We also want to drive this pin low during sleep.
  199. */
  200. PPSR &= ~PPC_TXD2;
  201. PSDR &= ~PPC_TXD2;
  202. PPDR |= PPC_TXD2;
  203. /*
  204. * Enable HP-SIR modulation, and ensure that the port is disabled.
  205. */
  206. Ser2UTCR3 = 0;
  207. Ser2HSCR0 = HSCR0_UART;
  208. Ser2UTCR4 = si->utcr4;
  209. Ser2UTCR0 = UTCR0_8BitData;
  210. Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
  211. /*
  212. * Clear status register
  213. */
  214. Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
  215. ret = sa1100_irda_set_speed(si, si->speed = 9600);
  216. if (ret) {
  217. Ser2UTCR3 = 0;
  218. Ser2HSCR0 = 0;
  219. if (si->pdata->shutdown)
  220. si->pdata->shutdown(si->dev);
  221. }
  222. return ret;
  223. }
  224. static void sa1100_irda_shutdown(struct sa1100_irda *si)
  225. {
  226. /*
  227. * Stop all DMA activity.
  228. */
  229. sa1100_stop_dma(si->rxdma);
  230. sa1100_stop_dma(si->txdma);
  231. /* Disable the port. */
  232. Ser2UTCR3 = 0;
  233. Ser2HSCR0 = 0;
  234. if (si->pdata->shutdown)
  235. si->pdata->shutdown(si->dev);
  236. }
  237. #ifdef CONFIG_PM
  238. /*
  239. * Suspend the IrDA interface.
  240. */
  241. static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
  242. {
  243. struct net_device *dev = platform_get_drvdata(pdev);
  244. struct sa1100_irda *si;
  245. if (!dev)
  246. return 0;
  247. si = netdev_priv(dev);
  248. if (si->open) {
  249. /*
  250. * Stop the transmit queue
  251. */
  252. netif_device_detach(dev);
  253. disable_irq(dev->irq);
  254. sa1100_irda_shutdown(si);
  255. __sa1100_irda_set_power(si, 0);
  256. }
  257. return 0;
  258. }
  259. /*
  260. * Resume the IrDA interface.
  261. */
  262. static int sa1100_irda_resume(struct platform_device *pdev)
  263. {
  264. struct net_device *dev = platform_get_drvdata(pdev);
  265. struct sa1100_irda *si;
  266. if (!dev)
  267. return 0;
  268. si = netdev_priv(dev);
  269. if (si->open) {
  270. /*
  271. * If we missed a speed change, initialise at the new speed
  272. * directly. It is debatable whether this is actually
  273. * required, but in the interests of continuing from where
  274. * we left off it is desirable. The converse argument is
  275. * that we should re-negotiate at 9600 baud again.
  276. */
  277. if (si->newspeed) {
  278. si->speed = si->newspeed;
  279. si->newspeed = 0;
  280. }
  281. sa1100_irda_startup(si);
  282. __sa1100_irda_set_power(si, si->power);
  283. enable_irq(dev->irq);
  284. /*
  285. * This automatically wakes up the queue
  286. */
  287. netif_device_attach(dev);
  288. }
  289. return 0;
  290. }
  291. #else
  292. #define sa1100_irda_suspend NULL
  293. #define sa1100_irda_resume NULL
  294. #endif
  295. /*
  296. * HP-SIR format interrupt service routines.
  297. */
  298. static void sa1100_irda_hpsir_irq(struct net_device *dev)
  299. {
  300. struct sa1100_irda *si = netdev_priv(dev);
  301. int status;
  302. status = Ser2UTSR0;
  303. /*
  304. * Deal with any receive errors first. The bytes in error may be
  305. * the only bytes in the receive FIFO, so we do this first.
  306. */
  307. while (status & UTSR0_EIF) {
  308. int stat, data;
  309. stat = Ser2UTSR1;
  310. data = Ser2UTDR;
  311. if (stat & (UTSR1_FRE | UTSR1_ROR)) {
  312. dev->stats.rx_errors++;
  313. if (stat & UTSR1_FRE)
  314. dev->stats.rx_frame_errors++;
  315. if (stat & UTSR1_ROR)
  316. dev->stats.rx_fifo_errors++;
  317. } else
  318. async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
  319. status = Ser2UTSR0;
  320. }
  321. /*
  322. * We must clear certain bits.
  323. */
  324. Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
  325. if (status & UTSR0_RFS) {
  326. /*
  327. * There are at least 4 bytes in the FIFO. Read 3 bytes
  328. * and leave the rest to the block below.
  329. */
  330. async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
  331. async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
  332. async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
  333. }
  334. if (status & (UTSR0_RFS | UTSR0_RID)) {
  335. /*
  336. * Fifo contains more than 1 character.
  337. */
  338. do {
  339. async_unwrap_char(dev, &dev->stats, &si->rx_buff,
  340. Ser2UTDR);
  341. } while (Ser2UTSR1 & UTSR1_RNE);
  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. dev->stats.tx_packets++;
  353. dev->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. dev->stats.rx_errors++;
  404. if (stat & HSSR1_CRE)
  405. dev->stats.rx_crc_errors++;
  406. if (stat & HSSR1_ROR)
  407. dev->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_reset_mac_header(skb);
  422. skb->protocol = htons(ETH_P_IRDA);
  423. dev->stats.rx_packets++;
  424. dev->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. } else {
  431. /*
  432. * Remap the buffer.
  433. */
  434. si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
  435. HPSIR_MAX_RXLEN,
  436. DMA_FROM_DEVICE);
  437. }
  438. }
  439. /*
  440. * FIR format interrupt service routine. We only have to
  441. * handle RX events; transmit events go via the TX DMA handler.
  442. *
  443. * No matter what, we disable RX, process, and the restart RX.
  444. */
  445. static void sa1100_irda_fir_irq(struct net_device *dev)
  446. {
  447. struct sa1100_irda *si = netdev_priv(dev);
  448. /*
  449. * Stop RX DMA
  450. */
  451. sa1100_stop_dma(si->rxdma);
  452. /*
  453. * Framing error - we throw away the packet completely.
  454. * Clearing RXE flushes the error conditions and data
  455. * from the fifo.
  456. */
  457. if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
  458. dev->stats.rx_errors++;
  459. if (Ser2HSSR0 & HSSR0_FRE)
  460. dev->stats.rx_frame_errors++;
  461. /*
  462. * Clear out the DMA...
  463. */
  464. Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
  465. /*
  466. * Clear selected status bits now, so we
  467. * don't miss them next time around.
  468. */
  469. Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
  470. }
  471. /*
  472. * Deal with any receive errors. The any of the lowest
  473. * 8 bytes in the FIFO may contain an error. We must read
  474. * them one by one. The "error" could even be the end of
  475. * packet!
  476. */
  477. if (Ser2HSSR0 & HSSR0_EIF)
  478. sa1100_irda_fir_error(si, dev);
  479. /*
  480. * No matter what happens, we must restart reception.
  481. */
  482. sa1100_irda_rx_dma_start(si);
  483. }
  484. static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
  485. {
  486. struct net_device *dev = dev_id;
  487. if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
  488. sa1100_irda_fir_irq(dev);
  489. else
  490. sa1100_irda_hpsir_irq(dev);
  491. return IRQ_HANDLED;
  492. }
  493. /*
  494. * TX DMA completion handler.
  495. */
  496. static void sa1100_irda_txdma_irq(void *id)
  497. {
  498. struct net_device *dev = id;
  499. struct sa1100_irda *si = netdev_priv(dev);
  500. struct sk_buff *skb = si->txskb;
  501. si->txskb = NULL;
  502. /*
  503. * Wait for the transmission to complete. Unfortunately,
  504. * the hardware doesn't give us an interrupt to indicate
  505. * "end of frame".
  506. */
  507. do
  508. rmb();
  509. while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
  510. /*
  511. * Clear the transmit underrun bit.
  512. */
  513. Ser2HSSR0 = HSSR0_TUR;
  514. /*
  515. * Do we need to change speed? Note that we're lazy
  516. * here - we don't free the old rxskb. We don't need
  517. * to allocate a buffer either.
  518. */
  519. if (si->newspeed) {
  520. sa1100_irda_set_speed(si, si->newspeed);
  521. si->newspeed = 0;
  522. }
  523. /*
  524. * Start reception. This disables the transmitter for
  525. * us. This will be using the existing RX buffer.
  526. */
  527. sa1100_irda_rx_dma_start(si);
  528. /*
  529. * Account and free the packet.
  530. */
  531. if (skb) {
  532. dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
  533. dev->stats.tx_packets ++;
  534. dev->stats.tx_bytes += skb->len;
  535. dev_kfree_skb_irq(skb);
  536. }
  537. /*
  538. * Make sure that the TX queue is available for sending
  539. * (for retries). TX has priority over RX at all times.
  540. */
  541. netif_wake_queue(dev);
  542. }
  543. static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  544. {
  545. struct sa1100_irda *si = netdev_priv(dev);
  546. int speed = irda_get_next_speed(skb);
  547. /*
  548. * Does this packet contain a request to change the interface
  549. * speed? If so, remember it until we complete the transmission
  550. * of this frame.
  551. */
  552. if (speed != si->speed && speed != -1)
  553. si->newspeed = speed;
  554. /*
  555. * If this is an empty frame, we can bypass a lot.
  556. */
  557. if (skb->len == 0) {
  558. if (si->newspeed) {
  559. si->newspeed = 0;
  560. sa1100_irda_set_speed(si, speed);
  561. }
  562. dev_kfree_skb(skb);
  563. return NETDEV_TX_OK;
  564. }
  565. if (!IS_FIR(si)) {
  566. netif_stop_queue(dev);
  567. si->tx_buff.data = si->tx_buff.head;
  568. si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
  569. si->tx_buff.truesize);
  570. /*
  571. * Set the transmit interrupt enable. This will fire
  572. * off an interrupt immediately. Note that we disable
  573. * the receiver so we won't get spurious characteres
  574. * received.
  575. */
  576. Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
  577. dev_kfree_skb(skb);
  578. } else {
  579. int mtt = irda_get_mtt(skb);
  580. /*
  581. * We must not be transmitting...
  582. */
  583. BUG_ON(si->txskb);
  584. netif_stop_queue(dev);
  585. si->txskb = skb;
  586. si->txbuf_dma = dma_map_single(si->dev, skb->data,
  587. skb->len, DMA_TO_DEVICE);
  588. sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
  589. /*
  590. * If we have a mean turn-around time, impose the specified
  591. * specified delay. We could shorten this by timing from
  592. * the point we received the packet.
  593. */
  594. if (mtt)
  595. udelay(mtt);
  596. Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
  597. }
  598. return NETDEV_TX_OK;
  599. }
  600. static int
  601. sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  602. {
  603. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  604. struct sa1100_irda *si = netdev_priv(dev);
  605. int ret = -EOPNOTSUPP;
  606. switch (cmd) {
  607. case SIOCSBANDWIDTH:
  608. if (capable(CAP_NET_ADMIN)) {
  609. /*
  610. * We are unable to set the speed if the
  611. * device is not running.
  612. */
  613. if (si->open) {
  614. ret = sa1100_irda_set_speed(si,
  615. rq->ifr_baudrate);
  616. } else {
  617. printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
  618. ret = 0;
  619. }
  620. }
  621. break;
  622. case SIOCSMEDIABUSY:
  623. ret = -EPERM;
  624. if (capable(CAP_NET_ADMIN)) {
  625. irda_device_set_media_busy(dev, TRUE);
  626. ret = 0;
  627. }
  628. break;
  629. case SIOCGRECEIVING:
  630. rq->ifr_receiving = IS_FIR(si) ? 0
  631. : si->rx_buff.state != OUTSIDE_FRAME;
  632. break;
  633. default:
  634. break;
  635. }
  636. return ret;
  637. }
  638. static int sa1100_irda_start(struct net_device *dev)
  639. {
  640. struct sa1100_irda *si = netdev_priv(dev);
  641. int err;
  642. si->speed = 9600;
  643. err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
  644. if (err)
  645. goto err_irq;
  646. err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
  647. NULL, NULL, &si->rxdma);
  648. if (err)
  649. goto err_rx_dma;
  650. err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
  651. sa1100_irda_txdma_irq, dev, &si->txdma);
  652. if (err)
  653. goto err_tx_dma;
  654. /*
  655. * The interrupt must remain disabled for now.
  656. */
  657. disable_irq(dev->irq);
  658. /*
  659. * Setup the serial port for the specified speed.
  660. */
  661. err = sa1100_irda_startup(si);
  662. if (err)
  663. goto err_startup;
  664. /*
  665. * Open a new IrLAP layer instance.
  666. */
  667. si->irlap = irlap_open(dev, &si->qos, "sa1100");
  668. err = -ENOMEM;
  669. if (!si->irlap)
  670. goto err_irlap;
  671. /*
  672. * Now enable the interrupt and start the queue
  673. */
  674. si->open = 1;
  675. sa1100_set_power(si, power_level); /* low power mode */
  676. enable_irq(dev->irq);
  677. netif_start_queue(dev);
  678. return 0;
  679. err_irlap:
  680. si->open = 0;
  681. sa1100_irda_shutdown(si);
  682. err_startup:
  683. sa1100_free_dma(si->txdma);
  684. err_tx_dma:
  685. sa1100_free_dma(si->rxdma);
  686. err_rx_dma:
  687. free_irq(dev->irq, dev);
  688. err_irq:
  689. return err;
  690. }
  691. static int sa1100_irda_stop(struct net_device *dev)
  692. {
  693. struct sa1100_irda *si = netdev_priv(dev);
  694. disable_irq(dev->irq);
  695. sa1100_irda_shutdown(si);
  696. /*
  697. * If we have been doing DMA receive, make sure we
  698. * tidy that up cleanly.
  699. */
  700. if (si->rxskb) {
  701. dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
  702. DMA_FROM_DEVICE);
  703. dev_kfree_skb(si->rxskb);
  704. si->rxskb = NULL;
  705. }
  706. /* Stop IrLAP */
  707. if (si->irlap) {
  708. irlap_close(si->irlap);
  709. si->irlap = NULL;
  710. }
  711. netif_stop_queue(dev);
  712. si->open = 0;
  713. /*
  714. * Free resources
  715. */
  716. sa1100_free_dma(si->txdma);
  717. sa1100_free_dma(si->rxdma);
  718. free_irq(dev->irq, dev);
  719. sa1100_set_power(si, 0);
  720. return 0;
  721. }
  722. static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
  723. {
  724. io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
  725. if (io->head != NULL) {
  726. io->truesize = size;
  727. io->in_frame = FALSE;
  728. io->state = OUTSIDE_FRAME;
  729. io->data = io->head;
  730. }
  731. return io->head ? 0 : -ENOMEM;
  732. }
  733. static const struct net_device_ops sa1100_irda_netdev_ops = {
  734. .ndo_open = sa1100_irda_start,
  735. .ndo_stop = sa1100_irda_stop,
  736. .ndo_start_xmit = sa1100_irda_hard_xmit,
  737. .ndo_do_ioctl = sa1100_irda_ioctl,
  738. };
  739. static int sa1100_irda_probe(struct platform_device *pdev)
  740. {
  741. struct net_device *dev;
  742. struct sa1100_irda *si;
  743. unsigned int baudrate_mask;
  744. int err;
  745. if (!pdev->dev.platform_data)
  746. return -EINVAL;
  747. err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
  748. if (err)
  749. goto err_mem_1;
  750. err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
  751. if (err)
  752. goto err_mem_2;
  753. err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
  754. if (err)
  755. goto err_mem_3;
  756. dev = alloc_irdadev(sizeof(struct sa1100_irda));
  757. if (!dev)
  758. goto err_mem_4;
  759. si = netdev_priv(dev);
  760. si->dev = &pdev->dev;
  761. si->pdata = pdev->dev.platform_data;
  762. /*
  763. * Initialise the HP-SIR buffers
  764. */
  765. err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
  766. if (err)
  767. goto err_mem_5;
  768. err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
  769. if (err)
  770. goto err_mem_5;
  771. dev->netdev_ops = &sa1100_irda_netdev_ops;
  772. dev->irq = IRQ_Ser2ICP;
  773. irda_init_max_qos_capabilies(&si->qos);
  774. /*
  775. * We support original IRDA up to 115k2. (we don't currently
  776. * support 4Mbps). Min Turn Time set to 1ms or greater.
  777. */
  778. baudrate_mask = IR_9600;
  779. switch (max_rate) {
  780. case 4000000: baudrate_mask |= IR_4000000 << 8;
  781. case 115200: baudrate_mask |= IR_115200;
  782. case 57600: baudrate_mask |= IR_57600;
  783. case 38400: baudrate_mask |= IR_38400;
  784. case 19200: baudrate_mask |= IR_19200;
  785. }
  786. si->qos.baud_rate.bits &= baudrate_mask;
  787. si->qos.min_turn_time.bits = 7;
  788. irda_qos_bits_to_value(&si->qos);
  789. si->utcr4 = UTCR4_HPSIR;
  790. if (tx_lpm)
  791. si->utcr4 |= UTCR4_Z1_6us;
  792. /*
  793. * Initially enable HP-SIR modulation, and ensure that the port
  794. * is disabled.
  795. */
  796. Ser2UTCR3 = 0;
  797. Ser2UTCR4 = si->utcr4;
  798. Ser2HSCR0 = HSCR0_UART;
  799. err = register_netdev(dev);
  800. if (err == 0)
  801. platform_set_drvdata(pdev, dev);
  802. if (err) {
  803. err_mem_5:
  804. kfree(si->tx_buff.head);
  805. kfree(si->rx_buff.head);
  806. free_netdev(dev);
  807. err_mem_4:
  808. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  809. err_mem_3:
  810. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  811. err_mem_2:
  812. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  813. }
  814. err_mem_1:
  815. return err;
  816. }
  817. static int sa1100_irda_remove(struct platform_device *pdev)
  818. {
  819. struct net_device *dev = platform_get_drvdata(pdev);
  820. if (dev) {
  821. struct sa1100_irda *si = netdev_priv(dev);
  822. unregister_netdev(dev);
  823. kfree(si->tx_buff.head);
  824. kfree(si->rx_buff.head);
  825. free_netdev(dev);
  826. }
  827. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  828. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  829. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  830. return 0;
  831. }
  832. static struct platform_driver sa1100ir_driver = {
  833. .probe = sa1100_irda_probe,
  834. .remove = sa1100_irda_remove,
  835. .suspend = sa1100_irda_suspend,
  836. .resume = sa1100_irda_resume,
  837. .driver = {
  838. .name = "sa11x0-ir",
  839. .owner = THIS_MODULE,
  840. },
  841. };
  842. static int __init sa1100_irda_init(void)
  843. {
  844. /*
  845. * Limit power level a sensible range.
  846. */
  847. if (power_level < 1)
  848. power_level = 1;
  849. if (power_level > 3)
  850. power_level = 3;
  851. return platform_driver_register(&sa1100ir_driver);
  852. }
  853. static void __exit sa1100_irda_exit(void)
  854. {
  855. platform_driver_unregister(&sa1100ir_driver);
  856. }
  857. module_init(sa1100_irda_init);
  858. module_exit(sa1100_irda_exit);
  859. module_param(power_level, int, 0);
  860. module_param(tx_lpm, int, 0);
  861. module_param(max_rate, int, 0);
  862. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  863. MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
  864. MODULE_LICENSE("GPL");
  865. MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
  866. MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
  867. MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
  868. MODULE_ALIAS("platform:sa11x0-ir");