applicom.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Derived from Applicom driver ac.c for SCO Unix */
  3. /* Ported by David Woodhouse, Axiom (Cambridge) Ltd. */
  4. /* dwmw2@infradead.org 30/8/98 */
  5. /* $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $ */
  6. /* This module is for Linux 2.1 and 2.2 series kernels. */
  7. /*****************************************************************************/
  8. /* J PAGET 18/02/94 passage V2.4.2 ioctl avec code 2 reset to les interrupt */
  9. /* ceci pour reseter correctement apres une sortie sauvage */
  10. /* J PAGET 02/05/94 passage V2.4.3 dans le traitement de d'interruption, */
  11. /* LoopCount n'etait pas initialise a 0. */
  12. /* F LAFORSE 04/07/95 version V2.6.0 lecture bidon apres acces a une carte */
  13. /* pour liberer le bus */
  14. /* J.PAGET 19/11/95 version V2.6.1 Nombre, addresse,irq n'est plus configure */
  15. /* et passe en argument a acinit, mais est scrute sur le bus pour s'adapter */
  16. /* au nombre de cartes presentes sur le bus. IOCL code 6 affichait V2.4.3 */
  17. /* F.LAFORSE 28/11/95 creation de fichiers acXX.o avec les differentes */
  18. /* addresses de base des cartes, IOCTL 6 plus complet */
  19. /* J.PAGET le 19/08/96 copie de la version V2.6 en V2.8.0 sans modification */
  20. /* de code autre que le texte V2.6.1 en V2.8.0 */
  21. /*****************************************************************************/
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/slab.h>
  27. #include <linux/errno.h>
  28. #include <linux/mutex.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/pci.h>
  31. #include <linux/wait.h>
  32. #include <linux/init.h>
  33. #include <linux/fs.h>
  34. #include <linux/nospec.h>
  35. #include <asm/io.h>
  36. #include <linux/uaccess.h>
  37. #include "applicom.h"
  38. /* NOTE: We use for loops with {write,read}b() instead of
  39. memcpy_{from,to}io throughout this driver. This is because
  40. the board doesn't correctly handle word accesses - only
  41. bytes.
  42. */
  43. #undef DEBUG
  44. #define MAX_BOARD 8 /* maximum of pc board possible */
  45. #define MAX_ISA_BOARD 4
  46. #define LEN_RAM_IO 0x800
  47. #ifndef PCI_VENDOR_ID_APPLICOM
  48. #define PCI_VENDOR_ID_APPLICOM 0x1389
  49. #define PCI_DEVICE_ID_APPLICOM_PCIGENERIC 0x0001
  50. #define PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN 0x0002
  51. #define PCI_DEVICE_ID_APPLICOM_PCI2000PFB 0x0003
  52. #endif
  53. static DEFINE_MUTEX(ac_mutex);
  54. static char *applicom_pci_devnames[] = {
  55. "PCI board",
  56. "PCI2000IBS / PCI2000CAN",
  57. "PCI2000PFB"
  58. };
  59. static const struct pci_device_id applicom_pci_tbl[] = {
  60. { PCI_VDEVICE(APPLICOM, PCI_DEVICE_ID_APPLICOM_PCIGENERIC) },
  61. { PCI_VDEVICE(APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN) },
  62. { PCI_VDEVICE(APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000PFB) },
  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_ALIAS_MISCDEV(AC_MINOR);
  70. MODULE_SUPPORTED_DEVICE("ac");
  71. static struct applicom_board {
  72. unsigned long PhysIO;
  73. void __iomem *RamIO;
  74. wait_queue_head_t FlagSleepSend;
  75. long irq;
  76. spinlock_t mutex;
  77. } apbs[MAX_BOARD];
  78. static unsigned int irq = 0; /* interrupt number IRQ */
  79. static unsigned long mem = 0; /* physical segment of board */
  80. module_param_hw(irq, uint, irq, 0);
  81. MODULE_PARM_DESC(irq, "IRQ of the Applicom board");
  82. module_param_hw(mem, ulong, iomem, 0);
  83. MODULE_PARM_DESC(mem, "Shared Memory Address of Applicom board");
  84. static unsigned int numboards; /* number of installed boards */
  85. static volatile unsigned char Dummy;
  86. static DECLARE_WAIT_QUEUE_HEAD(FlagSleepRec);
  87. static unsigned int WriteErrorCount; /* number of write error */
  88. static unsigned int ReadErrorCount; /* number of read error */
  89. static unsigned int DeviceErrorCount; /* number of device error */
  90. static ssize_t ac_read (struct file *, char __user *, size_t, loff_t *);
  91. static ssize_t ac_write (struct file *, const char __user *, size_t, loff_t *);
  92. static long ac_ioctl(struct file *, unsigned int, unsigned long);
  93. static irqreturn_t ac_interrupt(int, void *);
  94. static const struct file_operations ac_fops = {
  95. .owner = THIS_MODULE,
  96. .llseek = no_llseek,
  97. .read = ac_read,
  98. .write = ac_write,
  99. .unlocked_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. static void __exit applicom_exit(void)
  138. {
  139. unsigned int i;
  140. misc_deregister(&ac_miscdev);
  141. for (i = 0; i < MAX_BOARD; i++) {
  142. if (!apbs[i].RamIO)
  143. continue;
  144. if (apbs[i].irq)
  145. free_irq(apbs[i].irq, &dummy);
  146. iounmap(apbs[i].RamIO);
  147. }
  148. }
  149. static int __init applicom_init(void)
  150. {
  151. int i, numisa = 0;
  152. struct pci_dev *dev = NULL;
  153. void __iomem *RamIO;
  154. int boardno, ret;
  155. printk(KERN_INFO "Applicom driver: $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $\n");
  156. /* No mem and irq given - check for a PCI card */
  157. while ( (dev = pci_get_class(PCI_CLASS_OTHERS << 16, dev))) {
  158. if (!pci_match_id(applicom_pci_tbl, dev))
  159. continue;
  160. if (pci_enable_device(dev))
  161. return -EIO;
  162. RamIO = ioremap(pci_resource_start(dev, 0), LEN_RAM_IO);
  163. if (!RamIO) {
  164. printk(KERN_INFO "ac.o: Failed to ioremap PCI memory "
  165. "space at 0x%llx\n",
  166. (unsigned long long)pci_resource_start(dev, 0));
  167. pci_disable_device(dev);
  168. return -EIO;
  169. }
  170. printk(KERN_INFO "Applicom %s found at mem 0x%llx, irq %d\n",
  171. applicom_pci_devnames[dev->device-1],
  172. (unsigned long long)pci_resource_start(dev, 0),
  173. dev->irq);
  174. boardno = ac_register_board(pci_resource_start(dev, 0),
  175. RamIO, 0);
  176. if (!boardno) {
  177. printk(KERN_INFO "ac.o: PCI Applicom device doesn't have correct signature.\n");
  178. iounmap(RamIO);
  179. pci_disable_device(dev);
  180. continue;
  181. }
  182. if (request_irq(dev->irq, &ac_interrupt, IRQF_SHARED, "Applicom PCI", &dummy)) {
  183. printk(KERN_INFO "Could not allocate IRQ %d for PCI Applicom device.\n", dev->irq);
  184. iounmap(RamIO);
  185. pci_disable_device(dev);
  186. apbs[boardno - 1].RamIO = NULL;
  187. continue;
  188. }
  189. /* Enable interrupts. */
  190. writeb(0x40, apbs[boardno - 1].RamIO + RAM_IT_FROM_PC);
  191. apbs[boardno - 1].irq = dev->irq;
  192. }
  193. /* Finished with PCI cards. If none registered,
  194. * and there was no mem/irq specified, exit */
  195. if (!mem || !irq) {
  196. if (numboards)
  197. goto fin;
  198. else {
  199. printk(KERN_INFO "ac.o: No PCI boards found.\n");
  200. printk(KERN_INFO "ac.o: For an ISA board you must supply memory and irq parameters.\n");
  201. return -ENXIO;
  202. }
  203. }
  204. /* Now try the specified ISA cards */
  205. for (i = 0; i < MAX_ISA_BOARD; i++) {
  206. RamIO = ioremap(mem + (LEN_RAM_IO * i), LEN_RAM_IO);
  207. if (!RamIO) {
  208. printk(KERN_INFO "ac.o: Failed to ioremap the ISA card's memory space (slot #%d)\n", i + 1);
  209. continue;
  210. }
  211. if (!(boardno = ac_register_board((unsigned long)mem+ (LEN_RAM_IO*i),
  212. RamIO,i+1))) {
  213. iounmap(RamIO);
  214. continue;
  215. }
  216. printk(KERN_NOTICE "Applicom ISA card found at mem 0x%lx, irq %d\n", mem + (LEN_RAM_IO*i), irq);
  217. if (!numisa) {
  218. if (request_irq(irq, &ac_interrupt, IRQF_SHARED, "Applicom ISA", &dummy)) {
  219. printk(KERN_WARNING "Could not allocate IRQ %d for ISA Applicom device.\n", irq);
  220. iounmap(RamIO);
  221. apbs[boardno - 1].RamIO = NULL;
  222. }
  223. else
  224. apbs[boardno - 1].irq = irq;
  225. }
  226. else
  227. apbs[boardno - 1].irq = 0;
  228. numisa++;
  229. }
  230. if (!numisa)
  231. printk(KERN_WARNING "ac.o: No valid ISA Applicom boards found "
  232. "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. ret = misc_register(&ac_miscdev);
  240. if (ret) {
  241. printk(KERN_WARNING "ac.o: Unable to register misc device\n");
  242. goto out;
  243. }
  244. for (i = 0; i < MAX_BOARD; i++) {
  245. int serial;
  246. char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
  247. if (!apbs[i].RamIO)
  248. continue;
  249. for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
  250. boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
  251. boardname[serial] = 0;
  252. printk(KERN_INFO "Applicom board %d: %s, PROM V%d.%d",
  253. i+1, boardname,
  254. (int)(readb(apbs[i].RamIO + VERS) >> 4),
  255. (int)(readb(apbs[i].RamIO + VERS) & 0xF));
  256. serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
  257. (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
  258. (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
  259. if (serial != 0)
  260. printk(" S/N %d\n", serial);
  261. else
  262. printk("\n");
  263. }
  264. return 0;
  265. }
  266. else
  267. return -ENXIO;
  268. out:
  269. for (i = 0; i < MAX_BOARD; i++) {
  270. if (!apbs[i].RamIO)
  271. continue;
  272. if (apbs[i].irq)
  273. free_irq(apbs[i].irq, &dummy);
  274. iounmap(apbs[i].RamIO);
  275. }
  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 (IndexCard >= MAX_BOARD)
  310. return -EINVAL;
  311. IndexCard = array_index_nospec(IndexCard, MAX_BOARD);
  312. if (!apbs[IndexCard].RamIO)
  313. return -EINVAL;
  314. #ifdef DEBUG
  315. printk("Write to applicom card #%d. struct st_ram_io follows:",
  316. IndexCard+1);
  317. for (c = 0; c < sizeof(struct st_ram_io);) {
  318. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
  319. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  320. printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
  321. }
  322. }
  323. printk("\nstruct mailbox follows:");
  324. for (c = 0; c < sizeof(struct mailbox);) {
  325. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
  326. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  327. printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
  328. }
  329. }
  330. printk("\n");
  331. #endif
  332. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  333. /* Test octet ready correct */
  334. if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
  335. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  336. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  337. printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
  338. IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
  339. DeviceErrorCount++;
  340. return -EIO;
  341. }
  342. /* Place ourselves on the wait queue */
  343. set_current_state(TASK_INTERRUPTIBLE);
  344. add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  345. /* Check whether the card is ready for us */
  346. while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
  347. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  348. /* It's busy. Sleep. */
  349. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  350. schedule();
  351. if (signal_pending(current)) {
  352. remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
  353. &wait);
  354. return -EINTR;
  355. }
  356. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  357. set_current_state(TASK_INTERRUPTIBLE);
  358. }
  359. /* We may not have actually slept */
  360. set_current_state(TASK_RUNNING);
  361. remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  362. writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  363. /* Which is best - lock down the pages with rawio and then
  364. copy directly, or use bounce buffers? For now we do the latter
  365. because it works with 2.2 still */
  366. {
  367. unsigned char *from = (unsigned char *) &tmpmailbox;
  368. void __iomem *to = apbs[IndexCard].RamIO + RAM_FROM_PC;
  369. int c;
  370. for (c = 0; c < sizeof(struct mailbox); c++)
  371. writeb(*(from++), to++);
  372. }
  373. writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
  374. writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
  375. writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
  376. writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  377. writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  378. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  379. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  380. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  381. return 0;
  382. }
  383. static int do_ac_read(int IndexCard, char __user *buf,
  384. struct st_ram_io *st_loc, struct mailbox *mailbox)
  385. {
  386. void __iomem *from = apbs[IndexCard].RamIO + RAM_TO_PC;
  387. unsigned char *to = (unsigned char *)mailbox;
  388. #ifdef DEBUG
  389. int c;
  390. #endif
  391. st_loc->tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
  392. st_loc->numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  393. {
  394. int c;
  395. for (c = 0; c < sizeof(struct mailbox); c++)
  396. *(to++) = readb(from++);
  397. }
  398. writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  399. writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
  400. writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  401. writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
  402. apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
  403. writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  404. writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
  405. writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  406. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  407. #ifdef DEBUG
  408. printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
  409. for (c = 0; c < sizeof(struct st_ram_io);) {
  410. printk("\n%5.5X: %2.2X", c, ((unsigned char *)st_loc)[c]);
  411. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  412. printk(" %2.2X", ((unsigned char *)st_loc)[c]);
  413. }
  414. }
  415. printk("\nstruct mailbox follows:");
  416. for (c = 0; c < sizeof(struct mailbox);) {
  417. printk("\n%5.5X: %2.2X", c, ((unsigned char *)mailbox)[c]);
  418. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  419. printk(" %2.2X", ((unsigned char *)mailbox)[c]);
  420. }
  421. }
  422. printk("\n");
  423. #endif
  424. return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
  425. }
  426. static ssize_t ac_read (struct file *filp, char __user *buf, size_t count, loff_t *ptr)
  427. {
  428. unsigned long flags;
  429. unsigned int i;
  430. unsigned char tmp;
  431. int ret = 0;
  432. DECLARE_WAITQUEUE(wait, current);
  433. #ifdef DEBUG
  434. int loopcount=0;
  435. #endif
  436. /* No need to ratelimit this. Only root can trigger it anyway */
  437. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  438. printk( KERN_WARNING "Hmmm. read() of Applicom card, length %zd != expected %zd\n",
  439. count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
  440. return -EINVAL;
  441. }
  442. while(1) {
  443. /* Stick ourself on the wait queue */
  444. set_current_state(TASK_INTERRUPTIBLE);
  445. add_wait_queue(&FlagSleepRec, &wait);
  446. /* Scan each board, looking for one which has a packet for us */
  447. for (i=0; i < MAX_BOARD; i++) {
  448. if (!apbs[i].RamIO)
  449. continue;
  450. spin_lock_irqsave(&apbs[i].mutex, flags);
  451. tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
  452. if (tmp == 2) {
  453. struct st_ram_io st_loc;
  454. struct mailbox mailbox;
  455. /* Got a packet for us */
  456. memset(&st_loc, 0, sizeof(st_loc));
  457. ret = do_ac_read(i, buf, &st_loc, &mailbox);
  458. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  459. set_current_state(TASK_RUNNING);
  460. remove_wait_queue(&FlagSleepRec, &wait);
  461. if (copy_to_user(buf, &st_loc, sizeof(st_loc)))
  462. return -EFAULT;
  463. if (copy_to_user(buf + sizeof(st_loc), &mailbox, sizeof(mailbox)))
  464. return -EFAULT;
  465. return tmp;
  466. }
  467. if (tmp > 2) {
  468. /* Got an error */
  469. Dummy = readb(apbs[i].RamIO + VERS);
  470. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  471. set_current_state(TASK_RUNNING);
  472. remove_wait_queue(&FlagSleepRec, &wait);
  473. printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
  474. i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  475. DeviceErrorCount++;
  476. return -EIO;
  477. }
  478. /* Nothing for us. Try the next board */
  479. Dummy = readb(apbs[i].RamIO + VERS);
  480. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  481. } /* per board */
  482. /* OK - No boards had data for us. Sleep now */
  483. schedule();
  484. remove_wait_queue(&FlagSleepRec, &wait);
  485. if (signal_pending(current))
  486. return -EINTR;
  487. #ifdef DEBUG
  488. if (loopcount++ > 2) {
  489. printk(KERN_DEBUG "Looping in ac_read. loopcount %d\n", loopcount);
  490. }
  491. #endif
  492. }
  493. }
  494. static irqreturn_t ac_interrupt(int vec, void *dev_instance)
  495. {
  496. unsigned int i;
  497. unsigned int FlagInt;
  498. unsigned int LoopCount;
  499. int handled = 0;
  500. // printk("Applicom interrupt on IRQ %d occurred\n", vec);
  501. LoopCount = 0;
  502. do {
  503. FlagInt = 0;
  504. for (i = 0; i < MAX_BOARD; i++) {
  505. /* Skip if this board doesn't exist */
  506. if (!apbs[i].RamIO)
  507. continue;
  508. spin_lock(&apbs[i].mutex);
  509. /* Skip if this board doesn't want attention */
  510. if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
  511. spin_unlock(&apbs[i].mutex);
  512. continue;
  513. }
  514. handled = 1;
  515. FlagInt = 1;
  516. writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
  517. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
  518. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
  519. i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  520. DeviceErrorCount++;
  521. }
  522. if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
  523. (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
  524. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
  525. i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
  526. DeviceErrorCount++;
  527. }
  528. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) { /* mailbox sent by the card ? */
  529. if (waitqueue_active(&FlagSleepRec)) {
  530. wake_up_interruptible(&FlagSleepRec);
  531. }
  532. }
  533. if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) { /* ram i/o free for write by pc ? */
  534. if (waitqueue_active(&apbs[i].FlagSleepSend)) { /* process sleep during read ? */
  535. wake_up_interruptible(&apbs[i].FlagSleepSend);
  536. }
  537. }
  538. Dummy = readb(apbs[i].RamIO + VERS);
  539. if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
  540. /* There's another int waiting on this card */
  541. spin_unlock(&apbs[i].mutex);
  542. i--;
  543. } else {
  544. spin_unlock(&apbs[i].mutex);
  545. }
  546. }
  547. if (FlagInt)
  548. LoopCount = 0;
  549. else
  550. LoopCount++;
  551. } while(LoopCount < 2);
  552. return IRQ_RETVAL(handled);
  553. }
  554. static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  555. { /* @ ADG ou ATO selon le cas */
  556. int i;
  557. unsigned char IndexCard;
  558. void __iomem *pmem;
  559. int ret = 0;
  560. static int warncount = 10;
  561. volatile unsigned char byte_reset_it;
  562. struct st_ram_io *adgl;
  563. void __user *argp = (void __user *)arg;
  564. /* In general, the device is only openable by root anyway, so we're not
  565. particularly concerned that bogus ioctls can flood the console. */
  566. adgl = memdup_user(argp, sizeof(struct st_ram_io));
  567. if (IS_ERR(adgl))
  568. return PTR_ERR(adgl);
  569. mutex_lock(&ac_mutex);
  570. IndexCard = adgl->num_card-1;
  571. if (cmd != 6 && IndexCard >= MAX_BOARD)
  572. goto err;
  573. IndexCard = array_index_nospec(IndexCard, MAX_BOARD);
  574. if (cmd != 6 && !apbs[IndexCard].RamIO)
  575. goto err;
  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[i].RamIO + VERS) >> 4),
  651. (int)(readb(apbs[i].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. mutex_unlock(&ac_mutex);
  681. return 0;
  682. err:
  683. if (warncount) {
  684. pr_warn("APPLICOM driver IOCTL, bad board number %d\n",
  685. (int)IndexCard + 1);
  686. warncount--;
  687. }
  688. kfree(adgl);
  689. mutex_unlock(&ac_mutex);
  690. return -EINVAL;
  691. }