applicom.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /* Derived from Applicom driver ac.c for SCO Unix */
  2. /* Ported by David Woodhouse, Axiom (Cambridge) Ltd. */
  3. /* dwmw2@infradead.org 30/8/98 */
  4. /* $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $ */
  5. /* This module is for Linux 2.1 and 2.2 series kernels. */
  6. /*****************************************************************************/
  7. /* J PAGET 18/02/94 passage V2.4.2 ioctl avec code 2 reset to les interrupt */
  8. /* ceci pour reseter correctement apres une sortie sauvage */
  9. /* J PAGET 02/05/94 passage V2.4.3 dans le traitement de d'interruption, */
  10. /* LoopCount n'etait pas initialise a 0. */
  11. /* F LAFORSE 04/07/95 version V2.6.0 lecture bidon apres acces a une carte */
  12. /* pour liberer le bus */
  13. /* J.PAGET 19/11/95 version V2.6.1 Nombre, addresse,irq n'est plus configure */
  14. /* et passe en argument a acinit, mais est scrute sur le bus pour s'adapter */
  15. /* au nombre de cartes presentes sur le bus. IOCL code 6 affichait V2.4.3 */
  16. /* F.LAFORSE 28/11/95 creation de fichiers acXX.o avec les differentes */
  17. /* adresses de base des cartes, IOCTL 6 plus complet */
  18. /* J.PAGET le 19/08/96 copie de la version V2.6 en V2.8.0 sans modification */
  19. /* de code autre que le texte V2.6.1 en V2.8.0 */
  20. /*****************************************************************************/
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/slab.h>
  25. #include <linux/errno.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/pci.h>
  28. #include <linux/wait.h>
  29. #include <linux/init.h>
  30. #include <linux/fs.h>
  31. #include <asm/io.h>
  32. #include <asm/uaccess.h>
  33. #include "applicom.h"
  34. /* NOTE: We use for loops with {write,read}b() instead of
  35. memcpy_{from,to}io throughout this driver. This is because
  36. the board doesn't correctly handle word accesses - only
  37. bytes.
  38. */
  39. #undef DEBUG
  40. #define MAX_BOARD 8 /* maximum of pc board possible */
  41. #define MAX_ISA_BOARD 4
  42. #define LEN_RAM_IO 0x800
  43. #define AC_MINOR 157
  44. #ifndef PCI_VENDOR_ID_APPLICOM
  45. #define PCI_VENDOR_ID_APPLICOM 0x1389
  46. #define PCI_DEVICE_ID_APPLICOM_PCIGENERIC 0x0001
  47. #define PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN 0x0002
  48. #define PCI_DEVICE_ID_APPLICOM_PCI2000PFB 0x0003
  49. #endif
  50. static char *applicom_pci_devnames[] = {
  51. "PCI board",
  52. "PCI2000IBS / PCI2000CAN",
  53. "PCI2000PFB"
  54. };
  55. static struct pci_device_id applicom_pci_tbl[] = {
  56. { PCI_VDEVICE(APPLICOM, PCI_DEVICE_ID_APPLICOM_PCIGENERIC) },
  57. { PCI_VDEVICE(APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN) },
  58. { PCI_VDEVICE(APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000PFB) },
  59. { 0 }
  60. };
  61. MODULE_DEVICE_TABLE(pci, applicom_pci_tbl);
  62. MODULE_AUTHOR("David Woodhouse & Applicom International");
  63. MODULE_DESCRIPTION("Driver for Applicom Profibus card");
  64. MODULE_LICENSE("GPL");
  65. MODULE_SUPPORTED_DEVICE("ac");
  66. static struct applicom_board {
  67. unsigned long PhysIO;
  68. void __iomem *RamIO;
  69. wait_queue_head_t FlagSleepSend;
  70. long irq;
  71. spinlock_t mutex;
  72. } apbs[MAX_BOARD];
  73. static unsigned int irq = 0; /* interrupt number IRQ */
  74. static unsigned long mem = 0; /* physical segment of board */
  75. module_param(irq, uint, 0);
  76. MODULE_PARM_DESC(irq, "IRQ of the Applicom board");
  77. module_param(mem, ulong, 0);
  78. MODULE_PARM_DESC(mem, "Shared Memory Address of Applicom board");
  79. static unsigned int numboards; /* number of installed boards */
  80. static volatile unsigned char Dummy;
  81. static DECLARE_WAIT_QUEUE_HEAD(FlagSleepRec);
  82. static unsigned int WriteErrorCount; /* number of write error */
  83. static unsigned int ReadErrorCount; /* number of read error */
  84. static unsigned int DeviceErrorCount; /* number of device error */
  85. static ssize_t ac_read (struct file *, char __user *, size_t, loff_t *);
  86. static ssize_t ac_write (struct file *, const char __user *, size_t, loff_t *);
  87. static int ac_ioctl(struct inode *, struct file *, unsigned int,
  88. unsigned long);
  89. static irqreturn_t ac_interrupt(int, void *);
  90. static const struct file_operations ac_fops = {
  91. .owner = THIS_MODULE,
  92. .llseek = no_llseek,
  93. .read = ac_read,
  94. .write = ac_write,
  95. .ioctl = ac_ioctl,
  96. };
  97. static struct miscdevice ac_miscdev = {
  98. AC_MINOR,
  99. "ac",
  100. &ac_fops
  101. };
  102. static int dummy; /* dev_id for request_irq() */
  103. static int ac_register_board(unsigned long physloc, void __iomem *loc,
  104. unsigned char boardno)
  105. {
  106. volatile unsigned char byte_reset_it;
  107. if((readb(loc + CONF_END_TEST) != 0x00) ||
  108. (readb(loc + CONF_END_TEST + 1) != 0x55) ||
  109. (readb(loc + CONF_END_TEST + 2) != 0xAA) ||
  110. (readb(loc + CONF_END_TEST + 3) != 0xFF))
  111. return 0;
  112. if (!boardno)
  113. boardno = readb(loc + NUMCARD_OWNER_TO_PC);
  114. if (!boardno || boardno > MAX_BOARD) {
  115. printk(KERN_WARNING "Board #%d (at 0x%lx) is out of range (1 <= x <= %d).\n",
  116. boardno, physloc, MAX_BOARD);
  117. return 0;
  118. }
  119. if (apbs[boardno - 1].RamIO) {
  120. printk(KERN_WARNING "Board #%d (at 0x%lx) conflicts with previous board #%d (at 0x%lx)\n",
  121. boardno, physloc, boardno, apbs[boardno-1].PhysIO);
  122. return 0;
  123. }
  124. boardno--;
  125. apbs[boardno].PhysIO = physloc;
  126. apbs[boardno].RamIO = loc;
  127. init_waitqueue_head(&apbs[boardno].FlagSleepSend);
  128. spin_lock_init(&apbs[boardno].mutex);
  129. byte_reset_it = readb(loc + RAM_IT_TO_PC);
  130. numboards++;
  131. return boardno + 1;
  132. }
  133. static void __exit applicom_exit(void)
  134. {
  135. unsigned int i;
  136. misc_deregister(&ac_miscdev);
  137. for (i = 0; i < MAX_BOARD; i++) {
  138. if (!apbs[i].RamIO)
  139. continue;
  140. if (apbs[i].irq)
  141. free_irq(apbs[i].irq, &dummy);
  142. iounmap(apbs[i].RamIO);
  143. }
  144. }
  145. static int __init applicom_init(void)
  146. {
  147. int i, numisa = 0;
  148. struct pci_dev *dev = NULL;
  149. void __iomem *RamIO;
  150. int boardno, ret;
  151. printk(KERN_INFO "Applicom driver: $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $\n");
  152. /* No mem and irq given - check for a PCI card */
  153. while ( (dev = pci_get_class(PCI_CLASS_OTHERS << 16, dev))) {
  154. if (!pci_match_id(applicom_pci_tbl, dev))
  155. continue;
  156. if (pci_enable_device(dev))
  157. return -EIO;
  158. RamIO = ioremap_nocache(pci_resource_start(dev, 0), LEN_RAM_IO);
  159. if (!RamIO) {
  160. printk(KERN_INFO "ac.o: Failed to ioremap PCI memory "
  161. "space at 0x%llx\n",
  162. (unsigned long long)pci_resource_start(dev, 0));
  163. pci_disable_device(dev);
  164. return -EIO;
  165. }
  166. printk(KERN_INFO "Applicom %s found at mem 0x%llx, irq %d\n",
  167. applicom_pci_devnames[dev->device-1],
  168. (unsigned long long)pci_resource_start(dev, 0),
  169. dev->irq);
  170. boardno = ac_register_board(pci_resource_start(dev, 0),
  171. RamIO, 0);
  172. if (!boardno) {
  173. printk(KERN_INFO "ac.o: PCI Applicom device doesn't have correct signature.\n");
  174. iounmap(RamIO);
  175. pci_disable_device(dev);
  176. continue;
  177. }
  178. if (request_irq(dev->irq, &ac_interrupt, IRQF_SHARED, "Applicom PCI", &dummy)) {
  179. printk(KERN_INFO "Could not allocate IRQ %d for PCI Applicom device.\n", dev->irq);
  180. iounmap(RamIO);
  181. pci_disable_device(dev);
  182. apbs[boardno - 1].RamIO = NULL;
  183. continue;
  184. }
  185. /* Enable interrupts. */
  186. writeb(0x40, apbs[boardno - 1].RamIO + RAM_IT_FROM_PC);
  187. apbs[boardno - 1].irq = dev->irq;
  188. }
  189. /* Finished with PCI cards. If none registered,
  190. * and there was no mem/irq specified, exit */
  191. if (!mem || !irq) {
  192. if (numboards)
  193. goto fin;
  194. else {
  195. printk(KERN_INFO "ac.o: No PCI boards found.\n");
  196. printk(KERN_INFO "ac.o: For an ISA board you must supply memory and irq parameters.\n");
  197. return -ENXIO;
  198. }
  199. }
  200. /* Now try the specified ISA cards */
  201. for (i = 0; i < MAX_ISA_BOARD; i++) {
  202. RamIO = ioremap_nocache(mem + (LEN_RAM_IO * i), LEN_RAM_IO);
  203. if (!RamIO) {
  204. printk(KERN_INFO "ac.o: Failed to ioremap the ISA card's memory space (slot #%d)\n", i + 1);
  205. continue;
  206. }
  207. if (!(boardno = ac_register_board((unsigned long)mem+ (LEN_RAM_IO*i),
  208. RamIO,i+1))) {
  209. iounmap(RamIO);
  210. continue;
  211. }
  212. printk(KERN_NOTICE "Applicom ISA card found at mem 0x%lx, irq %d\n", mem + (LEN_RAM_IO*i), irq);
  213. if (!numisa) {
  214. if (request_irq(irq, &ac_interrupt, IRQF_SHARED, "Applicom ISA", &dummy)) {
  215. printk(KERN_WARNING "Could not allocate IRQ %d for ISA Applicom device.\n", irq);
  216. iounmap(RamIO);
  217. apbs[boardno - 1].RamIO = NULL;
  218. }
  219. else
  220. apbs[boardno - 1].irq = irq;
  221. }
  222. else
  223. apbs[boardno - 1].irq = 0;
  224. numisa++;
  225. }
  226. if (!numisa)
  227. printk(KERN_WARNING "ac.o: No valid ISA Applicom boards found "
  228. "at mem 0x%lx\n", mem);
  229. fin:
  230. init_waitqueue_head(&FlagSleepRec);
  231. WriteErrorCount = 0;
  232. ReadErrorCount = 0;
  233. DeviceErrorCount = 0;
  234. if (numboards) {
  235. ret = misc_register(&ac_miscdev);
  236. if (ret) {
  237. printk(KERN_WARNING "ac.o: Unable to register misc device\n");
  238. goto out;
  239. }
  240. for (i = 0; i < MAX_BOARD; i++) {
  241. int serial;
  242. char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
  243. if (!apbs[i].RamIO)
  244. continue;
  245. for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
  246. boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
  247. boardname[serial] = 0;
  248. printk(KERN_INFO "Applicom board %d: %s, PROM V%d.%d",
  249. i+1, boardname,
  250. (int)(readb(apbs[i].RamIO + VERS) >> 4),
  251. (int)(readb(apbs[i].RamIO + VERS) & 0xF));
  252. serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
  253. (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
  254. (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
  255. if (serial != 0)
  256. printk(" S/N %d\n", serial);
  257. else
  258. printk("\n");
  259. }
  260. return 0;
  261. }
  262. else
  263. return -ENXIO;
  264. out:
  265. for (i = 0; i < MAX_BOARD; i++) {
  266. if (!apbs[i].RamIO)
  267. continue;
  268. if (apbs[i].irq)
  269. free_irq(apbs[i].irq, &dummy);
  270. iounmap(apbs[i].RamIO);
  271. }
  272. pci_disable_device(dev);
  273. return ret;
  274. }
  275. module_init(applicom_init);
  276. module_exit(applicom_exit);
  277. static ssize_t ac_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  278. {
  279. unsigned int NumCard; /* Board number 1 -> 8 */
  280. unsigned int IndexCard; /* Index board number 0 -> 7 */
  281. unsigned char TicCard; /* Board TIC to send */
  282. unsigned long flags; /* Current priority */
  283. struct st_ram_io st_loc;
  284. struct mailbox tmpmailbox;
  285. #ifdef DEBUG
  286. int c;
  287. #endif
  288. DECLARE_WAITQUEUE(wait, current);
  289. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  290. static int warncount = 5;
  291. if (warncount) {
  292. printk(KERN_INFO "Hmmm. write() of Applicom card, length %zd != expected %zd\n",
  293. count, sizeof(struct st_ram_io) + sizeof(struct mailbox));
  294. warncount--;
  295. }
  296. return -EINVAL;
  297. }
  298. if(copy_from_user(&st_loc, buf, sizeof(struct st_ram_io)))
  299. return -EFAULT;
  300. if(copy_from_user(&tmpmailbox, &buf[sizeof(struct st_ram_io)],
  301. sizeof(struct mailbox)))
  302. return -EFAULT;
  303. NumCard = st_loc.num_card; /* board number to send */
  304. TicCard = st_loc.tic_des_from_pc; /* tic number to send */
  305. IndexCard = NumCard - 1;
  306. if((NumCard < 1) || (NumCard > MAX_BOARD) || !apbs[IndexCard].RamIO)
  307. return -EINVAL;
  308. #ifdef DEBUG
  309. printk("Write to applicom card #%d. struct st_ram_io follows:",
  310. IndexCard+1);
  311. for (c = 0; c < sizeof(struct st_ram_io);) {
  312. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
  313. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  314. printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
  315. }
  316. }
  317. printk("\nstruct mailbox follows:");
  318. for (c = 0; c < sizeof(struct mailbox);) {
  319. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
  320. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  321. printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
  322. }
  323. }
  324. printk("\n");
  325. #endif
  326. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  327. /* Test octet ready correct */
  328. if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
  329. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  330. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  331. printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
  332. IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
  333. DeviceErrorCount++;
  334. return -EIO;
  335. }
  336. /* Place ourselves on the wait queue */
  337. set_current_state(TASK_INTERRUPTIBLE);
  338. add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  339. /* Check whether the card is ready for us */
  340. while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
  341. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  342. /* It's busy. Sleep. */
  343. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  344. schedule();
  345. if (signal_pending(current)) {
  346. remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
  347. &wait);
  348. return -EINTR;
  349. }
  350. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  351. set_current_state(TASK_INTERRUPTIBLE);
  352. }
  353. /* We may not have actually slept */
  354. set_current_state(TASK_RUNNING);
  355. remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  356. writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  357. /* Which is best - lock down the pages with rawio and then
  358. copy directly, or use bounce buffers? For now we do the latter
  359. because it works with 2.2 still */
  360. {
  361. unsigned char *from = (unsigned char *) &tmpmailbox;
  362. void __iomem *to = apbs[IndexCard].RamIO + RAM_FROM_PC;
  363. int c;
  364. for (c = 0; c < sizeof(struct mailbox); c++)
  365. writeb(*(from++), to++);
  366. }
  367. writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
  368. writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
  369. writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
  370. writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  371. writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  372. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  373. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  374. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  375. return 0;
  376. }
  377. static int do_ac_read(int IndexCard, char __user *buf,
  378. struct st_ram_io *st_loc, struct mailbox *mailbox)
  379. {
  380. void __iomem *from = apbs[IndexCard].RamIO + RAM_TO_PC;
  381. unsigned char *to = (unsigned char *)&mailbox;
  382. #ifdef DEBUG
  383. int c;
  384. #endif
  385. st_loc->tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
  386. st_loc->numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  387. {
  388. int c;
  389. for (c = 0; c < sizeof(struct mailbox); c++)
  390. *(to++) = readb(from++);
  391. }
  392. writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  393. writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
  394. writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  395. writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
  396. apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
  397. writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  398. writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
  399. writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  400. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  401. #ifdef DEBUG
  402. printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
  403. for (c = 0; c < sizeof(struct st_ram_io);) {
  404. printk("\n%5.5X: %2.2X", c, ((unsigned char *)st_loc)[c]);
  405. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  406. printk(" %2.2X", ((unsigned char *)st_loc)[c]);
  407. }
  408. }
  409. printk("\nstruct mailbox follows:");
  410. for (c = 0; c < sizeof(struct mailbox);) {
  411. printk("\n%5.5X: %2.2X", c, ((unsigned char *)mailbox)[c]);
  412. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  413. printk(" %2.2X", ((unsigned char *)mailbox)[c]);
  414. }
  415. }
  416. printk("\n");
  417. #endif
  418. return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
  419. }
  420. static ssize_t ac_read (struct file *filp, char __user *buf, size_t count, loff_t *ptr)
  421. {
  422. unsigned long flags;
  423. unsigned int i;
  424. unsigned char tmp;
  425. int ret = 0;
  426. DECLARE_WAITQUEUE(wait, current);
  427. #ifdef DEBUG
  428. int loopcount=0;
  429. #endif
  430. /* No need to ratelimit this. Only root can trigger it anyway */
  431. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  432. printk( KERN_WARNING "Hmmm. read() of Applicom card, length %zd != expected %zd\n",
  433. count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
  434. return -EINVAL;
  435. }
  436. while(1) {
  437. /* Stick ourself on the wait queue */
  438. set_current_state(TASK_INTERRUPTIBLE);
  439. add_wait_queue(&FlagSleepRec, &wait);
  440. /* Scan each board, looking for one which has a packet for us */
  441. for (i=0; i < MAX_BOARD; i++) {
  442. if (!apbs[i].RamIO)
  443. continue;
  444. spin_lock_irqsave(&apbs[i].mutex, flags);
  445. tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
  446. if (tmp == 2) {
  447. struct st_ram_io st_loc;
  448. struct mailbox mailbox;
  449. /* Got a packet for us */
  450. ret = do_ac_read(i, buf, &st_loc, &mailbox);
  451. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  452. set_current_state(TASK_RUNNING);
  453. remove_wait_queue(&FlagSleepRec, &wait);
  454. if (copy_to_user(buf, &st_loc, sizeof(st_loc)))
  455. return -EFAULT;
  456. if (copy_to_user(buf + sizeof(st_loc), &mailbox, sizeof(mailbox)))
  457. return -EFAULT;
  458. return tmp;
  459. }
  460. if (tmp > 2) {
  461. /* Got an error */
  462. Dummy = readb(apbs[i].RamIO + VERS);
  463. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  464. set_current_state(TASK_RUNNING);
  465. remove_wait_queue(&FlagSleepRec, &wait);
  466. printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
  467. i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  468. DeviceErrorCount++;
  469. return -EIO;
  470. }
  471. /* Nothing for us. Try the next board */
  472. Dummy = readb(apbs[i].RamIO + VERS);
  473. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  474. } /* per board */
  475. /* OK - No boards had data for us. Sleep now */
  476. schedule();
  477. remove_wait_queue(&FlagSleepRec, &wait);
  478. if (signal_pending(current))
  479. return -EINTR;
  480. #ifdef DEBUG
  481. if (loopcount++ > 2) {
  482. printk(KERN_DEBUG "Looping in ac_read. loopcount %d\n", loopcount);
  483. }
  484. #endif
  485. }
  486. }
  487. static irqreturn_t ac_interrupt(int vec, void *dev_instance)
  488. {
  489. unsigned int i;
  490. unsigned int FlagInt;
  491. unsigned int LoopCount;
  492. int handled = 0;
  493. // printk("Applicom interrupt on IRQ %d occurred\n", vec);
  494. LoopCount = 0;
  495. do {
  496. FlagInt = 0;
  497. for (i = 0; i < MAX_BOARD; i++) {
  498. /* Skip if this board doesn't exist */
  499. if (!apbs[i].RamIO)
  500. continue;
  501. spin_lock(&apbs[i].mutex);
  502. /* Skip if this board doesn't want attention */
  503. if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
  504. spin_unlock(&apbs[i].mutex);
  505. continue;
  506. }
  507. handled = 1;
  508. FlagInt = 1;
  509. writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
  510. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
  511. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
  512. i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  513. DeviceErrorCount++;
  514. }
  515. if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
  516. (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
  517. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
  518. i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
  519. DeviceErrorCount++;
  520. }
  521. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) { /* mailbox sent by the card ? */
  522. if (waitqueue_active(&FlagSleepRec)) {
  523. wake_up_interruptible(&FlagSleepRec);
  524. }
  525. }
  526. if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) { /* ram i/o free for write by pc ? */
  527. if (waitqueue_active(&apbs[i].FlagSleepSend)) { /* process sleep during read ? */
  528. wake_up_interruptible(&apbs[i].FlagSleepSend);
  529. }
  530. }
  531. Dummy = readb(apbs[i].RamIO + VERS);
  532. if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
  533. /* There's another int waiting on this card */
  534. spin_unlock(&apbs[i].mutex);
  535. i--;
  536. } else {
  537. spin_unlock(&apbs[i].mutex);
  538. }
  539. }
  540. if (FlagInt)
  541. LoopCount = 0;
  542. else
  543. LoopCount++;
  544. } while(LoopCount < 2);
  545. return IRQ_RETVAL(handled);
  546. }
  547. static int ac_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  548. { /* @ ADG ou ATO selon le cas */
  549. int i;
  550. unsigned char IndexCard;
  551. void __iomem *pmem;
  552. int ret = 0;
  553. volatile unsigned char byte_reset_it;
  554. struct st_ram_io *adgl;
  555. void __user *argp = (void __user *)arg;
  556. /* In general, the device is only openable by root anyway, so we're not
  557. particularly concerned that bogus ioctls can flood the console. */
  558. adgl = kmalloc(sizeof(struct st_ram_io), GFP_KERNEL);
  559. if (!adgl)
  560. return -ENOMEM;
  561. if (copy_from_user(adgl, argp, sizeof(struct st_ram_io))) {
  562. kfree(adgl);
  563. return -EFAULT;
  564. }
  565. IndexCard = adgl->num_card-1;
  566. if(cmd != 0 && cmd != 6 &&
  567. ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) {
  568. static int warncount = 10;
  569. if (warncount) {
  570. printk( KERN_WARNING "APPLICOM driver IOCTL, bad board number %d\n",(int)IndexCard+1);
  571. warncount--;
  572. }
  573. kfree(adgl);
  574. return -EINVAL;
  575. }
  576. switch (cmd) {
  577. case 0:
  578. pmem = apbs[IndexCard].RamIO;
  579. for (i = 0; i < sizeof(struct st_ram_io); i++)
  580. ((unsigned char *)adgl)[i]=readb(pmem++);
  581. if (copy_to_user(argp, adgl, sizeof(struct st_ram_io)))
  582. ret = -EFAULT;
  583. break;
  584. case 1:
  585. pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
  586. for (i = 0; i < 4; i++)
  587. adgl->conf_end_test[i] = readb(pmem++);
  588. for (i = 0; i < 2; i++)
  589. adgl->error_code[i] = readb(pmem++);
  590. for (i = 0; i < 4; i++)
  591. adgl->parameter_error[i] = readb(pmem++);
  592. pmem = apbs[IndexCard].RamIO + VERS;
  593. adgl->vers = readb(pmem);
  594. pmem = apbs[IndexCard].RamIO + TYPE_CARD;
  595. for (i = 0; i < 20; i++)
  596. adgl->reserv1[i] = readb(pmem++);
  597. *(int *)&adgl->reserv1[20] =
  598. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER) << 16) +
  599. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 1) << 8) +
  600. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 2) );
  601. if (copy_to_user(argp, adgl, sizeof(struct st_ram_io)))
  602. ret = -EFAULT;
  603. break;
  604. case 2:
  605. pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
  606. for (i = 0; i < 10; i++)
  607. writeb(0xff, pmem++);
  608. writeb(adgl->data_from_pc_ready,
  609. apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  610. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  611. for (i = 0; i < MAX_BOARD; i++) {
  612. if (apbs[i].RamIO) {
  613. byte_reset_it = readb(apbs[i].RamIO + RAM_IT_TO_PC);
  614. }
  615. }
  616. break;
  617. case 3:
  618. pmem = apbs[IndexCard].RamIO + TIC_DES_FROM_PC;
  619. writeb(adgl->tic_des_from_pc, pmem);
  620. break;
  621. case 4:
  622. pmem = apbs[IndexCard].RamIO + TIC_OWNER_TO_PC;
  623. adgl->tic_owner_to_pc = readb(pmem++);
  624. adgl->numcard_owner_to_pc = readb(pmem);
  625. if (copy_to_user(argp, adgl,sizeof(struct st_ram_io)))
  626. ret = -EFAULT;
  627. break;
  628. case 5:
  629. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  630. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  631. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  632. writeb(4, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  633. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  634. break;
  635. case 6:
  636. printk(KERN_INFO "APPLICOM driver release .... V2.8.0 ($Revision: 1.30 $)\n");
  637. printk(KERN_INFO "Number of installed boards . %d\n", (int) numboards);
  638. printk(KERN_INFO "Segment of board ........... %X\n", (int) mem);
  639. printk(KERN_INFO "Interrupt IRQ number ....... %d\n", (int) irq);
  640. for (i = 0; i < MAX_BOARD; i++) {
  641. int serial;
  642. char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
  643. if (!apbs[i].RamIO)
  644. continue;
  645. for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
  646. boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
  647. boardname[serial] = 0;
  648. printk(KERN_INFO "Prom version board %d ....... V%d.%d %s",
  649. i+1,
  650. (int)(readb(apbs[IndexCard].RamIO + VERS) >> 4),
  651. (int)(readb(apbs[IndexCard].RamIO + VERS) & 0xF),
  652. boardname);
  653. serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
  654. (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
  655. (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
  656. if (serial != 0)
  657. printk(" S/N %d\n", serial);
  658. else
  659. printk("\n");
  660. }
  661. if (DeviceErrorCount != 0)
  662. printk(KERN_INFO "DeviceErrorCount ........... %d\n", DeviceErrorCount);
  663. if (ReadErrorCount != 0)
  664. printk(KERN_INFO "ReadErrorCount ............. %d\n", ReadErrorCount);
  665. if (WriteErrorCount != 0)
  666. printk(KERN_INFO "WriteErrorCount ............ %d\n", WriteErrorCount);
  667. if (waitqueue_active(&FlagSleepRec))
  668. printk(KERN_INFO "Process in read pending\n");
  669. for (i = 0; i < MAX_BOARD; i++) {
  670. if (apbs[i].RamIO && waitqueue_active(&apbs[i].FlagSleepSend))
  671. printk(KERN_INFO "Process in write pending board %d\n",i+1);
  672. }
  673. break;
  674. default:
  675. printk(KERN_INFO "APPLICOM driver ioctl, unknown function code %d\n",cmd) ;
  676. ret = -EINVAL;
  677. break;
  678. }
  679. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  680. kfree(adgl);
  681. return 0;
  682. }