sa1100_ir.c 22 KB

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