applicom.c 24 KB

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