applicom.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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_ALIAS_MISCDEV(AC_MINOR);
  66. MODULE_SUPPORTED_DEVICE("ac");
  67. static struct applicom_board {
  68. unsigned long PhysIO;
  69. void __iomem *RamIO;
  70. wait_queue_head_t FlagSleepSend;
  71. long irq;
  72. spinlock_t mutex;
  73. } apbs[MAX_BOARD];
  74. static unsigned int irq = 0; /* interrupt number IRQ */
  75. static unsigned long mem = 0; /* physical segment of board */
  76. module_param(irq, uint, 0);
  77. MODULE_PARM_DESC(irq, "IRQ of the Applicom board");
  78. module_param(mem, ulong, 0);
  79. MODULE_PARM_DESC(mem, "Shared Memory Address of Applicom board");
  80. static unsigned int numboards; /* number of installed boards */
  81. static volatile unsigned char Dummy;
  82. static DECLARE_WAIT_QUEUE_HEAD(FlagSleepRec);
  83. static unsigned int WriteErrorCount; /* number of write error */
  84. static unsigned int ReadErrorCount; /* number of read error */
  85. static unsigned int DeviceErrorCount; /* number of device error */
  86. static ssize_t ac_read (struct file *, char __user *, size_t, loff_t *);
  87. static ssize_t ac_write (struct file *, const char __user *, size_t, loff_t *);
  88. static int ac_ioctl(struct inode *, struct file *, unsigned int,
  89. unsigned long);
  90. static irqreturn_t ac_interrupt(int, void *);
  91. static const struct file_operations ac_fops = {
  92. .owner = THIS_MODULE,
  93. .llseek = no_llseek,
  94. .read = ac_read,
  95. .write = ac_write,
  96. .ioctl = ac_ioctl,
  97. };
  98. static struct miscdevice ac_miscdev = {
  99. AC_MINOR,
  100. "ac",
  101. &ac_fops
  102. };
  103. static int dummy; /* dev_id for request_irq() */
  104. static int ac_register_board(unsigned long physloc, void __iomem *loc,
  105. unsigned char boardno)
  106. {
  107. volatile unsigned char byte_reset_it;
  108. if((readb(loc + CONF_END_TEST) != 0x00) ||
  109. (readb(loc + CONF_END_TEST + 1) != 0x55) ||
  110. (readb(loc + CONF_END_TEST + 2) != 0xAA) ||
  111. (readb(loc + CONF_END_TEST + 3) != 0xFF))
  112. return 0;
  113. if (!boardno)
  114. boardno = readb(loc + NUMCARD_OWNER_TO_PC);
  115. if (!boardno || boardno > MAX_BOARD) {
  116. printk(KERN_WARNING "Board #%d (at 0x%lx) is out of range (1 <= x <= %d).\n",
  117. boardno, physloc, MAX_BOARD);
  118. return 0;
  119. }
  120. if (apbs[boardno - 1].RamIO) {
  121. printk(KERN_WARNING "Board #%d (at 0x%lx) conflicts with previous board #%d (at 0x%lx)\n",
  122. boardno, physloc, boardno, apbs[boardno-1].PhysIO);
  123. return 0;
  124. }
  125. boardno--;
  126. apbs[boardno].PhysIO = physloc;
  127. apbs[boardno].RamIO = loc;
  128. init_waitqueue_head(&apbs[boardno].FlagSleepSend);
  129. spin_lock_init(&apbs[boardno].mutex);
  130. byte_reset_it = readb(loc + RAM_IT_TO_PC);
  131. numboards++;
  132. return boardno + 1;
  133. }
  134. static void __exit applicom_exit(void)
  135. {
  136. unsigned int i;
  137. misc_deregister(&ac_miscdev);
  138. for (i = 0; i < MAX_BOARD; i++) {
  139. if (!apbs[i].RamIO)
  140. continue;
  141. if (apbs[i].irq)
  142. free_irq(apbs[i].irq, &dummy);
  143. iounmap(apbs[i].RamIO);
  144. }
  145. }
  146. static int __init applicom_init(void)
  147. {
  148. int i, numisa = 0;
  149. struct pci_dev *dev = NULL;
  150. void __iomem *RamIO;
  151. int boardno, ret;
  152. printk(KERN_INFO "Applicom driver: $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $\n");
  153. /* No mem and irq given - check for a PCI card */
  154. while ( (dev = pci_get_class(PCI_CLASS_OTHERS << 16, dev))) {
  155. if (!pci_match_id(applicom_pci_tbl, dev))
  156. continue;
  157. if (pci_enable_device(dev))
  158. return -EIO;
  159. RamIO = ioremap_nocache(pci_resource_start(dev, 0), LEN_RAM_IO);
  160. if (!RamIO) {
  161. printk(KERN_INFO "ac.o: Failed to ioremap PCI memory "
  162. "space at 0x%llx\n",
  163. (unsigned long long)pci_resource_start(dev, 0));
  164. pci_disable_device(dev);
  165. return -EIO;
  166. }
  167. printk(KERN_INFO "Applicom %s found at mem 0x%llx, irq %d\n",
  168. applicom_pci_devnames[dev->device-1],
  169. (unsigned long long)pci_resource_start(dev, 0),
  170. dev->irq);
  171. boardno = ac_register_board(pci_resource_start(dev, 0),
  172. RamIO, 0);
  173. if (!boardno) {
  174. printk(KERN_INFO "ac.o: PCI Applicom device doesn't have correct signature.\n");
  175. iounmap(RamIO);
  176. pci_disable_device(dev);
  177. continue;
  178. }
  179. if (request_irq(dev->irq, &ac_interrupt, IRQF_SHARED, "Applicom PCI", &dummy)) {
  180. printk(KERN_INFO "Could not allocate IRQ %d for PCI Applicom device.\n", dev->irq);
  181. iounmap(RamIO);
  182. pci_disable_device(dev);
  183. apbs[boardno - 1].RamIO = NULL;
  184. continue;
  185. }
  186. /* Enable interrupts. */
  187. writeb(0x40, apbs[boardno - 1].RamIO + RAM_IT_FROM_PC);
  188. apbs[boardno - 1].irq = dev->irq;
  189. }
  190. /* Finished with PCI cards. If none registered,
  191. * and there was no mem/irq specified, exit */
  192. if (!mem || !irq) {
  193. if (numboards)
  194. goto fin;
  195. else {
  196. printk(KERN_INFO "ac.o: No PCI boards found.\n");
  197. printk(KERN_INFO "ac.o: For an ISA board you must supply memory and irq parameters.\n");
  198. return -ENXIO;
  199. }
  200. }
  201. /* Now try the specified ISA cards */
  202. for (i = 0; i < MAX_ISA_BOARD; i++) {
  203. RamIO = ioremap_nocache(mem + (LEN_RAM_IO * i), LEN_RAM_IO);
  204. if (!RamIO) {
  205. printk(KERN_INFO "ac.o: Failed to ioremap the ISA card's memory space (slot #%d)\n", i + 1);
  206. continue;
  207. }
  208. if (!(boardno = ac_register_board((unsigned long)mem+ (LEN_RAM_IO*i),
  209. RamIO,i+1))) {
  210. iounmap(RamIO);
  211. continue;
  212. }
  213. printk(KERN_NOTICE "Applicom ISA card found at mem 0x%lx, irq %d\n", mem + (LEN_RAM_IO*i), irq);
  214. if (!numisa) {
  215. if (request_irq(irq, &ac_interrupt, IRQF_SHARED, "Applicom ISA", &dummy)) {
  216. printk(KERN_WARNING "Could not allocate IRQ %d for ISA Applicom device.\n", irq);
  217. iounmap(RamIO);
  218. apbs[boardno - 1].RamIO = NULL;
  219. }
  220. else
  221. apbs[boardno - 1].irq = irq;
  222. }
  223. else
  224. apbs[boardno - 1].irq = 0;
  225. numisa++;
  226. }
  227. if (!numisa)
  228. printk(KERN_WARNING "ac.o: No valid ISA Applicom boards found "
  229. "at mem 0x%lx\n", mem);
  230. fin:
  231. init_waitqueue_head(&FlagSleepRec);
  232. WriteErrorCount = 0;
  233. ReadErrorCount = 0;
  234. DeviceErrorCount = 0;
  235. if (numboards) {
  236. ret = misc_register(&ac_miscdev);
  237. if (ret) {
  238. printk(KERN_WARNING "ac.o: Unable to register misc device\n");
  239. goto out;
  240. }
  241. for (i = 0; i < MAX_BOARD; i++) {
  242. int serial;
  243. char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
  244. if (!apbs[i].RamIO)
  245. continue;
  246. for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
  247. boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
  248. boardname[serial] = 0;
  249. printk(KERN_INFO "Applicom board %d: %s, PROM V%d.%d",
  250. i+1, boardname,
  251. (int)(readb(apbs[i].RamIO + VERS) >> 4),
  252. (int)(readb(apbs[i].RamIO + VERS) & 0xF));
  253. serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
  254. (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
  255. (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
  256. if (serial != 0)
  257. printk(" S/N %d\n", serial);
  258. else
  259. printk("\n");
  260. }
  261. return 0;
  262. }
  263. else
  264. return -ENXIO;
  265. out:
  266. for (i = 0; i < MAX_BOARD; i++) {
  267. if (!apbs[i].RamIO)
  268. continue;
  269. if (apbs[i].irq)
  270. free_irq(apbs[i].irq, &dummy);
  271. iounmap(apbs[i].RamIO);
  272. }
  273. pci_disable_device(dev);
  274. return ret;
  275. }
  276. module_init(applicom_init);
  277. module_exit(applicom_exit);
  278. static ssize_t ac_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  279. {
  280. unsigned int NumCard; /* Board number 1 -> 8 */
  281. unsigned int IndexCard; /* Index board number 0 -> 7 */
  282. unsigned char TicCard; /* Board TIC to send */
  283. unsigned long flags; /* Current priority */
  284. struct st_ram_io st_loc;
  285. struct mailbox tmpmailbox;
  286. #ifdef DEBUG
  287. int c;
  288. #endif
  289. DECLARE_WAITQUEUE(wait, current);
  290. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  291. static int warncount = 5;
  292. if (warncount) {
  293. printk(KERN_INFO "Hmmm. write() of Applicom card, length %zd != expected %zd\n",
  294. count, sizeof(struct st_ram_io) + sizeof(struct mailbox));
  295. warncount--;
  296. }
  297. return -EINVAL;
  298. }
  299. if(copy_from_user(&st_loc, buf, sizeof(struct st_ram_io)))
  300. return -EFAULT;
  301. if(copy_from_user(&tmpmailbox, &buf[sizeof(struct st_ram_io)],
  302. sizeof(struct mailbox)))
  303. return -EFAULT;
  304. NumCard = st_loc.num_card; /* board number to send */
  305. TicCard = st_loc.tic_des_from_pc; /* tic number to send */
  306. IndexCard = NumCard - 1;
  307. if((NumCard < 1) || (NumCard > MAX_BOARD) || !apbs[IndexCard].RamIO)
  308. return -EINVAL;
  309. #ifdef DEBUG
  310. printk("Write to applicom card #%d. struct st_ram_io follows:",
  311. IndexCard+1);
  312. for (c = 0; c < sizeof(struct st_ram_io);) {
  313. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
  314. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  315. printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
  316. }
  317. }
  318. printk("\nstruct mailbox follows:");
  319. for (c = 0; c < sizeof(struct mailbox);) {
  320. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
  321. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  322. printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
  323. }
  324. }
  325. printk("\n");
  326. #endif
  327. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  328. /* Test octet ready correct */
  329. if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
  330. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  331. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  332. printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
  333. IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
  334. DeviceErrorCount++;
  335. return -EIO;
  336. }
  337. /* Place ourselves on the wait queue */
  338. set_current_state(TASK_INTERRUPTIBLE);
  339. add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  340. /* Check whether the card is ready for us */
  341. while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
  342. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  343. /* It's busy. Sleep. */
  344. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  345. schedule();
  346. if (signal_pending(current)) {
  347. remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
  348. &wait);
  349. return -EINTR;
  350. }
  351. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  352. set_current_state(TASK_INTERRUPTIBLE);
  353. }
  354. /* We may not have actually slept */
  355. set_current_state(TASK_RUNNING);
  356. remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  357. writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  358. /* Which is best - lock down the pages with rawio and then
  359. copy directly, or use bounce buffers? For now we do the latter
  360. because it works with 2.2 still */
  361. {
  362. unsigned char *from = (unsigned char *) &tmpmailbox;
  363. void __iomem *to = apbs[IndexCard].RamIO + RAM_FROM_PC;
  364. int c;
  365. for (c = 0; c < sizeof(struct mailbox); c++)
  366. writeb(*(from++), to++);
  367. }
  368. writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
  369. writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
  370. writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
  371. writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  372. writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  373. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  374. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  375. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  376. return 0;
  377. }
  378. static int do_ac_read(int IndexCard, char __user *buf,
  379. struct st_ram_io *st_loc, struct mailbox *mailbox)
  380. {
  381. void __iomem *from = apbs[IndexCard].RamIO + RAM_TO_PC;
  382. unsigned char *to = (unsigned char *)mailbox;
  383. #ifdef DEBUG
  384. int c;
  385. #endif
  386. st_loc->tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
  387. st_loc->numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  388. {
  389. int c;
  390. for (c = 0; c < sizeof(struct mailbox); c++)
  391. *(to++) = readb(from++);
  392. }
  393. writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  394. writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
  395. writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  396. writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
  397. apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
  398. writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  399. writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
  400. writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  401. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  402. #ifdef DEBUG
  403. printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
  404. for (c = 0; c < sizeof(struct st_ram_io);) {
  405. printk("\n%5.5X: %2.2X", c, ((unsigned char *)st_loc)[c]);
  406. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  407. printk(" %2.2X", ((unsigned char *)st_loc)[c]);
  408. }
  409. }
  410. printk("\nstruct mailbox follows:");
  411. for (c = 0; c < sizeof(struct mailbox);) {
  412. printk("\n%5.5X: %2.2X", c, ((unsigned char *)mailbox)[c]);
  413. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  414. printk(" %2.2X", ((unsigned char *)mailbox)[c]);
  415. }
  416. }
  417. printk("\n");
  418. #endif
  419. return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
  420. }
  421. static ssize_t ac_read (struct file *filp, char __user *buf, size_t count, loff_t *ptr)
  422. {
  423. unsigned long flags;
  424. unsigned int i;
  425. unsigned char tmp;
  426. int ret = 0;
  427. DECLARE_WAITQUEUE(wait, current);
  428. #ifdef DEBUG
  429. int loopcount=0;
  430. #endif
  431. /* No need to ratelimit this. Only root can trigger it anyway */
  432. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  433. printk( KERN_WARNING "Hmmm. read() of Applicom card, length %zd != expected %zd\n",
  434. count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
  435. return -EINVAL;
  436. }
  437. while(1) {
  438. /* Stick ourself on the wait queue */
  439. set_current_state(TASK_INTERRUPTIBLE);
  440. add_wait_queue(&FlagSleepRec, &wait);
  441. /* Scan each board, looking for one which has a packet for us */
  442. for (i=0; i < MAX_BOARD; i++) {
  443. if (!apbs[i].RamIO)
  444. continue;
  445. spin_lock_irqsave(&apbs[i].mutex, flags);
  446. tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
  447. if (tmp == 2) {
  448. struct st_ram_io st_loc;
  449. struct mailbox mailbox;
  450. /* Got a packet for us */
  451. ret = do_ac_read(i, buf, &st_loc, &mailbox);
  452. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  453. set_current_state(TASK_RUNNING);
  454. remove_wait_queue(&FlagSleepRec, &wait);
  455. if (copy_to_user(buf, &st_loc, sizeof(st_loc)))
  456. return -EFAULT;
  457. if (copy_to_user(buf + sizeof(st_loc), &mailbox, sizeof(mailbox)))
  458. return -EFAULT;
  459. return tmp;
  460. }
  461. if (tmp > 2) {
  462. /* Got an error */
  463. Dummy = readb(apbs[i].RamIO + VERS);
  464. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  465. set_current_state(TASK_RUNNING);
  466. remove_wait_queue(&FlagSleepRec, &wait);
  467. printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
  468. i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  469. DeviceErrorCount++;
  470. return -EIO;
  471. }
  472. /* Nothing for us. Try the next board */
  473. Dummy = readb(apbs[i].RamIO + VERS);
  474. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  475. } /* per board */
  476. /* OK - No boards had data for us. Sleep now */
  477. schedule();
  478. remove_wait_queue(&FlagSleepRec, &wait);
  479. if (signal_pending(current))
  480. return -EINTR;
  481. #ifdef DEBUG
  482. if (loopcount++ > 2) {
  483. printk(KERN_DEBUG "Looping in ac_read. loopcount %d\n", loopcount);
  484. }
  485. #endif
  486. }
  487. }
  488. static irqreturn_t ac_interrupt(int vec, void *dev_instance)
  489. {
  490. unsigned int i;
  491. unsigned int FlagInt;
  492. unsigned int LoopCount;
  493. int handled = 0;
  494. // printk("Applicom interrupt on IRQ %d occurred\n", vec);
  495. LoopCount = 0;
  496. do {
  497. FlagInt = 0;
  498. for (i = 0; i < MAX_BOARD; i++) {
  499. /* Skip if this board doesn't exist */
  500. if (!apbs[i].RamIO)
  501. continue;
  502. spin_lock(&apbs[i].mutex);
  503. /* Skip if this board doesn't want attention */
  504. if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
  505. spin_unlock(&apbs[i].mutex);
  506. continue;
  507. }
  508. handled = 1;
  509. FlagInt = 1;
  510. writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
  511. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
  512. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
  513. i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  514. DeviceErrorCount++;
  515. }
  516. if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
  517. (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
  518. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
  519. i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
  520. DeviceErrorCount++;
  521. }
  522. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) { /* mailbox sent by the card ? */
  523. if (waitqueue_active(&FlagSleepRec)) {
  524. wake_up_interruptible(&FlagSleepRec);
  525. }
  526. }
  527. if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) { /* ram i/o free for write by pc ? */
  528. if (waitqueue_active(&apbs[i].FlagSleepSend)) { /* process sleep during read ? */
  529. wake_up_interruptible(&apbs[i].FlagSleepSend);
  530. }
  531. }
  532. Dummy = readb(apbs[i].RamIO + VERS);
  533. if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
  534. /* There's another int waiting on this card */
  535. spin_unlock(&apbs[i].mutex);
  536. i--;
  537. } else {
  538. spin_unlock(&apbs[i].mutex);
  539. }
  540. }
  541. if (FlagInt)
  542. LoopCount = 0;
  543. else
  544. LoopCount++;
  545. } while(LoopCount < 2);
  546. return IRQ_RETVAL(handled);
  547. }
  548. static int ac_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  549. { /* @ ADG ou ATO selon le cas */
  550. int i;
  551. unsigned char IndexCard;
  552. void __iomem *pmem;
  553. int ret = 0;
  554. volatile unsigned char byte_reset_it;
  555. struct st_ram_io *adgl;
  556. void __user *argp = (void __user *)arg;
  557. /* In general, the device is only openable by root anyway, so we're not
  558. particularly concerned that bogus ioctls can flood the console. */
  559. adgl = kmalloc(sizeof(struct st_ram_io), GFP_KERNEL);
  560. if (!adgl)
  561. return -ENOMEM;
  562. if (copy_from_user(adgl, argp, sizeof(struct st_ram_io))) {
  563. kfree(adgl);
  564. return -EFAULT;
  565. }
  566. IndexCard = adgl->num_card-1;
  567. if(cmd != 6 && ((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. ret = -ENOTTY;
  676. break;
  677. }
  678. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  679. kfree(adgl);
  680. return 0;
  681. }