sa1100_ir.c 24 KB

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