sa1100_ir.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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 <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. if (dma_mapping_error(si->dev, si->rxbuf_dma)) {
  86. dev_kfree_skb_any(si->rxskb);
  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->rxskb) {
  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->rxdma);
  109. sa1100_start_dma(si->rxdma, si->rxbuf_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->rxdma);
  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->rxdma);
  234. sa1100_stop_dma(si->txdma);
  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->rxskb;
  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->rxdma);
  395. len = dma_addr - si->rxbuf_dma;
  396. if (len > HPSIR_MAX_RXLEN)
  397. len = HPSIR_MAX_RXLEN;
  398. dma_unmap_single(si->dev, si->rxbuf_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->rxskb = 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->rxbuf_dma = dma_map_single(si->dev, si->rxskb->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->rxdma);
  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 = si->txskb;
  506. si->txskb = NULL;
  507. /*
  508. * Wait for the transmission to complete. Unfortunately,
  509. * the hardware doesn't give us an interrupt to indicate
  510. * "end of frame".
  511. */
  512. do
  513. rmb();
  514. while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
  515. /*
  516. * Clear the transmit underrun bit.
  517. */
  518. Ser2HSSR0 = HSSR0_TUR;
  519. /*
  520. * Do we need to change speed? Note that we're lazy
  521. * here - we don't free the old rxskb. We don't need
  522. * to allocate a buffer either.
  523. */
  524. if (si->newspeed) {
  525. sa1100_irda_set_speed(si, si->newspeed);
  526. si->newspeed = 0;
  527. }
  528. /*
  529. * Start reception. This disables the transmitter for
  530. * us. This will be using the existing RX buffer.
  531. */
  532. sa1100_irda_rx_dma_start(si);
  533. /*
  534. * Account and free the packet.
  535. */
  536. if (skb) {
  537. dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
  538. dev->stats.tx_packets ++;
  539. dev->stats.tx_bytes += skb->len;
  540. dev_kfree_skb_irq(skb);
  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->txskb);
  589. netif_stop_queue(dev);
  590. si->txskb = skb;
  591. si->txbuf_dma = dma_map_single(si->dev, skb->data,
  592. skb->len, DMA_TO_DEVICE);
  593. if (dma_mapping_error(si->dev, si->txbuf_dma)) {
  594. si->txskb = 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->txdma, si->txbuf_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->rxdma);
  660. if (err)
  661. goto err_rx_dma;
  662. err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
  663. sa1100_irda_txdma_irq, dev, &si->txdma);
  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->txdma);
  696. err_tx_dma:
  697. sa1100_free_dma(si->rxdma);
  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. disable_irq(dev->irq);
  707. sa1100_irda_shutdown(si);
  708. /*
  709. * If we have been doing DMA receive, make sure we
  710. * tidy that up cleanly.
  711. */
  712. if (si->rxskb) {
  713. dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
  714. DMA_FROM_DEVICE);
  715. dev_kfree_skb(si->rxskb);
  716. si->rxskb = NULL;
  717. }
  718. /* Stop IrLAP */
  719. if (si->irlap) {
  720. irlap_close(si->irlap);
  721. si->irlap = NULL;
  722. }
  723. netif_stop_queue(dev);
  724. si->open = 0;
  725. /*
  726. * Free resources
  727. */
  728. sa1100_free_dma(si->txdma);
  729. sa1100_free_dma(si->rxdma);
  730. free_irq(dev->irq, dev);
  731. sa1100_set_power(si, 0);
  732. return 0;
  733. }
  734. static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
  735. {
  736. io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
  737. if (io->head != NULL) {
  738. io->truesize = size;
  739. io->in_frame = FALSE;
  740. io->state = OUTSIDE_FRAME;
  741. io->data = io->head;
  742. }
  743. return io->head ? 0 : -ENOMEM;
  744. }
  745. static const struct net_device_ops sa1100_irda_netdev_ops = {
  746. .ndo_open = sa1100_irda_start,
  747. .ndo_stop = sa1100_irda_stop,
  748. .ndo_start_xmit = sa1100_irda_hard_xmit,
  749. .ndo_do_ioctl = sa1100_irda_ioctl,
  750. };
  751. static int sa1100_irda_probe(struct platform_device *pdev)
  752. {
  753. struct net_device *dev;
  754. struct sa1100_irda *si;
  755. unsigned int baudrate_mask;
  756. int err;
  757. if (!pdev->dev.platform_data)
  758. return -EINVAL;
  759. err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
  760. if (err)
  761. goto err_mem_1;
  762. err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
  763. if (err)
  764. goto err_mem_2;
  765. err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
  766. if (err)
  767. goto err_mem_3;
  768. dev = alloc_irdadev(sizeof(struct sa1100_irda));
  769. if (!dev)
  770. goto err_mem_4;
  771. si = netdev_priv(dev);
  772. si->dev = &pdev->dev;
  773. si->pdata = pdev->dev.platform_data;
  774. /*
  775. * Initialise the HP-SIR buffers
  776. */
  777. err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
  778. if (err)
  779. goto err_mem_5;
  780. err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
  781. if (err)
  782. goto err_mem_5;
  783. dev->netdev_ops = &sa1100_irda_netdev_ops;
  784. dev->irq = IRQ_Ser2ICP;
  785. irda_init_max_qos_capabilies(&si->qos);
  786. /*
  787. * We support original IRDA up to 115k2. (we don't currently
  788. * support 4Mbps). Min Turn Time set to 1ms or greater.
  789. */
  790. baudrate_mask = IR_9600;
  791. switch (max_rate) {
  792. case 4000000: baudrate_mask |= IR_4000000 << 8;
  793. case 115200: baudrate_mask |= IR_115200;
  794. case 57600: baudrate_mask |= IR_57600;
  795. case 38400: baudrate_mask |= IR_38400;
  796. case 19200: baudrate_mask |= IR_19200;
  797. }
  798. si->qos.baud_rate.bits &= baudrate_mask;
  799. si->qos.min_turn_time.bits = 7;
  800. irda_qos_bits_to_value(&si->qos);
  801. si->utcr4 = UTCR4_HPSIR;
  802. if (tx_lpm)
  803. si->utcr4 |= UTCR4_Z1_6us;
  804. /*
  805. * Initially enable HP-SIR modulation, and ensure that the port
  806. * is disabled.
  807. */
  808. Ser2UTCR3 = 0;
  809. Ser2UTCR4 = si->utcr4;
  810. Ser2HSCR0 = HSCR0_UART;
  811. err = register_netdev(dev);
  812. if (err == 0)
  813. platform_set_drvdata(pdev, dev);
  814. if (err) {
  815. err_mem_5:
  816. kfree(si->tx_buff.head);
  817. kfree(si->rx_buff.head);
  818. free_netdev(dev);
  819. err_mem_4:
  820. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  821. err_mem_3:
  822. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  823. err_mem_2:
  824. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  825. }
  826. err_mem_1:
  827. return err;
  828. }
  829. static int sa1100_irda_remove(struct platform_device *pdev)
  830. {
  831. struct net_device *dev = platform_get_drvdata(pdev);
  832. if (dev) {
  833. struct sa1100_irda *si = netdev_priv(dev);
  834. unregister_netdev(dev);
  835. kfree(si->tx_buff.head);
  836. kfree(si->rx_buff.head);
  837. free_netdev(dev);
  838. }
  839. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  840. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  841. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  842. return 0;
  843. }
  844. static struct platform_driver sa1100ir_driver = {
  845. .probe = sa1100_irda_probe,
  846. .remove = sa1100_irda_remove,
  847. .suspend = sa1100_irda_suspend,
  848. .resume = sa1100_irda_resume,
  849. .driver = {
  850. .name = "sa11x0-ir",
  851. .owner = THIS_MODULE,
  852. },
  853. };
  854. static int __init sa1100_irda_init(void)
  855. {
  856. /*
  857. * Limit power level a sensible range.
  858. */
  859. if (power_level < 1)
  860. power_level = 1;
  861. if (power_level > 3)
  862. power_level = 3;
  863. return platform_driver_register(&sa1100ir_driver);
  864. }
  865. static void __exit sa1100_irda_exit(void)
  866. {
  867. platform_driver_unregister(&sa1100ir_driver);
  868. }
  869. module_init(sa1100_irda_init);
  870. module_exit(sa1100_irda_exit);
  871. module_param(power_level, int, 0);
  872. module_param(tx_lpm, int, 0);
  873. module_param(max_rate, int, 0);
  874. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  875. MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
  876. MODULE_LICENSE("GPL");
  877. MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
  878. MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
  879. MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
  880. MODULE_ALIAS("platform:sa11x0-ir");