speedfax.c 13 KB

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