applicom.c 24 KB

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