speedfax.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * speedfax.c low level stuff for Sedlbauer Speedfax+ cards
  3. * based on the ISAR DSP
  4. * Thanks to Sedlbauer AG for informations and HW
  5. *
  6. * Author Karsten Keil <keil@isdn4linux.de>
  7. *
  8. * Copyright 2009 by Karsten Keil <keil@isdn4linux.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/pci.h>
  27. #include <linux/delay.h>
  28. #include <linux/mISDNhw.h>
  29. #include <linux/firmware.h>
  30. #include "ipac.h"
  31. #include "isar.h"
  32. #define SPEEDFAX_REV "2.0"
  33. #define PCI_SUBVENDOR_SPEEDFAX_PYRAMID 0x51
  34. #define PCI_SUBVENDOR_SPEEDFAX_PCI 0x54
  35. #define PCI_SUB_ID_SEDLBAUER 0x01
  36. #define SFAX_PCI_ADDR 0xc8
  37. #define SFAX_PCI_ISAC 0xd0
  38. #define SFAX_PCI_ISAR 0xe0
  39. /* TIGER 100 Registers */
  40. #define TIGER_RESET_ADDR 0x00
  41. #define TIGER_EXTERN_RESET_ON 0x01
  42. #define TIGER_EXTERN_RESET_OFF 0x00
  43. #define TIGER_AUX_CTRL 0x02
  44. #define TIGER_AUX_DATA 0x03
  45. #define TIGER_AUX_IRQMASK 0x05
  46. #define TIGER_AUX_STATUS 0x07
  47. /* Tiger AUX BITs */
  48. #define SFAX_AUX_IOMASK 0xdd /* 1 and 5 are inputs */
  49. #define SFAX_ISAR_RESET_BIT_OFF 0x00
  50. #define SFAX_ISAR_RESET_BIT_ON 0x01
  51. #define SFAX_TIGER_IRQ_BIT 0x02
  52. #define SFAX_LED1_BIT 0x08
  53. #define SFAX_LED2_BIT 0x10
  54. #define SFAX_PCI_RESET_ON (SFAX_ISAR_RESET_BIT_ON)
  55. #define SFAX_PCI_RESET_OFF (SFAX_LED1_BIT | SFAX_LED2_BIT)
  56. static int sfax_cnt;
  57. static u32 debug;
  58. static u32 irqloops = 4;
  59. struct sfax_hw {
  60. struct list_head list;
  61. struct pci_dev *pdev;
  62. char name[MISDN_MAX_IDLEN];
  63. u32 irq;
  64. u32 irqcnt;
  65. u32 cfg;
  66. struct _ioport p_isac;
  67. struct _ioport p_isar;
  68. u8 aux_data;
  69. spinlock_t lock; /* HW access lock */
  70. struct isac_hw isac;
  71. struct isar_hw isar;
  72. };
  73. static LIST_HEAD(Cards);
  74. static DEFINE_RWLOCK(card_lock); /* protect Cards */
  75. static void
  76. _set_debug(struct sfax_hw *card)
  77. {
  78. card->isac.dch.debug = debug;
  79. card->isar.ch[0].bch.debug = debug;
  80. card->isar.ch[1].bch.debug = debug;
  81. }
  82. static int
  83. set_debug(const char *val, struct kernel_param *kp)
  84. {
  85. int ret;
  86. struct sfax_hw *card;
  87. ret = param_set_uint(val, kp);
  88. if (!ret) {
  89. read_lock(&card_lock);
  90. list_for_each_entry(card, &Cards, list)
  91. _set_debug(card);
  92. read_unlock(&card_lock);
  93. }
  94. return ret;
  95. }
  96. MODULE_AUTHOR("Karsten Keil");
  97. MODULE_LICENSE("GPL v2");
  98. MODULE_VERSION(SPEEDFAX_REV);
  99. MODULE_FIRMWARE("isdn/ISAR.BIN");
  100. module_param_call(debug, set_debug, param_get_uint, &debug, S_IRUGO | S_IWUSR);
  101. MODULE_PARM_DESC(debug, "Speedfax debug mask");
  102. module_param(irqloops, uint, S_IRUGO | S_IWUSR);
  103. MODULE_PARM_DESC(irqloops, "Speedfax maximal irqloops (default 4)");
  104. IOFUNC_IND(ISAC, sfax_hw, p_isac)
  105. IOFUNC_IND(ISAR, sfax_hw, p_isar)
  106. static irqreturn_t
  107. speedfax_irq(int intno, void *dev_id)
  108. {
  109. struct sfax_hw *sf = dev_id;
  110. u8 val;
  111. int cnt = irqloops;
  112. spin_lock(&sf->lock);
  113. val = inb(sf->cfg + TIGER_AUX_STATUS);
  114. if (val & SFAX_TIGER_IRQ_BIT) { /* for us or shared ? */
  115. spin_unlock(&sf->lock);
  116. return IRQ_NONE; /* shared */
  117. }
  118. sf->irqcnt++;
  119. val = ReadISAR_IND(sf, ISAR_IRQBIT);
  120. Start_ISAR:
  121. if (val & ISAR_IRQSTA)
  122. mISDNisar_irq(&sf->isar);
  123. val = ReadISAC_IND(sf, ISAC_ISTA);
  124. if (val)
  125. mISDNisac_irq(&sf->isac, val);
  126. val = ReadISAR_IND(sf, ISAR_IRQBIT);
  127. if ((val & ISAR_IRQSTA) && cnt--)
  128. goto Start_ISAR;
  129. if (cnt < irqloops)
  130. pr_debug("%s: %d irqloops cpu%d\n", sf->name,
  131. irqloops - cnt, smp_processor_id());
  132. if (irqloops && !cnt)
  133. pr_notice("%s: %d IRQ LOOP cpu%d\n", sf->name,
  134. irqloops, smp_processor_id());
  135. spin_unlock(&sf->lock);
  136. return IRQ_HANDLED;
  137. }
  138. static void
  139. enable_hwirq(struct sfax_hw *sf)
  140. {
  141. WriteISAC_IND(sf, ISAC_MASK, 0);
  142. WriteISAR_IND(sf, ISAR_IRQBIT, ISAR_IRQMSK);
  143. outb(SFAX_TIGER_IRQ_BIT, sf->cfg + TIGER_AUX_IRQMASK);
  144. }
  145. static void
  146. disable_hwirq(struct sfax_hw *sf)
  147. {
  148. WriteISAC_IND(sf, ISAC_MASK, 0xFF);
  149. WriteISAR_IND(sf, ISAR_IRQBIT, 0);
  150. outb(0, sf->cfg + TIGER_AUX_IRQMASK);
  151. }
  152. static void
  153. reset_speedfax(struct sfax_hw *sf)
  154. {
  155. pr_debug("%s: resetting card\n", sf->name);
  156. outb(TIGER_EXTERN_RESET_ON, sf->cfg + TIGER_RESET_ADDR);
  157. outb(SFAX_PCI_RESET_ON, sf->cfg + TIGER_AUX_DATA);
  158. mdelay(1);
  159. outb(TIGER_EXTERN_RESET_OFF, sf->cfg + TIGER_RESET_ADDR);
  160. sf->aux_data = SFAX_PCI_RESET_OFF;
  161. outb(sf->aux_data, sf->cfg + TIGER_AUX_DATA);
  162. mdelay(1);
  163. }
  164. static int
  165. sfax_ctrl(struct sfax_hw *sf, u32 cmd, u_long arg)
  166. {
  167. int ret = 0;
  168. switch (cmd) {
  169. case HW_RESET_REQ:
  170. reset_speedfax(sf);
  171. break;
  172. case HW_ACTIVATE_IND:
  173. if (arg & 1)
  174. sf->aux_data &= ~SFAX_LED1_BIT;
  175. if (arg & 2)
  176. sf->aux_data &= ~SFAX_LED2_BIT;
  177. outb(sf->aux_data, sf->cfg + TIGER_AUX_DATA);
  178. break;
  179. case HW_DEACT_IND:
  180. if (arg & 1)
  181. sf->aux_data |= SFAX_LED1_BIT;
  182. if (arg & 2)
  183. sf->aux_data |= SFAX_LED2_BIT;
  184. outb(sf->aux_data, sf->cfg + TIGER_AUX_DATA);
  185. break;
  186. default:
  187. pr_info("%s: %s unknown command %x %lx\n",
  188. sf->name, __func__, cmd, arg);
  189. ret = -EINVAL;
  190. break;
  191. }
  192. return ret;
  193. }
  194. static int
  195. channel_ctrl(struct sfax_hw *sf, struct mISDN_ctrl_req *cq)
  196. {
  197. int ret = 0;
  198. switch (cq->op) {
  199. case MISDN_CTRL_GETOP:
  200. cq->op = MISDN_CTRL_LOOP;
  201. break;
  202. case MISDN_CTRL_LOOP:
  203. /* cq->channel: 0 disable, 1 B1 loop 2 B2 loop, 3 both */
  204. if (cq->channel < 0 || cq->channel > 3) {
  205. ret = -EINVAL;
  206. break;
  207. }
  208. ret = sf->isac.ctrl(&sf->isac, HW_TESTLOOP, cq->channel);
  209. break;
  210. default:
  211. pr_info("%s: unknown Op %x\n", sf->name, cq->op);
  212. ret = -EINVAL;
  213. break;
  214. }
  215. return ret;
  216. }
  217. static int
  218. sfax_dctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
  219. {
  220. struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
  221. struct dchannel *dch = container_of(dev, struct dchannel, dev);
  222. struct sfax_hw *sf = dch->hw;
  223. struct channel_req *rq;
  224. int err = 0;
  225. pr_debug("%s: cmd:%x %p\n", sf->name, cmd, arg);
  226. switch (cmd) {
  227. case OPEN_CHANNEL:
  228. rq = arg;
  229. if (rq->protocol == ISDN_P_TE_S0)
  230. err = sf->isac.open(&sf->isac, rq);
  231. else
  232. err = sf->isar.open(&sf->isar, rq);
  233. if (err)
  234. break;
  235. if (!try_module_get(THIS_MODULE))
  236. pr_info("%s: cannot get module\n", sf->name);
  237. break;
  238. case CLOSE_CHANNEL:
  239. pr_debug("%s: dev(%d) close from %p\n", sf->name,
  240. dch->dev.id, __builtin_return_address(0));
  241. module_put(THIS_MODULE);
  242. break;
  243. case CONTROL_CHANNEL:
  244. err = channel_ctrl(sf, arg);
  245. break;
  246. default:
  247. pr_debug("%s: unknown command %x\n", sf->name, cmd);
  248. return -EINVAL;
  249. }
  250. return err;
  251. }
  252. static int __devinit
  253. init_card(struct sfax_hw *sf)
  254. {
  255. int ret, cnt = 3;
  256. u_long flags;
  257. ret = request_irq(sf->irq, speedfax_irq, IRQF_SHARED, sf->name, sf);
  258. if (ret) {
  259. pr_info("%s: couldn't get interrupt %d\n", sf->name, sf->irq);
  260. return ret;
  261. }
  262. while (cnt--) {
  263. spin_lock_irqsave(&sf->lock, flags);
  264. ret = sf->isac.init(&sf->isac);
  265. if (ret) {
  266. spin_unlock_irqrestore(&sf->lock, flags);
  267. pr_info("%s: ISAC init failed with %d\n",
  268. sf->name, ret);
  269. break;
  270. }
  271. enable_hwirq(sf);
  272. /* RESET Receiver and Transmitter */
  273. WriteISAC_IND(sf, ISAC_CMDR, 0x41);
  274. spin_unlock_irqrestore(&sf->lock, flags);
  275. msleep_interruptible(10);
  276. if (debug & DEBUG_HW)
  277. pr_notice("%s: IRQ %d count %d\n", sf->name,
  278. sf->irq, sf->irqcnt);
  279. if (!sf->irqcnt) {
  280. pr_info("%s: IRQ(%d) got no requests during init %d\n",
  281. sf->name, sf->irq, 3 - cnt);
  282. } else
  283. return 0;
  284. }
  285. free_irq(sf->irq, sf);
  286. return -EIO;
  287. }
  288. static int __devinit
  289. setup_speedfax(struct sfax_hw *sf)
  290. {
  291. u_long flags;
  292. if (!request_region(sf->cfg, 256, sf->name)) {
  293. pr_info("mISDN: %s config port %x-%x already in use\n",
  294. sf->name, sf->cfg, sf->cfg + 255);
  295. return -EIO;
  296. }
  297. outb(0xff, sf->cfg);
  298. outb(0, sf->cfg);
  299. outb(0xdd, sf->cfg + TIGER_AUX_CTRL);
  300. outb(0, sf->cfg + TIGER_AUX_IRQMASK);
  301. sf->isac.type = IPAC_TYPE_ISAC;
  302. sf->p_isac.ale = sf->cfg + SFAX_PCI_ADDR;
  303. sf->p_isac.port = sf->cfg + SFAX_PCI_ISAC;
  304. sf->p_isar.ale = sf->cfg + SFAX_PCI_ADDR;
  305. sf->p_isar.port = sf->cfg + SFAX_PCI_ISAR;
  306. ASSIGN_FUNC(IND, ISAC, sf->isac);
  307. ASSIGN_FUNC(IND, ISAR, sf->isar);
  308. spin_lock_irqsave(&sf->lock, flags);
  309. reset_speedfax(sf);
  310. disable_hwirq(sf);
  311. spin_unlock_irqrestore(&sf->lock, flags);
  312. return 0;
  313. }
  314. static void
  315. release_card(struct sfax_hw *card) {
  316. u_long flags;
  317. spin_lock_irqsave(&card->lock, flags);
  318. disable_hwirq(card);
  319. spin_unlock_irqrestore(&card->lock, flags);
  320. card->isac.release(&card->isac);
  321. free_irq(card->irq, card);
  322. card->isar.release(&card->isar);
  323. mISDN_unregister_device(&card->isac.dch.dev);
  324. release_region(card->cfg, 256);
  325. pci_disable_device(card->pdev);
  326. pci_set_drvdata(card->pdev, NULL);
  327. write_lock_irqsave(&card_lock, flags);
  328. list_del(&card->list);
  329. write_unlock_irqrestore(&card_lock, flags);
  330. kfree(card);
  331. sfax_cnt--;
  332. }
  333. static int __devinit
  334. setup_instance(struct sfax_hw *card)
  335. {
  336. const struct firmware *firmware;
  337. int i, err;
  338. u_long flags;
  339. snprintf(card->name, MISDN_MAX_IDLEN - 1, "Speedfax.%d", sfax_cnt + 1);
  340. write_lock_irqsave(&card_lock, flags);
  341. list_add_tail(&card->list, &Cards);
  342. write_unlock_irqrestore(&card_lock, flags);
  343. _set_debug(card);
  344. spin_lock_init(&card->lock);
  345. card->isac.hwlock = &card->lock;
  346. card->isar.hwlock = &card->lock;
  347. card->isar.ctrl = (void *)&sfax_ctrl;
  348. card->isac.name = card->name;
  349. card->isar.name = card->name;
  350. card->isar.owner = THIS_MODULE;
  351. err = request_firmware(&firmware, "isdn/ISAR.BIN", &card->pdev->dev);
  352. if (err < 0) {
  353. pr_info("%s: firmware request failed %d\n",
  354. card->name, err);
  355. goto error_fw;
  356. }
  357. if (debug & DEBUG_HW)
  358. pr_notice("%s: got firmware %zu bytes\n",
  359. card->name, firmware->size);
  360. mISDNisac_init(&card->isac, card);
  361. card->isac.dch.dev.D.ctrl = sfax_dctrl;
  362. card->isac.dch.dev.Bprotocols =
  363. mISDNisar_init(&card->isar, card);
  364. for (i = 0; i < 2; i++) {
  365. set_channelmap(i + 1, card->isac.dch.dev.channelmap);
  366. list_add(&card->isar.ch[i].bch.ch.list,
  367. &card->isac.dch.dev.bchannels);
  368. }
  369. err = setup_speedfax(card);
  370. if (err)
  371. goto error_setup;
  372. err = card->isar.init(&card->isar);
  373. if (err)
  374. goto error;
  375. err = mISDN_register_device(&card->isac.dch.dev,
  376. &card->pdev->dev, card->name);
  377. if (err)
  378. goto error;
  379. err = init_card(card);
  380. if (err)
  381. goto error_init;
  382. err = card->isar.firmware(&card->isar, firmware->data, firmware->size);
  383. if (!err) {
  384. release_firmware(firmware);
  385. sfax_cnt++;
  386. pr_notice("SpeedFax %d cards installed\n", sfax_cnt);
  387. return 0;
  388. }
  389. disable_hwirq(card);
  390. free_irq(card->irq, card);
  391. error_init:
  392. mISDN_unregister_device(&card->isac.dch.dev);
  393. error:
  394. release_region(card->cfg, 256);
  395. error_setup:
  396. card->isac.release(&card->isac);
  397. card->isar.release(&card->isar);
  398. release_firmware(firmware);
  399. error_fw:
  400. pci_disable_device(card->pdev);
  401. write_lock_irqsave(&card_lock, flags);
  402. list_del(&card->list);
  403. write_unlock_irqrestore(&card_lock, flags);
  404. kfree(card);
  405. return err;
  406. }
  407. static int __devinit
  408. sfaxpci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  409. {
  410. int err = -ENOMEM;
  411. struct sfax_hw *card = kzalloc(sizeof(struct sfax_hw), GFP_KERNEL);
  412. if (!card) {
  413. pr_info("No memory for Speedfax+ PCI\n");
  414. return err;
  415. }
  416. card->pdev = pdev;
  417. err = pci_enable_device(pdev);
  418. if (err) {
  419. kfree(card);
  420. return err;
  421. }
  422. pr_notice("mISDN: Speedfax found adapter %s at %s\n",
  423. (char *)ent->driver_data, pci_name(pdev));
  424. card->cfg = pci_resource_start(pdev, 0);
  425. card->irq = pdev->irq;
  426. pci_set_drvdata(pdev, card);
  427. err = setup_instance(card);
  428. if (err)
  429. pci_set_drvdata(pdev, NULL);
  430. return err;
  431. }
  432. static void __devexit
  433. sfax_remove_pci(struct pci_dev *pdev)
  434. {
  435. struct sfax_hw *card = pci_get_drvdata(pdev);
  436. if (card)
  437. release_card(card);
  438. else
  439. pr_debug("%s: drvdata already removed\n", __func__);
  440. }
  441. static struct pci_device_id sfaxpci_ids[] __devinitdata = {
  442. { PCI_VENDOR_ID_TIGERJET, PCI_DEVICE_ID_TIGERJET_100,
  443. PCI_SUBVENDOR_SPEEDFAX_PYRAMID, PCI_SUB_ID_SEDLBAUER,
  444. 0, 0, (unsigned long) "Pyramid Speedfax + PCI"
  445. },
  446. { PCI_VENDOR_ID_TIGERJET, PCI_DEVICE_ID_TIGERJET_100,
  447. PCI_SUBVENDOR_SPEEDFAX_PCI, PCI_SUB_ID_SEDLBAUER,
  448. 0, 0, (unsigned long) "Sedlbauer Speedfax + PCI"
  449. },
  450. { }
  451. };
  452. MODULE_DEVICE_TABLE(pci, sfaxpci_ids);
  453. static struct pci_driver sfaxpci_driver = {
  454. .name = "speedfax+ pci",
  455. .probe = sfaxpci_probe,
  456. .remove = __devexit_p(sfax_remove_pci),
  457. .id_table = sfaxpci_ids,
  458. };
  459. static int __init
  460. Speedfax_init(void)
  461. {
  462. int err;
  463. pr_notice("Sedlbauer Speedfax+ Driver Rev. %s\n",
  464. SPEEDFAX_REV);
  465. err = pci_register_driver(&sfaxpci_driver);
  466. return err;
  467. }
  468. static void __exit
  469. Speedfax_cleanup(void)
  470. {
  471. pci_unregister_driver(&sfaxpci_driver);
  472. }
  473. module_init(Speedfax_init);
  474. module_exit(Speedfax_cleanup);