PageRenderTime 57ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/irda/nsc-ircc.c

http://github.com/mirrors/linux
C | 2410 lines | 1479 code | 393 blank | 538 comment | 192 complexity | 17f57aebf552ae244da316190f45a690 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*********************************************************************
  2. *
  3. * Filename: nsc-ircc.c
  4. * Version: 1.0
  5. * Description: Driver for the NSC PC'108 and PC'338 IrDA chipsets
  6. * Status: Stable.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sat Nov 7 21:43:15 1998
  9. * Modified at: Wed Mar 1 11:29:34 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>
  13. * Copyright (c) 1998 Lichen Wang, <lwang@actisys.com>
  14. * Copyright (c) 1998 Actisys Corp., www.actisys.com
  15. * Copyright (c) 2000-2004 Jean Tourrilhes <jt@hpl.hp.com>
  16. * All Rights Reserved
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of
  21. * the License, or (at your option) any later version.
  22. *
  23. * Neither Dag Brattli nor University of Tromsø admit liability nor
  24. * provide warranty for any of this software. This material is
  25. * provided "AS-IS" and at no charge.
  26. *
  27. * Notice that all functions that needs to access the chip in _any_
  28. * way, must save BSR register on entry, and restore it on exit.
  29. * It is _very_ important to follow this policy!
  30. *
  31. * __u8 bank;
  32. *
  33. * bank = inb(iobase+BSR);
  34. *
  35. * do_your_stuff_here();
  36. *
  37. * outb(bank, iobase+BSR);
  38. *
  39. * If you find bugs in this file, its very likely that the same bug
  40. * will also be in w83977af_ir.c since the implementations are quite
  41. * similar.
  42. *
  43. ********************************************************************/
  44. #include <linux/module.h>
  45. #include <linux/gfp.h>
  46. #include <linux/kernel.h>
  47. #include <linux/types.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/ioport.h>
  51. #include <linux/delay.h>
  52. #include <linux/init.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/rtnetlink.h>
  55. #include <linux/dma-mapping.h>
  56. #include <linux/pnp.h>
  57. #include <linux/platform_device.h>
  58. #include <asm/io.h>
  59. #include <asm/dma.h>
  60. #include <asm/byteorder.h>
  61. #include <net/irda/wrapper.h>
  62. #include <net/irda/irda.h>
  63. #include <net/irda/irda_device.h>
  64. #include "nsc-ircc.h"
  65. #define CHIP_IO_EXTENT 8
  66. #define BROKEN_DONGLE_ID
  67. static char *driver_name = "nsc-ircc";
  68. /* Power Management */
  69. #define NSC_IRCC_DRIVER_NAME "nsc-ircc"
  70. static int nsc_ircc_suspend(struct platform_device *dev, pm_message_t state);
  71. static int nsc_ircc_resume(struct platform_device *dev);
  72. static struct platform_driver nsc_ircc_driver = {
  73. .suspend = nsc_ircc_suspend,
  74. .resume = nsc_ircc_resume,
  75. .driver = {
  76. .name = NSC_IRCC_DRIVER_NAME,
  77. },
  78. };
  79. /* Module parameters */
  80. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  81. static int dongle_id;
  82. /* Use BIOS settions by default, but user may supply module parameters */
  83. static unsigned int io[] = { ~0, ~0, ~0, ~0, ~0 };
  84. static unsigned int irq[] = { 0, 0, 0, 0, 0 };
  85. static unsigned int dma[] = { 0, 0, 0, 0, 0 };
  86. static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info);
  87. static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info);
  88. static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info);
  89. static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info);
  90. static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info);
  91. static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info);
  92. #ifdef CONFIG_PNP
  93. static int nsc_ircc_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *id);
  94. #endif
  95. /* These are the known NSC chips */
  96. static nsc_chip_t chips[] = {
  97. /* Name, {cfg registers}, chip id index reg, chip id expected value, revision mask */
  98. { "PC87108", { 0x150, 0x398, 0xea }, 0x05, 0x10, 0xf0,
  99. nsc_ircc_probe_108, nsc_ircc_init_108 },
  100. { "PC87338", { 0x398, 0x15c, 0x2e }, 0x08, 0xb0, 0xf8,
  101. nsc_ircc_probe_338, nsc_ircc_init_338 },
  102. /* Contributed by Steffen Pingel - IBM X40 */
  103. { "PC8738x", { 0x164e, 0x4e, 0x2e }, 0x20, 0xf4, 0xff,
  104. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  105. /* Contributed by Jan Frey - IBM A30/A31 */
  106. { "PC8739x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xea, 0xff,
  107. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  108. /* IBM ThinkPads using PC8738x (T60/X60/Z60) */
  109. { "IBM-PC8738x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xf4, 0xff,
  110. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  111. /* IBM ThinkPads using PC8394T (T43/R52/?) */
  112. { "IBM-PC8394T", { 0x2e, 0x4e, 0x0 }, 0x20, 0xf9, 0xff,
  113. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  114. { NULL }
  115. };
  116. static struct nsc_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL, NULL };
  117. static char *dongle_types[] = {
  118. "Differential serial interface",
  119. "Differential serial interface",
  120. "Reserved",
  121. "Reserved",
  122. "Sharp RY5HD01",
  123. "Reserved",
  124. "Single-ended serial interface",
  125. "Consumer-IR only",
  126. "HP HSDL-2300, HP HSDL-3600/HSDL-3610",
  127. "IBM31T1100 or Temic TFDS6000/TFDS6500",
  128. "Reserved",
  129. "Reserved",
  130. "HP HSDL-1100/HSDL-2100",
  131. "HP HSDL-1100/HSDL-2100",
  132. "Supports SIR Mode only",
  133. "No dongle connected",
  134. };
  135. /* PNP probing */
  136. static chipio_t pnp_info;
  137. static const struct pnp_device_id nsc_ircc_pnp_table[] = {
  138. { .id = "NSC6001", .driver_data = 0 },
  139. { .id = "HWPC224", .driver_data = 0 },
  140. { .id = "IBM0071", .driver_data = NSC_FORCE_DONGLE_TYPE9 },
  141. { }
  142. };
  143. MODULE_DEVICE_TABLE(pnp, nsc_ircc_pnp_table);
  144. static struct pnp_driver nsc_ircc_pnp_driver = {
  145. #ifdef CONFIG_PNP
  146. .name = "nsc-ircc",
  147. .id_table = nsc_ircc_pnp_table,
  148. .probe = nsc_ircc_pnp_probe,
  149. #endif
  150. };
  151. /* Some prototypes */
  152. static int nsc_ircc_open(chipio_t *info);
  153. static int nsc_ircc_close(struct nsc_ircc_cb *self);
  154. static int nsc_ircc_setup(chipio_t *info);
  155. static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self);
  156. static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self);
  157. static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase);
  158. static netdev_tx_t nsc_ircc_hard_xmit_sir(struct sk_buff *skb,
  159. struct net_device *dev);
  160. static netdev_tx_t nsc_ircc_hard_xmit_fir(struct sk_buff *skb,
  161. struct net_device *dev);
  162. static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
  163. static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase);
  164. static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 baud);
  165. static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self);
  166. static int nsc_ircc_read_dongle_id (int iobase);
  167. static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id);
  168. static int nsc_ircc_net_open(struct net_device *dev);
  169. static int nsc_ircc_net_close(struct net_device *dev);
  170. static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  171. /* Globals */
  172. static int pnp_registered;
  173. static int pnp_succeeded;
  174. /*
  175. * Function nsc_ircc_init ()
  176. *
  177. * Initialize chip. Just try to find out how many chips we are dealing with
  178. * and where they are
  179. */
  180. static int __init nsc_ircc_init(void)
  181. {
  182. chipio_t info;
  183. nsc_chip_t *chip;
  184. int ret;
  185. int cfg_base;
  186. int cfg, id;
  187. int reg;
  188. int i = 0;
  189. ret = platform_driver_register(&nsc_ircc_driver);
  190. if (ret) {
  191. net_err_ratelimited("%s, Can't register driver!\n",
  192. driver_name);
  193. return ret;
  194. }
  195. /* Register with PnP subsystem to detect disable ports */
  196. ret = pnp_register_driver(&nsc_ircc_pnp_driver);
  197. if (!ret)
  198. pnp_registered = 1;
  199. ret = -ENODEV;
  200. /* Probe for all the NSC chipsets we know about */
  201. for (chip = chips; chip->name ; chip++) {
  202. pr_debug("%s(), Probing for %s ...\n", __func__,
  203. chip->name);
  204. /* Try all config registers for this chip */
  205. for (cfg = 0; cfg < ARRAY_SIZE(chip->cfg); cfg++) {
  206. cfg_base = chip->cfg[cfg];
  207. if (!cfg_base)
  208. continue;
  209. /* Read index register */
  210. reg = inb(cfg_base);
  211. if (reg == 0xff) {
  212. pr_debug("%s() no chip at 0x%03x\n",
  213. __func__, cfg_base);
  214. continue;
  215. }
  216. /* Read chip identification register */
  217. outb(chip->cid_index, cfg_base);
  218. id = inb(cfg_base+1);
  219. if ((id & chip->cid_mask) == chip->cid_value) {
  220. pr_debug("%s() Found %s chip, revision=%d\n",
  221. __func__, chip->name,
  222. id & ~chip->cid_mask);
  223. /*
  224. * If we found a correct PnP setting,
  225. * we first try it.
  226. */
  227. if (pnp_succeeded) {
  228. memset(&info, 0, sizeof(chipio_t));
  229. info.cfg_base = cfg_base;
  230. info.fir_base = pnp_info.fir_base;
  231. info.dma = pnp_info.dma;
  232. info.irq = pnp_info.irq;
  233. if (info.fir_base < 0x2000) {
  234. net_info_ratelimited("%s, chip->init\n",
  235. driver_name);
  236. chip->init(chip, &info);
  237. } else
  238. chip->probe(chip, &info);
  239. if (nsc_ircc_open(&info) >= 0)
  240. ret = 0;
  241. }
  242. /*
  243. * Opening based on PnP values failed.
  244. * Let's fallback to user values, or probe
  245. * the chip.
  246. */
  247. if (ret) {
  248. pr_debug("%s, PnP init failed\n",
  249. driver_name);
  250. memset(&info, 0, sizeof(chipio_t));
  251. info.cfg_base = cfg_base;
  252. info.fir_base = io[i];
  253. info.dma = dma[i];
  254. info.irq = irq[i];
  255. /*
  256. * If the user supplies the base address, then
  257. * we init the chip, if not we probe the values
  258. * set by the BIOS
  259. */
  260. if (io[i] < 0x2000) {
  261. chip->init(chip, &info);
  262. } else
  263. chip->probe(chip, &info);
  264. if (nsc_ircc_open(&info) >= 0)
  265. ret = 0;
  266. }
  267. i++;
  268. } else {
  269. pr_debug("%s(), Wrong chip id=0x%02x\n",
  270. __func__, id);
  271. }
  272. }
  273. }
  274. if (ret) {
  275. platform_driver_unregister(&nsc_ircc_driver);
  276. pnp_unregister_driver(&nsc_ircc_pnp_driver);
  277. pnp_registered = 0;
  278. }
  279. return ret;
  280. }
  281. /*
  282. * Function nsc_ircc_cleanup ()
  283. *
  284. * Close all configured chips
  285. *
  286. */
  287. static void __exit nsc_ircc_cleanup(void)
  288. {
  289. int i;
  290. for (i = 0; i < ARRAY_SIZE(dev_self); i++) {
  291. if (dev_self[i])
  292. nsc_ircc_close(dev_self[i]);
  293. }
  294. platform_driver_unregister(&nsc_ircc_driver);
  295. if (pnp_registered)
  296. pnp_unregister_driver(&nsc_ircc_pnp_driver);
  297. pnp_registered = 0;
  298. }
  299. static const struct net_device_ops nsc_ircc_sir_ops = {
  300. .ndo_open = nsc_ircc_net_open,
  301. .ndo_stop = nsc_ircc_net_close,
  302. .ndo_start_xmit = nsc_ircc_hard_xmit_sir,
  303. .ndo_do_ioctl = nsc_ircc_net_ioctl,
  304. };
  305. static const struct net_device_ops nsc_ircc_fir_ops = {
  306. .ndo_open = nsc_ircc_net_open,
  307. .ndo_stop = nsc_ircc_net_close,
  308. .ndo_start_xmit = nsc_ircc_hard_xmit_fir,
  309. .ndo_do_ioctl = nsc_ircc_net_ioctl,
  310. };
  311. /*
  312. * Function nsc_ircc_open (iobase, irq)
  313. *
  314. * Open driver instance
  315. *
  316. */
  317. static int __init nsc_ircc_open(chipio_t *info)
  318. {
  319. struct net_device *dev;
  320. struct nsc_ircc_cb *self;
  321. void *ret;
  322. int err, chip_index;
  323. for (chip_index = 0; chip_index < ARRAY_SIZE(dev_self); chip_index++) {
  324. if (!dev_self[chip_index])
  325. break;
  326. }
  327. if (chip_index == ARRAY_SIZE(dev_self)) {
  328. net_err_ratelimited("%s(), maximum number of supported chips reached!\n",
  329. __func__);
  330. return -ENOMEM;
  331. }
  332. net_info_ratelimited("%s, Found chip at base=0x%03x\n",
  333. driver_name, info->cfg_base);
  334. if ((nsc_ircc_setup(info)) == -1)
  335. return -1;
  336. net_info_ratelimited("%s, driver loaded (Dag Brattli)\n", driver_name);
  337. dev = alloc_irdadev(sizeof(struct nsc_ircc_cb));
  338. if (dev == NULL) {
  339. net_err_ratelimited("%s(), can't allocate memory for control block!\n",
  340. __func__);
  341. return -ENOMEM;
  342. }
  343. self = netdev_priv(dev);
  344. self->netdev = dev;
  345. spin_lock_init(&self->lock);
  346. /* Need to store self somewhere */
  347. dev_self[chip_index] = self;
  348. self->index = chip_index;
  349. /* Initialize IO */
  350. self->io.cfg_base = info->cfg_base;
  351. self->io.fir_base = info->fir_base;
  352. self->io.irq = info->irq;
  353. self->io.fir_ext = CHIP_IO_EXTENT;
  354. self->io.dma = info->dma;
  355. self->io.fifo_size = 32;
  356. /* Reserve the ioports that we need */
  357. ret = request_region(self->io.fir_base, self->io.fir_ext, driver_name);
  358. if (!ret) {
  359. net_warn_ratelimited("%s(), can't get iobase of 0x%03x\n",
  360. __func__, self->io.fir_base);
  361. err = -ENODEV;
  362. goto out1;
  363. }
  364. /* Initialize QoS for this device */
  365. irda_init_max_qos_capabilies(&self->qos);
  366. /* The only value we must override it the baudrate */
  367. self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  368. IR_115200|IR_576000|IR_1152000 |(IR_4000000 << 8);
  369. self->qos.min_turn_time.bits = qos_mtt_bits;
  370. irda_qos_bits_to_value(&self->qos);
  371. /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
  372. self->rx_buff.truesize = 14384;
  373. self->tx_buff.truesize = 14384;
  374. /* Allocate memory if needed */
  375. self->rx_buff.head =
  376. dma_zalloc_coherent(NULL, self->rx_buff.truesize,
  377. &self->rx_buff_dma, GFP_KERNEL);
  378. if (self->rx_buff.head == NULL) {
  379. err = -ENOMEM;
  380. goto out2;
  381. }
  382. self->tx_buff.head =
  383. dma_zalloc_coherent(NULL, self->tx_buff.truesize,
  384. &self->tx_buff_dma, GFP_KERNEL);
  385. if (self->tx_buff.head == NULL) {
  386. err = -ENOMEM;
  387. goto out3;
  388. }
  389. self->rx_buff.in_frame = FALSE;
  390. self->rx_buff.state = OUTSIDE_FRAME;
  391. self->tx_buff.data = self->tx_buff.head;
  392. self->rx_buff.data = self->rx_buff.head;
  393. /* Reset Tx queue info */
  394. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  395. self->tx_fifo.tail = self->tx_buff.head;
  396. /* Override the network functions we need to use */
  397. dev->netdev_ops = &nsc_ircc_sir_ops;
  398. err = register_netdev(dev);
  399. if (err) {
  400. net_err_ratelimited("%s(), register_netdev() failed!\n",
  401. __func__);
  402. goto out4;
  403. }
  404. net_info_ratelimited("IrDA: Registered device %s\n", dev->name);
  405. /* Check if user has supplied a valid dongle id or not */
  406. if ((dongle_id <= 0) ||
  407. (dongle_id >= ARRAY_SIZE(dongle_types))) {
  408. dongle_id = nsc_ircc_read_dongle_id(self->io.fir_base);
  409. net_info_ratelimited("%s, Found dongle: %s\n",
  410. driver_name, dongle_types[dongle_id]);
  411. } else {
  412. net_info_ratelimited("%s, Using dongle: %s\n",
  413. driver_name, dongle_types[dongle_id]);
  414. }
  415. self->io.dongle_id = dongle_id;
  416. nsc_ircc_init_dongle_interface(self->io.fir_base, dongle_id);
  417. self->pldev = platform_device_register_simple(NSC_IRCC_DRIVER_NAME,
  418. self->index, NULL, 0);
  419. if (IS_ERR(self->pldev)) {
  420. err = PTR_ERR(self->pldev);
  421. goto out5;
  422. }
  423. platform_set_drvdata(self->pldev, self);
  424. return chip_index;
  425. out5:
  426. unregister_netdev(dev);
  427. out4:
  428. dma_free_coherent(NULL, self->tx_buff.truesize,
  429. self->tx_buff.head, self->tx_buff_dma);
  430. out3:
  431. dma_free_coherent(NULL, self->rx_buff.truesize,
  432. self->rx_buff.head, self->rx_buff_dma);
  433. out2:
  434. release_region(self->io.fir_base, self->io.fir_ext);
  435. out1:
  436. free_netdev(dev);
  437. dev_self[chip_index] = NULL;
  438. return err;
  439. }
  440. /*
  441. * Function nsc_ircc_close (self)
  442. *
  443. * Close driver instance
  444. *
  445. */
  446. static int __exit nsc_ircc_close(struct nsc_ircc_cb *self)
  447. {
  448. int iobase;
  449. IRDA_ASSERT(self != NULL, return -1;);
  450. iobase = self->io.fir_base;
  451. platform_device_unregister(self->pldev);
  452. /* Remove netdevice */
  453. unregister_netdev(self->netdev);
  454. /* Release the PORT that this driver is using */
  455. pr_debug("%s(), Releasing Region %03x\n",
  456. __func__, self->io.fir_base);
  457. release_region(self->io.fir_base, self->io.fir_ext);
  458. if (self->tx_buff.head)
  459. dma_free_coherent(NULL, self->tx_buff.truesize,
  460. self->tx_buff.head, self->tx_buff_dma);
  461. if (self->rx_buff.head)
  462. dma_free_coherent(NULL, self->rx_buff.truesize,
  463. self->rx_buff.head, self->rx_buff_dma);
  464. dev_self[self->index] = NULL;
  465. free_netdev(self->netdev);
  466. return 0;
  467. }
  468. /*
  469. * Function nsc_ircc_init_108 (iobase, cfg_base, irq, dma)
  470. *
  471. * Initialize the NSC '108 chip
  472. *
  473. */
  474. static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info)
  475. {
  476. int cfg_base = info->cfg_base;
  477. __u8 temp=0;
  478. outb(2, cfg_base); /* Mode Control Register (MCTL) */
  479. outb(0x00, cfg_base+1); /* Disable device */
  480. /* Base Address and Interrupt Control Register (BAIC) */
  481. outb(CFG_108_BAIC, cfg_base);
  482. switch (info->fir_base) {
  483. case 0x3e8: outb(0x14, cfg_base+1); break;
  484. case 0x2e8: outb(0x15, cfg_base+1); break;
  485. case 0x3f8: outb(0x16, cfg_base+1); break;
  486. case 0x2f8: outb(0x17, cfg_base+1); break;
  487. default: net_err_ratelimited("%s(), invalid base_address\n", __func__);
  488. }
  489. /* Control Signal Routing Register (CSRT) */
  490. switch (info->irq) {
  491. case 3: temp = 0x01; break;
  492. case 4: temp = 0x02; break;
  493. case 5: temp = 0x03; break;
  494. case 7: temp = 0x04; break;
  495. case 9: temp = 0x05; break;
  496. case 11: temp = 0x06; break;
  497. case 15: temp = 0x07; break;
  498. default: net_err_ratelimited("%s(), invalid irq\n", __func__);
  499. }
  500. outb(CFG_108_CSRT, cfg_base);
  501. switch (info->dma) {
  502. case 0: outb(0x08+temp, cfg_base+1); break;
  503. case 1: outb(0x10+temp, cfg_base+1); break;
  504. case 3: outb(0x18+temp, cfg_base+1); break;
  505. default: net_err_ratelimited("%s(), invalid dma\n", __func__);
  506. }
  507. outb(CFG_108_MCTL, cfg_base); /* Mode Control Register (MCTL) */
  508. outb(0x03, cfg_base+1); /* Enable device */
  509. return 0;
  510. }
  511. /*
  512. * Function nsc_ircc_probe_108 (chip, info)
  513. *
  514. *
  515. *
  516. */
  517. static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info)
  518. {
  519. int cfg_base = info->cfg_base;
  520. int reg;
  521. /* Read address and interrupt control register (BAIC) */
  522. outb(CFG_108_BAIC, cfg_base);
  523. reg = inb(cfg_base+1);
  524. switch (reg & 0x03) {
  525. case 0:
  526. info->fir_base = 0x3e8;
  527. break;
  528. case 1:
  529. info->fir_base = 0x2e8;
  530. break;
  531. case 2:
  532. info->fir_base = 0x3f8;
  533. break;
  534. case 3:
  535. info->fir_base = 0x2f8;
  536. break;
  537. }
  538. info->sir_base = info->fir_base;
  539. pr_debug("%s(), probing fir_base=0x%03x\n", __func__,
  540. info->fir_base);
  541. /* Read control signals routing register (CSRT) */
  542. outb(CFG_108_CSRT, cfg_base);
  543. reg = inb(cfg_base+1);
  544. switch (reg & 0x07) {
  545. case 0:
  546. info->irq = -1;
  547. break;
  548. case 1:
  549. info->irq = 3;
  550. break;
  551. case 2:
  552. info->irq = 4;
  553. break;
  554. case 3:
  555. info->irq = 5;
  556. break;
  557. case 4:
  558. info->irq = 7;
  559. break;
  560. case 5:
  561. info->irq = 9;
  562. break;
  563. case 6:
  564. info->irq = 11;
  565. break;
  566. case 7:
  567. info->irq = 15;
  568. break;
  569. }
  570. pr_debug("%s(), probing irq=%d\n", __func__, info->irq);
  571. /* Currently we only read Rx DMA but it will also be used for Tx */
  572. switch ((reg >> 3) & 0x03) {
  573. case 0:
  574. info->dma = -1;
  575. break;
  576. case 1:
  577. info->dma = 0;
  578. break;
  579. case 2:
  580. info->dma = 1;
  581. break;
  582. case 3:
  583. info->dma = 3;
  584. break;
  585. }
  586. pr_debug("%s(), probing dma=%d\n", __func__, info->dma);
  587. /* Read mode control register (MCTL) */
  588. outb(CFG_108_MCTL, cfg_base);
  589. reg = inb(cfg_base+1);
  590. info->enabled = reg & 0x01;
  591. info->suspended = !((reg >> 1) & 0x01);
  592. return 0;
  593. }
  594. /*
  595. * Function nsc_ircc_init_338 (chip, info)
  596. *
  597. * Initialize the NSC '338 chip. Remember that the 87338 needs two
  598. * consecutive writes to the data registers while CPU interrupts are
  599. * disabled. The 97338 does not require this, but shouldn't be any
  600. * harm if we do it anyway.
  601. */
  602. static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info)
  603. {
  604. /* No init yet */
  605. return 0;
  606. }
  607. /*
  608. * Function nsc_ircc_probe_338 (chip, info)
  609. *
  610. *
  611. *
  612. */
  613. static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info)
  614. {
  615. int cfg_base = info->cfg_base;
  616. int reg, com = 0;
  617. int pnp;
  618. /* Read function enable register (FER) */
  619. outb(CFG_338_FER, cfg_base);
  620. reg = inb(cfg_base+1);
  621. info->enabled = (reg >> 2) & 0x01;
  622. /* Check if we are in Legacy or PnP mode */
  623. outb(CFG_338_PNP0, cfg_base);
  624. reg = inb(cfg_base+1);
  625. pnp = (reg >> 3) & 0x01;
  626. if (pnp) {
  627. pr_debug("(), Chip is in PnP mode\n");
  628. outb(0x46, cfg_base);
  629. reg = (inb(cfg_base+1) & 0xfe) << 2;
  630. outb(0x47, cfg_base);
  631. reg |= ((inb(cfg_base+1) & 0xfc) << 8);
  632. info->fir_base = reg;
  633. } else {
  634. /* Read function address register (FAR) */
  635. outb(CFG_338_FAR, cfg_base);
  636. reg = inb(cfg_base+1);
  637. switch ((reg >> 4) & 0x03) {
  638. case 0:
  639. info->fir_base = 0x3f8;
  640. break;
  641. case 1:
  642. info->fir_base = 0x2f8;
  643. break;
  644. case 2:
  645. com = 3;
  646. break;
  647. case 3:
  648. com = 4;
  649. break;
  650. }
  651. if (com) {
  652. switch ((reg >> 6) & 0x03) {
  653. case 0:
  654. if (com == 3)
  655. info->fir_base = 0x3e8;
  656. else
  657. info->fir_base = 0x2e8;
  658. break;
  659. case 1:
  660. if (com == 3)
  661. info->fir_base = 0x338;
  662. else
  663. info->fir_base = 0x238;
  664. break;
  665. case 2:
  666. if (com == 3)
  667. info->fir_base = 0x2e8;
  668. else
  669. info->fir_base = 0x2e0;
  670. break;
  671. case 3:
  672. if (com == 3)
  673. info->fir_base = 0x220;
  674. else
  675. info->fir_base = 0x228;
  676. break;
  677. }
  678. }
  679. }
  680. info->sir_base = info->fir_base;
  681. /* Read PnP register 1 (PNP1) */
  682. outb(CFG_338_PNP1, cfg_base);
  683. reg = inb(cfg_base+1);
  684. info->irq = reg >> 4;
  685. /* Read PnP register 3 (PNP3) */
  686. outb(CFG_338_PNP3, cfg_base);
  687. reg = inb(cfg_base+1);
  688. info->dma = (reg & 0x07) - 1;
  689. /* Read power and test register (PTR) */
  690. outb(CFG_338_PTR, cfg_base);
  691. reg = inb(cfg_base+1);
  692. info->suspended = reg & 0x01;
  693. return 0;
  694. }
  695. /*
  696. * Function nsc_ircc_init_39x (chip, info)
  697. *
  698. * Now that we know it's a '39x (see probe below), we need to
  699. * configure it so we can use it.
  700. *
  701. * The NSC '338 chip is a Super I/O chip with a "bank" architecture,
  702. * the configuration of the different functionality (serial, parallel,
  703. * floppy...) are each in a different bank (Logical Device Number).
  704. * The base address, irq and dma configuration registers are common
  705. * to all functionalities (index 0x30 to 0x7F).
  706. * There is only one configuration register specific to the
  707. * serial port, CFG_39X_SPC.
  708. * JeanII
  709. *
  710. * Note : this code was written by Jan Frey <janfrey@web.de>
  711. */
  712. static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info)
  713. {
  714. int cfg_base = info->cfg_base;
  715. int enabled;
  716. /* User is sure about his config... accept it. */
  717. pr_debug("%s(): nsc_ircc_init_39x (user settings): io=0x%04x, irq=%d, dma=%d\n",
  718. __func__, info->fir_base, info->irq, info->dma);
  719. /* Access bank for SP2 */
  720. outb(CFG_39X_LDN, cfg_base);
  721. outb(0x02, cfg_base+1);
  722. /* Configure SP2 */
  723. /* We want to enable the device if not enabled */
  724. outb(CFG_39X_ACT, cfg_base);
  725. enabled = inb(cfg_base+1) & 0x01;
  726. if (!enabled) {
  727. /* Enable the device */
  728. outb(CFG_39X_SIOCF1, cfg_base);
  729. outb(0x01, cfg_base+1);
  730. /* May want to update info->enabled. Jean II */
  731. }
  732. /* Enable UART bank switching (bit 7) ; Sets the chip to normal
  733. * power mode (wake up from sleep mode) (bit 1) */
  734. outb(CFG_39X_SPC, cfg_base);
  735. outb(0x82, cfg_base+1);
  736. return 0;
  737. }
  738. /*
  739. * Function nsc_ircc_probe_39x (chip, info)
  740. *
  741. * Test if we really have a '39x chip at the given address
  742. *
  743. * Note : this code was written by Jan Frey <janfrey@web.de>
  744. */
  745. static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info)
  746. {
  747. int cfg_base = info->cfg_base;
  748. int reg1, reg2, irq, irqt, dma1, dma2;
  749. int enabled, susp;
  750. pr_debug("%s(), nsc_ircc_probe_39x, base=%d\n",
  751. __func__, cfg_base);
  752. /* This function should be executed with irq off to avoid
  753. * another driver messing with the Super I/O bank - Jean II */
  754. /* Access bank for SP2 */
  755. outb(CFG_39X_LDN, cfg_base);
  756. outb(0x02, cfg_base+1);
  757. /* Read infos about SP2 ; store in info struct */
  758. outb(CFG_39X_BASEH, cfg_base);
  759. reg1 = inb(cfg_base+1);
  760. outb(CFG_39X_BASEL, cfg_base);
  761. reg2 = inb(cfg_base+1);
  762. info->fir_base = (reg1 << 8) | reg2;
  763. outb(CFG_39X_IRQNUM, cfg_base);
  764. irq = inb(cfg_base+1);
  765. outb(CFG_39X_IRQSEL, cfg_base);
  766. irqt = inb(cfg_base+1);
  767. info->irq = irq;
  768. outb(CFG_39X_DMA0, cfg_base);
  769. dma1 = inb(cfg_base+1);
  770. outb(CFG_39X_DMA1, cfg_base);
  771. dma2 = inb(cfg_base+1);
  772. info->dma = dma1 -1;
  773. outb(CFG_39X_ACT, cfg_base);
  774. info->enabled = enabled = inb(cfg_base+1) & 0x01;
  775. outb(CFG_39X_SPC, cfg_base);
  776. susp = 1 - ((inb(cfg_base+1) & 0x02) >> 1);
  777. pr_debug("%s(): io=0x%02x%02x, irq=%d (type %d), rxdma=%d, txdma=%d, enabled=%d (suspended=%d)\n",
  778. __func__, reg1, reg2, irq, irqt, dma1, dma2, enabled, susp);
  779. /* Configure SP2 */
  780. /* We want to enable the device if not enabled */
  781. outb(CFG_39X_ACT, cfg_base);
  782. enabled = inb(cfg_base+1) & 0x01;
  783. if (!enabled) {
  784. /* Enable the device */
  785. outb(CFG_39X_SIOCF1, cfg_base);
  786. outb(0x01, cfg_base+1);
  787. /* May want to update info->enabled. Jean II */
  788. }
  789. /* Enable UART bank switching (bit 7) ; Sets the chip to normal
  790. * power mode (wake up from sleep mode) (bit 1) */
  791. outb(CFG_39X_SPC, cfg_base);
  792. outb(0x82, cfg_base+1);
  793. return 0;
  794. }
  795. #ifdef CONFIG_PNP
  796. /* PNP probing */
  797. static int nsc_ircc_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *id)
  798. {
  799. memset(&pnp_info, 0, sizeof(chipio_t));
  800. pnp_info.irq = -1;
  801. pnp_info.dma = -1;
  802. pnp_succeeded = 1;
  803. if (id->driver_data & NSC_FORCE_DONGLE_TYPE9)
  804. dongle_id = 0x9;
  805. /* There doesn't seem to be any way of getting the cfg_base.
  806. * On my box, cfg_base is in the PnP descriptor of the
  807. * motherboard. Oh well... Jean II */
  808. if (pnp_port_valid(dev, 0) &&
  809. !(pnp_port_flags(dev, 0) & IORESOURCE_DISABLED))
  810. pnp_info.fir_base = pnp_port_start(dev, 0);
  811. if (pnp_irq_valid(dev, 0) &&
  812. !(pnp_irq_flags(dev, 0) & IORESOURCE_DISABLED))
  813. pnp_info.irq = pnp_irq(dev, 0);
  814. if (pnp_dma_valid(dev, 0) &&
  815. !(pnp_dma_flags(dev, 0) & IORESOURCE_DISABLED))
  816. pnp_info.dma = pnp_dma(dev, 0);
  817. pr_debug("%s() : From PnP, found firbase 0x%03X ; irq %d ; dma %d.\n",
  818. __func__, pnp_info.fir_base, pnp_info.irq, pnp_info.dma);
  819. if((pnp_info.fir_base == 0) ||
  820. (pnp_info.irq == -1) || (pnp_info.dma == -1)) {
  821. /* Returning an error will disable the device. Yuck ! */
  822. //return -EINVAL;
  823. pnp_succeeded = 0;
  824. }
  825. return 0;
  826. }
  827. #endif
  828. /*
  829. * Function nsc_ircc_setup (info)
  830. *
  831. * Returns non-negative on success.
  832. *
  833. */
  834. static int nsc_ircc_setup(chipio_t *info)
  835. {
  836. int version;
  837. int iobase = info->fir_base;
  838. /* Read the Module ID */
  839. switch_bank(iobase, BANK3);
  840. version = inb(iobase+MID);
  841. pr_debug("%s() Driver %s Found chip version %02x\n",
  842. __func__, driver_name, version);
  843. /* Should be 0x2? */
  844. if (0x20 != (version & 0xf0)) {
  845. net_err_ratelimited("%s, Wrong chip version %02x\n",
  846. driver_name, version);
  847. return -1;
  848. }
  849. /* Switch to advanced mode */
  850. switch_bank(iobase, BANK2);
  851. outb(ECR1_EXT_SL, iobase+ECR1);
  852. switch_bank(iobase, BANK0);
  853. /* Set FIFO threshold to TX17, RX16, reset and enable FIFO's */
  854. switch_bank(iobase, BANK0);
  855. outb(FCR_RXTH|FCR_TXTH|FCR_TXSR|FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
  856. outb(0x03, iobase+LCR); /* 8 bit word length */
  857. outb(MCR_SIR, iobase+MCR); /* Start at SIR-mode, also clears LSR*/
  858. /* Set FIFO size to 32 */
  859. switch_bank(iobase, BANK2);
  860. outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
  861. /* IRCR2: FEND_MD is not set */
  862. switch_bank(iobase, BANK5);
  863. outb(0x02, iobase+4);
  864. /* Make sure that some defaults are OK */
  865. switch_bank(iobase, BANK6);
  866. outb(0x20, iobase+0); /* Set 32 bits FIR CRC */
  867. outb(0x0a, iobase+1); /* Set MIR pulse width */
  868. outb(0x0d, iobase+2); /* Set SIR pulse width to 1.6us */
  869. outb(0x2a, iobase+4); /* Set beginning frag, and preamble length */
  870. /* Enable receive interrupts */
  871. switch_bank(iobase, BANK0);
  872. outb(IER_RXHDL_IE, iobase+IER);
  873. return 0;
  874. }
  875. /*
  876. * Function nsc_ircc_read_dongle_id (void)
  877. *
  878. * Try to read dongle identification. This procedure needs to be executed
  879. * once after power-on/reset. It also needs to be used whenever you suspect
  880. * that the user may have plugged/unplugged the IrDA Dongle.
  881. */
  882. static int nsc_ircc_read_dongle_id (int iobase)
  883. {
  884. int dongle_id;
  885. __u8 bank;
  886. bank = inb(iobase+BSR);
  887. /* Select Bank 7 */
  888. switch_bank(iobase, BANK7);
  889. /* IRCFG4: IRSL0_DS and IRSL21_DS are cleared */
  890. outb(0x00, iobase+7);
  891. /* ID0, 1, and 2 are pulled up/down very slowly */
  892. udelay(50);
  893. /* IRCFG1: read the ID bits */
  894. dongle_id = inb(iobase+4) & 0x0f;
  895. #ifdef BROKEN_DONGLE_ID
  896. if (dongle_id == 0x0a)
  897. dongle_id = 0x09;
  898. #endif
  899. /* Go back to bank 0 before returning */
  900. switch_bank(iobase, BANK0);
  901. outb(bank, iobase+BSR);
  902. return dongle_id;
  903. }
  904. /*
  905. * Function nsc_ircc_init_dongle_interface (iobase, dongle_id)
  906. *
  907. * This function initializes the dongle for the transceiver that is
  908. * used. This procedure needs to be executed once after
  909. * power-on/reset. It also needs to be used whenever you suspect that
  910. * the dongle is changed.
  911. */
  912. static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id)
  913. {
  914. int bank;
  915. /* Save current bank */
  916. bank = inb(iobase+BSR);
  917. /* Select Bank 7 */
  918. switch_bank(iobase, BANK7);
  919. /* IRCFG4: set according to dongle_id */
  920. switch (dongle_id) {
  921. case 0x00: /* same as */
  922. case 0x01: /* Differential serial interface */
  923. pr_debug("%s(), %s not defined by irda yet\n",
  924. __func__, dongle_types[dongle_id]);
  925. break;
  926. case 0x02: /* same as */
  927. case 0x03: /* Reserved */
  928. pr_debug("%s(), %s not defined by irda yet\n",
  929. __func__, dongle_types[dongle_id]);
  930. break;
  931. case 0x04: /* Sharp RY5HD01 */
  932. break;
  933. case 0x05: /* Reserved, but this is what the Thinkpad reports */
  934. pr_debug("%s(), %s not defined by irda yet\n",
  935. __func__, dongle_types[dongle_id]);
  936. break;
  937. case 0x06: /* Single-ended serial interface */
  938. pr_debug("%s(), %s not defined by irda yet\n",
  939. __func__, dongle_types[dongle_id]);
  940. break;
  941. case 0x07: /* Consumer-IR only */
  942. pr_debug("%s(), %s is not for IrDA mode\n",
  943. __func__, dongle_types[dongle_id]);
  944. break;
  945. case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
  946. pr_debug("%s(), %s\n",
  947. __func__, dongle_types[dongle_id]);
  948. break;
  949. case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
  950. outb(0x28, iobase+7); /* Set irsl[0-2] as output */
  951. break;
  952. case 0x0A: /* same as */
  953. case 0x0B: /* Reserved */
  954. pr_debug("%s(), %s not defined by irda yet\n",
  955. __func__, dongle_types[dongle_id]);
  956. break;
  957. case 0x0C: /* same as */
  958. case 0x0D: /* HP HSDL-1100/HSDL-2100 */
  959. /*
  960. * Set irsl0 as input, irsl[1-2] as output, and separate
  961. * inputs are used for SIR and MIR/FIR
  962. */
  963. outb(0x48, iobase+7);
  964. break;
  965. case 0x0E: /* Supports SIR Mode only */
  966. outb(0x28, iobase+7); /* Set irsl[0-2] as output */
  967. break;
  968. case 0x0F: /* No dongle connected */
  969. pr_debug("%s(), %s\n",
  970. __func__, dongle_types[dongle_id]);
  971. switch_bank(iobase, BANK0);
  972. outb(0x62, iobase+MCR);
  973. break;
  974. default:
  975. pr_debug("%s(), invalid dongle_id %#x",
  976. __func__, dongle_id);
  977. }
  978. /* IRCFG1: IRSL1 and 2 are set to IrDA mode */
  979. outb(0x00, iobase+4);
  980. /* Restore bank register */
  981. outb(bank, iobase+BSR);
  982. } /* set_up_dongle_interface */
  983. /*
  984. * Function nsc_ircc_change_dongle_speed (iobase, speed, dongle_id)
  985. *
  986. * Change speed of the attach dongle
  987. *
  988. */
  989. static void nsc_ircc_change_dongle_speed(int iobase, int speed, int dongle_id)
  990. {
  991. __u8 bank;
  992. /* Save current bank */
  993. bank = inb(iobase+BSR);
  994. /* Select Bank 7 */
  995. switch_bank(iobase, BANK7);
  996. /* IRCFG1: set according to dongle_id */
  997. switch (dongle_id) {
  998. case 0x00: /* same as */
  999. case 0x01: /* Differential serial interface */
  1000. pr_debug("%s(), %s not defined by irda yet\n",
  1001. __func__, dongle_types[dongle_id]);
  1002. break;
  1003. case 0x02: /* same as */
  1004. case 0x03: /* Reserved */
  1005. pr_debug("%s(), %s not defined by irda yet\n",
  1006. __func__, dongle_types[dongle_id]);
  1007. break;
  1008. case 0x04: /* Sharp RY5HD01 */
  1009. break;
  1010. case 0x05: /* Reserved */
  1011. pr_debug("%s(), %s not defined by irda yet\n",
  1012. __func__, dongle_types[dongle_id]);
  1013. break;
  1014. case 0x06: /* Single-ended serial interface */
  1015. pr_debug("%s(), %s not defined by irda yet\n",
  1016. __func__, dongle_types[dongle_id]);
  1017. break;
  1018. case 0x07: /* Consumer-IR only */
  1019. pr_debug("%s(), %s is not for IrDA mode\n",
  1020. __func__, dongle_types[dongle_id]);
  1021. break;
  1022. case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
  1023. pr_debug("%s(), %s\n",
  1024. __func__, dongle_types[dongle_id]);
  1025. outb(0x00, iobase+4);
  1026. if (speed > 115200)
  1027. outb(0x01, iobase+4);
  1028. break;
  1029. case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
  1030. outb(0x01, iobase+4);
  1031. if (speed == 4000000) {
  1032. /* There was a cli() there, but we now are already
  1033. * under spin_lock_irqsave() - JeanII */
  1034. outb(0x81, iobase+4);
  1035. outb(0x80, iobase+4);
  1036. } else
  1037. outb(0x00, iobase+4);
  1038. break;
  1039. case 0x0A: /* same as */
  1040. case 0x0B: /* Reserved */
  1041. pr_debug("%s(), %s not defined by irda yet\n",
  1042. __func__, dongle_types[dongle_id]);
  1043. break;
  1044. case 0x0C: /* same as */
  1045. case 0x0D: /* HP HSDL-1100/HSDL-2100 */
  1046. break;
  1047. case 0x0E: /* Supports SIR Mode only */
  1048. break;
  1049. case 0x0F: /* No dongle connected */
  1050. pr_debug("%s(), %s is not for IrDA mode\n",
  1051. __func__, dongle_types[dongle_id]);
  1052. switch_bank(iobase, BANK0);
  1053. outb(0x62, iobase+MCR);
  1054. break;
  1055. default:
  1056. pr_debug("%s(), invalid data_rate\n", __func__);
  1057. }
  1058. /* Restore bank register */
  1059. outb(bank, iobase+BSR);
  1060. }
  1061. /*
  1062. * Function nsc_ircc_change_speed (self, baud)
  1063. *
  1064. * Change the speed of the device
  1065. *
  1066. * This function *must* be called with irq off and spin-lock.
  1067. */
  1068. static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 speed)
  1069. {
  1070. struct net_device *dev;
  1071. __u8 mcr = MCR_SIR;
  1072. int iobase;
  1073. __u8 bank;
  1074. __u8 ier; /* Interrupt enable register */
  1075. pr_debug("%s(), speed=%d\n", __func__, speed);
  1076. IRDA_ASSERT(self != NULL, return 0;);
  1077. dev = self->netdev;
  1078. iobase = self->io.fir_base;
  1079. /* Update accounting for new speed */
  1080. self->io.speed = speed;
  1081. /* Save current bank */
  1082. bank = inb(iobase+BSR);
  1083. /* Disable interrupts */
  1084. switch_bank(iobase, BANK0);
  1085. outb(0, iobase+IER);
  1086. /* Select Bank 2 */
  1087. switch_bank(iobase, BANK2);
  1088. outb(0x00, iobase+BGDH);
  1089. switch (speed) {
  1090. case 9600: outb(0x0c, iobase+BGDL); break;
  1091. case 19200: outb(0x06, iobase+BGDL); break;
  1092. case 38400: outb(0x03, iobase+BGDL); break;
  1093. case 57600: outb(0x02, iobase+BGDL); break;
  1094. case 115200: outb(0x01, iobase+BGDL); break;
  1095. case 576000:
  1096. switch_bank(iobase, BANK5);
  1097. /* IRCR2: MDRS is set */
  1098. outb(inb(iobase+4) | 0x04, iobase+4);
  1099. mcr = MCR_MIR;
  1100. pr_debug("%s(), handling baud of 576000\n", __func__);
  1101. break;
  1102. case 1152000:
  1103. mcr = MCR_MIR;
  1104. pr_debug("%s(), handling baud of 1152000\n", __func__);
  1105. break;
  1106. case 4000000:
  1107. mcr = MCR_FIR;
  1108. pr_debug("%s(), handling baud of 4000000\n", __func__);
  1109. break;
  1110. default:
  1111. mcr = MCR_FIR;
  1112. pr_debug("%s(), unknown baud rate of %d\n",
  1113. __func__, speed);
  1114. break;
  1115. }
  1116. /* Set appropriate speed mode */
  1117. switch_bank(iobase, BANK0);
  1118. outb(mcr | MCR_TX_DFR, iobase+MCR);
  1119. /* Give some hits to the transceiver */
  1120. nsc_ircc_change_dongle_speed(iobase, speed, self->io.dongle_id);
  1121. /* Set FIFO threshold to TX17, RX16 */
  1122. switch_bank(iobase, BANK0);
  1123. outb(0x00, iobase+FCR);
  1124. outb(FCR_FIFO_EN, iobase+FCR);
  1125. outb(FCR_RXTH| /* Set Rx FIFO threshold */
  1126. FCR_TXTH| /* Set Tx FIFO threshold */
  1127. FCR_TXSR| /* Reset Tx FIFO */
  1128. FCR_RXSR| /* Reset Rx FIFO */
  1129. FCR_FIFO_EN, /* Enable FIFOs */
  1130. iobase+FCR);
  1131. /* Set FIFO size to 32 */
  1132. switch_bank(iobase, BANK2);
  1133. outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
  1134. /* Enable some interrupts so we can receive frames */
  1135. switch_bank(iobase, BANK0);
  1136. if (speed > 115200) {
  1137. /* Install FIR xmit handler */
  1138. dev->netdev_ops = &nsc_ircc_fir_ops;
  1139. ier = IER_SFIF_IE;
  1140. nsc_ircc_dma_receive(self);
  1141. } else {
  1142. /* Install SIR xmit handler */
  1143. dev->netdev_ops = &nsc_ircc_sir_ops;
  1144. ier = IER_RXHDL_IE;
  1145. }
  1146. /* Set our current interrupt mask */
  1147. outb(ier, iobase+IER);
  1148. /* Restore BSR */
  1149. outb(bank, iobase+BSR);
  1150. /* Make sure interrupt handlers keep the proper interrupt mask */
  1151. return ier;
  1152. }
  1153. /*
  1154. * Function nsc_ircc_hard_xmit (skb, dev)
  1155. *
  1156. * Transmit the frame!
  1157. *
  1158. */
  1159. static netdev_tx_t nsc_ircc_hard_xmit_sir(struct sk_buff *skb,
  1160. struct net_device *dev)
  1161. {
  1162. struct nsc_ircc_cb *self;
  1163. unsigned long flags;
  1164. int iobase;
  1165. __s32 speed;
  1166. __u8 bank;
  1167. self = netdev_priv(dev);
  1168. IRDA_ASSERT(self != NULL, return NETDEV_TX_OK;);
  1169. iobase = self->io.fir_base;
  1170. netif_stop_queue(dev);
  1171. /* Make sure tests *& speed change are atomic */
  1172. spin_lock_irqsave(&self->lock, flags);
  1173. /* Check if we need to change the speed */
  1174. speed = irda_get_next_speed(skb);
  1175. if ((speed != self->io.speed) && (speed != -1)) {
  1176. /* Check for empty frame. */
  1177. if (!skb->len) {
  1178. /* If we just sent a frame, we get called before
  1179. * the last bytes get out (because of the SIR FIFO).
  1180. * If this is the case, let interrupt handler change
  1181. * the speed itself... Jean II */
  1182. if (self->io.direction == IO_RECV) {
  1183. nsc_ircc_change_speed(self, speed);
  1184. /* TODO : For SIR->SIR, the next packet
  1185. * may get corrupted - Jean II */
  1186. netif_wake_queue(dev);
  1187. } else {
  1188. self->new_speed = speed;
  1189. /* Queue will be restarted after speed change
  1190. * to make sure packets gets through the
  1191. * proper xmit handler - Jean II */
  1192. }
  1193. netif_trans_update(dev);
  1194. spin_unlock_irqrestore(&self->lock, flags);
  1195. dev_kfree_skb(skb);
  1196. return NETDEV_TX_OK;
  1197. } else
  1198. self->new_speed = speed;
  1199. }
  1200. /* Save current bank */
  1201. bank = inb(iobase+BSR);
  1202. self->tx_buff.data = self->tx_buff.head;
  1203. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  1204. self->tx_buff.truesize);
  1205. dev->stats.tx_bytes += self->tx_buff.len;
  1206. /* Add interrupt on tx low level (will fire immediately) */
  1207. switch_bank(iobase, BANK0);
  1208. outb(IER_TXLDL_IE, iobase+IER);
  1209. /* Restore bank register */
  1210. outb(bank, iobase+BSR);
  1211. netif_trans_update(dev);
  1212. spin_unlock_irqrestore(&self->lock, flags);
  1213. dev_kfree_skb(skb);
  1214. return NETDEV_TX_OK;
  1215. }
  1216. static netdev_tx_t nsc_ircc_hard_xmit_fir(struct sk_buff *skb,
  1217. struct net_device *dev)
  1218. {
  1219. struct nsc_ircc_cb *self;
  1220. unsigned long flags;
  1221. int iobase;
  1222. __s32 speed;
  1223. __u8 bank;
  1224. int mtt, diff;
  1225. self = netdev_priv(dev);
  1226. iobase = self->io.fir_base;
  1227. netif_stop_queue(dev);
  1228. /* Make sure tests *& speed change are atomic */
  1229. spin_lock_irqsave(&self->lock, flags);
  1230. /* Check if we need to change the speed */
  1231. speed = irda_get_next_speed(skb);
  1232. if ((speed != self->io.speed) && (speed != -1)) {
  1233. /* Check for empty frame. */
  1234. if (!skb->len) {
  1235. /* If we are currently transmitting, defer to
  1236. * interrupt handler. - Jean II */
  1237. if(self->tx_fifo.len == 0) {
  1238. nsc_ircc_change_speed(self, speed);
  1239. netif_wake_queue(dev);
  1240. } else {
  1241. self->new_speed = speed;
  1242. /* Keep queue stopped :
  1243. * the speed change operation may change the
  1244. * xmit handler, and we want to make sure
  1245. * the next packet get through the proper
  1246. * Tx path, so block the Tx queue until
  1247. * the speed change has been done.
  1248. * Jean II */
  1249. }
  1250. netif_trans_update(dev);
  1251. spin_unlock_irqrestore(&self->lock, flags);
  1252. dev_kfree_skb(skb);
  1253. return NETDEV_TX_OK;
  1254. } else {
  1255. /* Change speed after current frame */
  1256. self->new_speed = speed;
  1257. }
  1258. }
  1259. /* Save current bank */
  1260. bank = inb(iobase+BSR);
  1261. /* Register and copy this frame to DMA memory */
  1262. self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
  1263. self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
  1264. self->tx_fifo.tail += skb->len;
  1265. dev->stats.tx_bytes += skb->len;
  1266. skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
  1267. skb->len);
  1268. self->tx_fifo.len++;
  1269. self->tx_fifo.free++;
  1270. /* Start transmit only if there is currently no transmit going on */
  1271. if (self->tx_fifo.len == 1) {
  1272. /* Check if we must wait the min turn time or not */
  1273. mtt = irda_get_mtt(skb);
  1274. if (mtt) {
  1275. /* Check how much time we have used already */
  1276. diff = ktime_us_delta(ktime_get(), self->stamp);
  1277. /* Check if the mtt is larger than the time we have
  1278. * already used by all the protocol processing
  1279. */
  1280. if (mtt > diff) {
  1281. mtt -= diff;
  1282. /*
  1283. * Use timer if delay larger than 125 us, and
  1284. * use udelay for smaller values which should
  1285. * be acceptable
  1286. */
  1287. if (mtt > 125) {
  1288. /* Adjust for timer resolution */
  1289. mtt = mtt / 125;
  1290. /* Setup timer */
  1291. switch_bank(iobase, BANK4);
  1292. outb(mtt & 0xff, iobase+TMRL);
  1293. outb((mtt >> 8) & 0x0f, iobase+TMRH);
  1294. /* Start timer */
  1295. outb(IRCR1_TMR_EN, iobase+IRCR1);
  1296. self->io.direction = IO_XMIT;
  1297. /* Enable timer interrupt */
  1298. switch_bank(iobase, BANK0);
  1299. outb(IER_TMR_IE, iobase+IER);
  1300. /* Timer will take care of the rest */
  1301. goto out;
  1302. } else
  1303. udelay(mtt);
  1304. }
  1305. }
  1306. /* Enable DMA interrupt */
  1307. switch_bank(iobase, BANK0);
  1308. outb(IER_DMA_IE, iobase+IER);
  1309. /* Transmit frame */
  1310. nsc_ircc_dma_xmit(self, iobase);
  1311. }
  1312. out:
  1313. /* Not busy transmitting anymore if window is not full,
  1314. * and if we don't need to change speed */
  1315. if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0))
  1316. netif_wake_queue(self->netdev);
  1317. /* Restore bank register */
  1318. outb(bank, iobase+BSR);
  1319. netif_trans_update(dev);
  1320. spin_unlock_irqrestore(&self->lock, flags);
  1321. dev_kfree_skb(skb);
  1322. return NETDEV_TX_OK;
  1323. }
  1324. /*
  1325. * Function nsc_ircc_dma_xmit (self, iobase)
  1326. *
  1327. * Transmit data using DMA
  1328. *
  1329. */
  1330. static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase)
  1331. {
  1332. int bsr;
  1333. /* Save current bank */
  1334. bsr = inb(iobase+BSR);
  1335. /* Disable DMA */
  1336. switch_bank(iobase, BANK0);
  1337. outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
  1338. self->io.direction = IO_XMIT;
  1339. /* Choose transmit DMA channel */
  1340. switch_bank(iobase, BANK2);
  1341. outb(ECR1_DMASWP|ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
  1342. irda_setup_dma(self->io.dma,
  1343. ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
  1344. self->tx_buff.head) + self->tx_buff_dma,
  1345. self->tx_fifo.queue[self->tx_fifo.ptr].len,
  1346. DMA_TX_MODE);
  1347. /* Enable DMA and SIR interaction pulse */
  1348. switch_bank(iobase, BANK0);
  1349. outb(inb(iobase+MCR)|MCR_TX_DFR|MCR_DMA_EN|MCR_IR_PLS, iobase+MCR);
  1350. /* Restore bank register */
  1351. outb(bsr, iobase+BSR);
  1352. }
  1353. /*
  1354. * Function nsc_ircc_pio_xmit (self, iobase)
  1355. *
  1356. * Transmit data using PIO. Returns the number of bytes that actually
  1357. * got transferred
  1358. *
  1359. */
  1360. static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
  1361. {
  1362. int actual = 0;
  1363. __u8 bank;
  1364. /* Save current bank */
  1365. bank = inb(iobase+BSR);
  1366. switch_bank(iobase, BANK0);
  1367. if (!(inb_p(iobase+LSR) & LSR_TXEMP)) {
  1368. pr_debug("%s(), warning, FIFO not empty yet!\n",
  1369. __func__);
  1370. /* FIFO may still be filled to the Tx interrupt threshold */
  1371. fifo_size -= 17;
  1372. }
  1373. /* Fill FIFO with current frame */
  1374. while ((fifo_size-- > 0) && (actual < len)) {
  1375. /* Transmit next byte */
  1376. outb(buf[actual++], iobase+TXD);
  1377. }
  1378. pr_debug("%s(), fifo_size %d ; %d sent of %d\n",
  1379. __func__, fifo_size, actual, len);
  1380. /* Restore bank */
  1381. outb(bank, iobase+BSR);
  1382. return actual;
  1383. }
  1384. /*
  1385. * Function nsc_ircc_dma_xmit_complete (self)
  1386. *
  1387. * The transfer of a frame in finished. This function will only be called
  1388. * by the interrupt handler
  1389. *
  1390. */
  1391. static int nsc_ircc_dma_xmit_complete(struct nsc_ircc_cb *self)
  1392. {
  1393. int iobase;
  1394. __u8 bank;
  1395. int ret = TRUE;
  1396. iobase = self->io.fir_base;
  1397. /* Save current bank */
  1398. bank = inb(iobase+BSR);
  1399. /* Disable DMA */
  1400. switch_bank(iobase, BANK0);
  1401. outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
  1402. /* Check for underrun! */
  1403. if (inb(iobase+ASCR) & ASCR_TXUR) {
  1404. self->netdev->stats.tx_errors++;
  1405. self->netdev->stats.tx_fifo_errors++;
  1406. /* Clear bit, by writing 1 into it */
  1407. outb(ASCR_TXUR, iobase+ASCR);
  1408. } else {
  1409. self->netdev->stats.tx_packets++;
  1410. }
  1411. /* Finished with this frame, so prepare for next */
  1412. self->tx_fifo.ptr++;
  1413. self->tx_fifo.len--;
  1414. /* Any frames to be sent back-to-back? */
  1415. if (self->tx_fifo.len) {
  1416. nsc_ircc_dma_xmit(self, iobase);
  1417. /* Not finished yet! */
  1418. ret = FALSE;
  1419. } else {
  1420. /* Reset Tx FIFO info */
  1421. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1422. self->tx_fifo.tail = self->tx_buff.head;
  1423. }
  1424. /* Make sure we have room for more frames and
  1425. * that we don't need to change speed */
  1426. if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0)) {
  1427. /* Not busy transmitting anymore */
  1428. /* Tell the network layer, that we can accept more frames */
  1429. netif_wake_queue(self->netdev);
  1430. }
  1431. /* Restore bank */
  1432. outb(bank, iobase+BSR);
  1433. return ret;
  1434. }
  1435. /*
  1436. * Function nsc_ircc_dma_receive (self)
  1437. *
  1438. * Get ready for receiving a frame. The device will initiate a DMA
  1439. * if it starts to receive a frame.
  1440. *
  1441. */
  1442. static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self)
  1443. {
  1444. int iobase;
  1445. __u8 bsr;
  1446. iobase = self->io.fir_base;
  1447. /* Reset Tx FIFO info */
  1448. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1449. self->tx_fifo.tail = self->tx_buff.head;
  1450. /* Save current bank */
  1451. bsr = inb(iobase+BSR);
  1452. /* Disable DMA */
  1453. switch_bank(iobase, BANK0);
  1454. outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
  1455. /* Choose DMA Rx, DMA Fairness, and Advanced mode */
  1456. switch_bank(iobase, BANK2);
  1457. outb(ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
  1458. self->io.direction = IO_RECV;
  1459. self->rx_buff.data = self->rx_buff.head;
  1460. /* Reset Rx FIFO. This will also flush the ST_FIFO */
  1461. switch_bank(iobase, BANK0);
  1462. outb(FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
  1463. self->st_fifo.len = self->st_fifo.pending_bytes = 0;
  1464. self->st_fifo.tail = self->st_fifo.head = 0;
  1465. irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
  1466. DMA_RX_MODE);
  1467. /* Enable DMA */
  1468. switch_bank(iobase, BANK0);
  1469. outb(inb(iobase+MCR)|MCR_DMA_EN, iobase+MCR);
  1470. /* Restore bank register */
  1471. outb(bsr, iobase+BSR);
  1472. return 0;
  1473. }
  1474. /*
  1475. * Function nsc_ircc_dma_receive_complete (self)
  1476. *
  1477. * Finished with receiving frames
  1478. *
  1479. *
  1480. */
  1481. static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase)
  1482. {
  1483. struct st_fifo *st_fifo;
  1484. struct sk_buff *skb;
  1485. __u8 status;
  1486. __u8 bank;
  1487. int len;
  1488. st_fifo = &self->st_fifo;
  1489. /* Save current bank */
  1490. bank = inb(iobase+BSR);
  1491. /* Read all entries in status FIFO */
  1492. switch_bank(iobase, BANK5);
  1493. while ((status = inb(iobase+FRM_ST)) & FRM_ST_VLD) {
  1494. /* We must empty the status FIFO no matter what */
  1495. len = inb(iobase+RFLFL) | ((inb(iobase+RFLFH) & 0x1f) << 8);
  1496. if (st_fifo->tail >= MAX_RX_WINDOW) {
  1497. pr_debug("%s(), window is full!\n", __func__);
  1498. continue;
  1499. }
  1500. st_fifo->entries[st_fifo->tail].status = status;
  1501. st_fifo->entries[st_fifo->tail].len = len;
  1502. st_fifo->pending_bytes += len;
  1503. st_fifo->tail++;
  1504. st_fifo->len++;
  1505. }
  1506. /* Try to process all entries in status FIFO */
  1507. while (st_fifo->len > 0) {
  1508. /* Get first entry */
  1509. status = st_fifo->entries[st_fifo->head].status;
  1510. len = st_fifo->entries[st_fifo->head].len;
  1511. st_fifo->pending_bytes -= len;
  1512. st_fifo->head++;
  1513. st_fifo->len--;
  1514. /* Check for errors */
  1515. if (status & FRM_ST_ERR_MSK) {
  1516. if (status & FRM_ST_LOST_FR) {
  1517. /* Add number of lost frames to stats */
  1518. self->netdev->stats.rx_errors += len;
  1519. } else {
  1520. /* Skip frame */
  1521. self->netdev->stats.rx_errors++;
  1522. self->rx_buff.data += len;
  1523. if (status & FRM_ST_MAX_LEN)
  1524. self->netdev->stats.rx_length_errors++;
  1525. if (status & FRM_ST_PHY_ERR)
  1526. self->netdev->stats.rx_frame_errors++;
  1527. if (status & FRM_ST_BAD_CRC)
  1528. self->netdev->stats.rx_crc_errors++;
  1529. }
  1530. /* The errors below can be reported in both cases */
  1531. if (status & FRM_ST_OVR1)
  1532. self->netdev->stats.rx_fifo_errors++;
  1533. if (status & FRM_ST_OVR2)
  1534. self->netdev->stats.rx_fifo_errors++;
  1535. } else {
  1536. /*
  1537. * First we must make sure that the frame we
  1538. * want to deliver is all in main memory. If we
  1539. * cannot tell, then we check if the Rx FIFO is
  1540. * empty. If not then we will have to take a nap
  1541. * and try again later.
  1542. */
  1543. if (st_fifo->pending_bytes < self->io.fifo_size) {
  1544. switch_bank(iobase, BANK0);
  1545. if (inb(iobase+LSR) & LSR_RXDA) {
  1546. /* Put this entry back in fifo */
  1547. st_fifo->head--;
  1548. st_fifo->len++;
  1549. st_fifo->pending_bytes += len;
  1550. st_fifo->entries[st_fifo->head].status = status;
  1551. st_fifo->entries[st_fifo->head].len = len;
  1552. /*
  1553. * DMA not finished yet, so try again
  1554. * later, set timer value, resolution
  1555. * 125 us
  1556. */
  1557. switch_bank(iobase, BANK4);
  1558. outb(0x02, iobase+TMRL); /* x 125 us */
  1559. outb(0x00, iobase+TMRH);
  1560. /* Start timer */
  1561. outb(IRCR1_TMR_EN, iobase+IRCR1);
  1562. /* Restore bank register */
  1563. outb(bank, iobase+BSR);
  1564. return FALSE; /* I'll be back! */
  1565. }
  1566. }
  1567. /*
  1568. * Remember the time we received this frame, so we can
  1569. * reduce the min turn time a bit since we will know
  1570. * how much time we have used for protocol processing
  1571. */
  1572. self->stamp = ktime_get();
  1573. skb = dev_alloc_skb(len+1);
  1574. if (skb == NULL) {
  1575. self->netdev->stats.rx_dropped++;
  1576. /* Restore bank register */
  1577. outb(bank, iobase+BSR);
  1578. return FALSE;
  1579. }
  1580. /* Make sure IP header gets aligned */
  1581. skb_reserve(skb, 1);
  1582. /* Copy frame without CRC */
  1583. if (self->io.speed < 4000000) {
  1584. skb_put(skb, len-2);
  1585. skb_copy_to_linear_data(skb,
  1586. self->rx_buff.data,
  1587. len - 2);
  1588. } else {
  1589. skb_put(skb, len-4);
  1590. skb_copy_to_linear_data(skb,
  1591. self->rx_buff.data,
  1592. len - 4);
  1593. }
  1594. /* Move to next frame */
  1595. self->rx_buff.data += len;
  1596. self->netdev->stats.rx_bytes += len;
  1597. self->netdev->stats.rx_packets++;
  1598. skb->dev = self->netdev;
  1599. skb_reset_mac_header(skb);
  1600. skb->protocol = htons(ETH_P_IRDA);
  1601. netif_rx(skb);
  1602. }
  1603. }
  1604. /* Restore bank register */
  1605. outb(bank, iobase+BSR);
  1606. return TRUE;
  1607. }
  1608. /*
  1609. * Function nsc_ircc_pio_receive (self)
  1610. *
  1611. * Receive all data in receiver FIFO
  1612. *
  1613. */
  1614. static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self)
  1615. {
  1616. __u8 byte;
  1617. int iobase;
  1618. iobase = self->io.fir_base;
  1619. /* Receive all characters in Rx FIFO */
  1620. do {
  1621. byte = inb(iobase+RXD);
  1622. async_unwrap_char(self->netdev, &self->netdev->stats,
  1623. &self->rx_buff, byte);
  1624. } while (inb(iobase+LSR) & LSR_RXDA); /* Data available */
  1625. }
  1626. /*
  1627. * Function nsc_ircc_sir_interrupt (self, eir)
  1628. *
  1629. * Handle SIR interrupt
  1630. *
  1631. */
  1632. static void nsc_ircc_sir_interrupt(struct nsc_ircc_cb *self, int eir)
  1633. {
  1634. int actual;
  1635. /* Check if transmit FIFO is low on data */
  1636. if (eir & EIR_TXLDL_EV) {
  1637. /* Write data left in transmit buffer */
  1638. actual = nsc_ircc_pio_write(self->io.fir_base,
  1639. self->tx_buff.data,
  1640. self->tx_buff.len,
  1641. self->io.fifo_size);
  1642. self->tx_buff.data += actual;
  1643. self->tx_buff.len -= actual;
  1644. self->io.direction = IO_XMIT;
  1645. /* Check if finished */
  1646. if (self->tx_buff.len > 0)
  1647. self->ier = IER_TXLDL_IE;
  1648. else {
  1649. self->netdev->stats.tx_packets++;
  1650. netif_wake_queue(self->netdev);
  1651. self->ier = IER_TXEMP_IE;
  1652. }
  1653. }
  1654. /* Check if transmission has completed */
  1655. if (eir & EIR_TXEMP_EV) {
  1656. /* Turn around and get ready to receive some data */
  1657. self->io.direction = IO_RECV;
  1658. self->ier = IER_RXHDL_IE;
  1659. /* Check if we need to change the speed?
  1660. * Need to be after self->io.direction to avoid race with
  1661. * nsc_ircc_hard_xmit_sir() - Jean II */
  1662. if (self->new_speed) {
  1663. pr_debug("%s(), Changing speed!\n", __func__);
  1664. self->ier = nsc_ircc_change_speed(self,
  1665. self->new_speed);
  1666. self->new_speed = 0;
  1667. netif_wake_queue(self->netdev);
  1668. /* Check if we are going to FIR */
  1669. if (self->io.speed > 115200) {
  1670. /* No need to do anymore SIR stuff */
  1671. return;
  1672. }
  1673. }
  1674. }
  1675. /* Rx FIFO threshold or timeout */
  1676. if (eir & EIR_RXHDL_EV) {
  1677. nsc_ircc_pio_receive(self);
  1678. /* Keep receiving */
  1679. self->ier = IER_RXHDL_IE;
  1680. }
  1681. }
  1682. /*
  1683. * Function nsc_ircc_fir_interrupt (self, eir)
  1684. *
  1685. * Handle MIR/FIR interrupt
  1686. *
  1687. */
  1688. static void nsc_ircc_fir_interrupt(struct nsc_ircc_cb *self, int iobase,
  1689. int eir)
  1690. {
  1691. __u8 bank;
  1692. bank = inb(iobase+BSR);
  1693. /* Status FIFO event*/
  1694. if (eir & EIR_SFIF_EV) {
  1695. /* Check if DMA has finished */
  1696. if (nsc_ircc_dma_receive_complete(self, iobase)) {
  1697. /* Wait for next status FIFO interrupt */
  1698. self->ier = IER_SFIF_IE;
  1699. } else {
  1700. self->ier = IER_SFIF_IE | IER_TMR_IE;
  1701. }
  1702. } else if (eir & EIR_TMR_EV) { /* Timer finished */
  1703. /* Disable timer */
  1704. switch_bank(iobase, BANK4);
  1705. outb(0, iobase+IRCR1);
  1706. /* Clear timer event */
  1707. switch_bank(iobase, BANK0);
  1708. outb(ASCR_CTE, iobase+ASCR);
  1709. /* Check if this is a Tx timer interrupt */
  1710. if (self->io.direction == IO_XMIT) {
  1711. nsc_ircc_dma_xmit(self, iobase);
  1712. /* Interrupt on DMA */
  1713. self->ier = IER_DMA_IE;
  1714. } else {
  1715. /* Check (again) if DMA has finished */
  1716. if (nsc_ircc_dma_receive_complete(self, iobase)) {
  1717. self->ier = IER_SFIF_IE;
  1718. } else {
  1719. self->ier = IER_SFIF_IE | IER_TMR_IE;
  1720. }
  1721. }
  1722. } else if (eir & EIR_DMA_EV) {
  1723. /* Finished with all transmissions? */
  1724. if (nsc_ircc_dma_xmit_complete(self)) {
  1725. if(self->new_speed != 0) {
  1726. /* As we stop the Tx queue, the speed change
  1727. * need to be done when the Tx fifo is
  1728. * empty. Ask for a Tx done interrupt */
  1729. self->ier = IER_TXEMP_IE;
  1730. } else {
  1731. /* Check if there are more frames to be
  1732. * transmitted */
  1733. if (irda_device_txqueue_empty(self->netdev)) {
  1734. /* Prepare for receive */
  1735. nsc_ircc_dma_receive(self);
  1736. self->ier = IER_SFIF_IE;
  1737. } else
  1738. net_warn_ratelimited("%s(), potential Tx queue lockup !\n",
  1739. __func__);
  1740. }
  1741. } else {
  1742. /* Not finished yet, so interrupt on DMA again */
  1743. self->ier = IER_DMA_IE;
  1744. }
  1745. } else if (eir & EIR_TXEMP_EV) {
  1746. /* The Tx FIFO has totally drained out, so now we can change
  1747. * the speed... - Jean II */
  1748. self->ier = nsc_ircc_change_speed(self, self->new_speed);
  1749. self->new_speed = 0;
  1750. netif_wake_queue(self->netdev);
  1751. /* Note : nsc_ircc_change_speed() restarted Rx fifo */
  1752. }
  1753. outb(bank, iobase+BSR);
  1754. }
  1755. /*
  1756. * Function nsc_ircc_interrupt (irq, dev_id, regs)
  1757. *
  1758. * An interrupt from the chip has arrived. Time to do some work
  1759. *
  1760. */
  1761. static irqreturn_t nsc_ircc_interrupt(int irq, void *dev_id)
  1762. {
  1763. struct net_device *dev = dev_id;
  1764. struct nsc_ircc_cb *self;
  1765. __u8 bsr, eir;
  1766. int iobase;
  1767. self = netdev_priv(dev);
  1768. spin_lock(&self->lock);
  1769. iobase = self->io.fir_base;
  1770. bsr = inb(iobase+BSR); /* Save current bank */
  1771. switch_bank(iobase, BANK0);
  1772. self->ier = inb(iobase+IER);
  1773. eir = inb(iobase+EIR) & self->ier; /* Mask out the interesting ones */
  1774. outb(0, iobase+IER); /* Disable interrupts */
  1775. if (eir) {
  1776. /* Dispatch interrupt handler for the current speed */
  1777. if (self->io.speed > 115200)
  1778. nsc_ircc_fir_interrupt(self, iobase, eir);
  1779. else
  1780. nsc_ircc_sir_interrupt(self, eir);
  1781. }
  1782. outb(self->ier, iobase+IER); /* Restore interrupts */
  1783. outb(bsr, iobase+BSR); /* Restore bank register */
  1784. spin_unlock(&self->lock);
  1785. return IRQ_RETVAL(eir);
  1786. }
  1787. /*
  1788. * Function nsc_ircc_is_receiving (self)
  1789. *
  1790. * Return TRUE is we are currently receiving a frame
  1791. *
  1792. */
  1793. static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self)
  1794. {
  1795. unsigned long flags;
  1796. int status = FALSE;
  1797. int iobase;
  1798. __u8 bank;
  1799. IRDA_ASSERT(self != NULL, return FALSE;);
  1800. spin_lock_irqsave(&self->lock, flags);
  1801. if (self->io.speed > 115200) {
  1802. iobase = self->io.fir_base;
  1803. /* Check if rx FIFO is not empty */
  1804. bank = inb(iobase+BSR);
  1805. switch_bank(iobase, BANK2);
  1806. if ((inb(iobase+RXFLV) & 0x3f) != 0) {
  1807. /* We are receiving something */
  1808. status = TRUE;
  1809. }
  1810. outb(bank, iobase+BSR);
  1811. } else
  1812. status = (self->rx_buff.state != OUTSIDE_FRAME);
  1813. spin_unlock_irqrestore(&self->lock, flags);
  1814. return status;
  1815. }
  1816. /*
  1817. * Function nsc_ircc_net_open (dev)
  1818. *
  1819. * Start the device
  1820. *
  1821. */
  1822. static int nsc_ircc_net_open(struct net_device *dev)
  1823. {
  1824. struct nsc_ircc_cb *self;
  1825. int iobase;
  1826. char hwname[32];
  1827. __u8 bank;
  1828. IRDA_ASSERT(dev != NULL, return -1;);
  1829. self = netdev_priv(dev);
  1830. IRDA_ASSERT(self != NULL, return 0;);
  1831. iobase = self->io.fir_base;
  1832. if (request_irq(self->io.irq, nsc_ircc_interrupt, 0, dev->name, dev)) {
  1833. net_warn_ratelimited("%s, unable to allocate irq=%d\n",
  1834. driver_name, self->io.irq);
  1835. return -EAGAIN;
  1836. }
  1837. /*
  1838. * Always allocate the DMA channel after the IRQ, and clean up on
  1839. * failure.
  1840. */
  1841. if (request_dma(self->io.dma, dev->name)) {
  1842. net_warn_ratelimited("%s, unable to allocate dma=%d\n",
  1843. driver_name, self->io.dma);
  1844. free_irq(self->io.irq, dev);
  1845. return -EAGAIN;
  1846. }
  1847. /* Save current bank */
  1848. bank = inb(iobase+BSR);
  1849. /* turn on interrupts */
  1850. switch_bank(iobase, BANK0);
  1851. outb(IER_LS_IE | IER_RXHDL_IE, iobase+IER);
  1852. /* Restore bank register */
  1853. outb(bank, iobase+BSR);
  1854. /* Ready to play! */
  1855. netif_start_queue(dev);
  1856. /* Give self a hardware name */
  1857. sprintf(hwname, "NSC-FIR @ 0x%03x", self->io.fir_base);
  1858. /*
  1859. * Open new IrLAP layer instance, now that everything should be
  1860. * initialized properly
  1861. */
  1862. self->irlap = irlap_open(dev, &self->qos, hwname);
  1863. return 0;
  1864. }
  1865. /*
  1866. * Function nsc_ircc_net_close (dev)
  1867. *
  1868. * Stop the device
  1869. *
  1870. */
  1871. static int nsc_ircc_net_close(struct net_device *dev)
  1872. {
  1873. struct nsc_ircc_cb *self;
  1874. int iobase;
  1875. __u8 bank;
  1876. IRDA_ASSERT(dev != NULL, return -1;);
  1877. self = netdev_priv(dev);
  1878. IRDA_ASSERT(self != NULL, return 0;);
  1879. /* Stop device */
  1880. netif_stop_queue(dev);
  1881. /* Stop and remove instance of IrLAP */
  1882. if (self->irlap)
  1883. irlap_close(self->irlap);
  1884. self->irlap = NULL;
  1885. iobase = self->io.fir_base;
  1886. disable_dma(self->io.dma);
  1887. /* Save current bank */
  1888. bank = inb(iobase+BSR);
  1889. /* Disable interrupts */
  1890. switch_bank(iobase, BANK0);
  1891. outb(0, iobase+IER);
  1892. free_irq(self->io.irq, dev);
  1893. free_dma(self->io.dma);
  1894. /* Restore bank register */
  1895. outb(bank, iobase+BSR);
  1896. return 0;
  1897. }
  1898. /*
  1899. * Function nsc_ircc_net_ioctl (dev, rq, cmd)
  1900. *
  1901. * Process IOCTL commands for this device
  1902. *
  1903. */
  1904. static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1905. {
  1906. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1907. struct nsc_ircc_cb *self;
  1908. unsigned long flags;
  1909. int ret = 0;
  1910. IRDA_ASSERT(dev != NULL, return -1;);
  1911. self = netdev_priv(dev);
  1912. IRDA_ASSERT(self != NULL, return -1;);
  1913. pr_debug("%s(), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
  1914. switch (cmd) {
  1915. case SIOCSBANDWIDTH: /* Set bandwidth */
  1916. if (!capable(CAP_NET_ADMIN)) {
  1917. ret = -EPERM;
  1918. break;
  1919. }
  1920. spin_lock_irqsave(&self->lock, flags);
  1921. nsc_ircc_change_speed(self, irq->ifr_baudrate);
  1922. spin_unlock_irqrestore(&self->lock, flags);
  1923. break;
  1924. case SIOCSMEDIABUSY: /* Set media busy */
  1925. if (!capable(CAP_NET_ADMIN)) {
  1926. ret = -EPERM;
  1927. break;
  1928. }
  1929. irda_device_set_media_busy(self->netdev, TRUE);
  1930. break;
  1931. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1932. /* This is already protected */
  1933. irq->ifr_receiving = nsc_ircc_is_receiving(self);
  1934. break;
  1935. default:
  1936. ret = -EOPNOTSUPP;
  1937. }
  1938. return ret;
  1939. }
  1940. static int nsc_ircc_suspend(struct platform_device *dev, pm_message_t state)
  1941. {
  1942. struct nsc_ircc_cb *self = platform_get_drvdata(dev);
  1943. int bank;
  1944. unsigned long flags;
  1945. int iobase = self->io.fir_base;
  1946. if (self->io.suspended)
  1947. return 0;
  1948. pr_debug("%s, Suspending\n", driver_name);
  1949. rtnl_lock();
  1950. if (netif_running(self->netdev)) {
  1951. netif_device_detach(self->netdev);
  1952. spin_lock_irqsave(&self->lock, flags);
  1953. /* Save current bank */
  1954. bank = inb(iobase+BSR);
  1955. /* Disable interrupts */
  1956. switch_bank(iobase, BANK0);
  1957. outb(0, iobase+IER);
  1958. /* Restore bank register */
  1959. outb(bank, iobase+BSR);
  1960. spin_unlock_irqrestore(&self->lock, flags);
  1961. free_irq(self->io.irq, self->netdev);
  1962. disable_dma(self->io.dma);
  1963. }
  1964. self->io.suspended = 1;
  1965. rtnl_unlock();
  1966. return 0;
  1967. }
  1968. static int nsc_ircc_resume(struct platform_device *dev)
  1969. {
  1970. struct nsc_ircc_cb *self = platform_get_drvdata(dev);
  1971. unsigned long flags;
  1972. if (!self->io.suspended)
  1973. return 0;
  1974. pr_debug("%s, Waking up\n", driver_name);
  1975. rtnl_lock();
  1976. nsc_ircc_setup(&self->io);
  1977. nsc_ircc_init_dongle_interface(self->io.fir_base, self->io.dongle_id);
  1978. if (netif_running(self->netdev)) {
  1979. if (request_irq(self->io.irq, nsc_ircc_interrupt, 0,
  1980. self->netdev->name, self->netdev)) {
  1981. net_warn_ratelimited("%s, unable to allocate irq=%d\n",
  1982. driver_name, self->io.irq);
  1983. /*
  1984. * Don't fail resume process, just kill this
  1985. * network interface
  1986. */
  1987. unregister_netdevice(self->netdev);
  1988. } else {
  1989. spin_lock_irqsave(&self->lock, flags);
  1990. nsc_ircc_change_speed(self, self->io.speed);
  1991. spin_unlock_irqrestore(&self->lock, flags);
  1992. netif_device_attach(self->netdev);
  1993. }
  1994. } else {
  1995. spin_lock_irqsave(&self->lock, flags);
  1996. nsc_ircc_change_speed(self, 9600);
  1997. spin_unlock_irqrestore(&self->lock, flags);
  1998. }
  1999. self->io.suspended = 0;
  2000. rtnl_unlock();
  2001. return 0;
  2002. }
  2003. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  2004. MODULE_DESCRIPTION("NSC IrDA Device Driver");
  2005. MODULE_LICENSE("GPL");
  2006. module_param(qos_mtt_bits, int, 0);
  2007. MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
  2008. module_param_array(io, int, NULL, 0);
  2009. MODULE_PARM_DESC(io, "Base I/O addresses");
  2010. module_param_array(irq, int, NULL, 0);
  2011. MODULE_PARM_DESC(irq, "IRQ lines");
  2012. module_param_array(dma, int, NULL, 0);
  2013. MODULE_PARM_DESC(dma, "DMA channels");
  2014. module_param(dongle_id, int, 0);
  2015. MODULE_PARM_DESC(dongle_id, "Type-id of used dongle");
  2016. module_init(nsc_ircc_init);
  2017. module_exit(nsc_ircc_cleanup);