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