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. /* [email protected] 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. 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; /* interrupt number IRQ */
  78. static unsigned long mem; /* physical segment of board */
  79. module_param_hw(irq, uint, irq, 0);
  80. MODULE_PARM_DESC(irq, "IRQ of the Applicom board");
  81. module_param_hw(mem, ulong, iomem, 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. pci_dev_put(dev);
  161. return -EIO;
  162. }
  163. RamIO = ioremap(pci_resource_start(dev, 0), LEN_RAM_IO);
  164. if (!RamIO) {
  165. printk(KERN_INFO "ac.o: Failed to ioremap PCI memory "
  166. "space at 0x%llx\n",
  167. (unsigned long long)pci_resource_start(dev, 0));
  168. pci_disable_device(dev);
  169. pci_dev_put(dev);
  170. return -EIO;
  171. }
  172. printk(KERN_INFO "Applicom %s found at mem 0x%llx, irq %d\n",
  173. applicom_pci_devnames[dev->device-1],
  174. (unsigned long long)pci_resource_start(dev, 0),
  175. dev->irq);
  176. boardno = ac_register_board(pci_resource_start(dev, 0),
  177. RamIO, 0);
  178. if (!boardno) {
  179. printk(KERN_INFO "ac.o: PCI Applicom device doesn't have correct signature.\n");
  180. iounmap(RamIO);
  181. pci_disable_device(dev);
  182. continue;
  183. }
  184. if (request_irq(dev->irq, &ac_interrupt, IRQF_SHARED, "Applicom PCI", &dummy)) {
  185. printk(KERN_INFO "Could not allocate IRQ %d for PCI Applicom device.\n", dev->irq);
  186. iounmap(RamIO);
  187. pci_disable_device(dev);
  188. apbs[boardno - 1].RamIO = NULL;
  189. continue;
  190. }
  191. /* Enable interrupts. */
  192. writeb(0x40, apbs[boardno - 1].RamIO + RAM_IT_FROM_PC);
  193. apbs[boardno - 1].irq = dev->irq;
  194. }
  195. /* Finished with PCI cards. If none registered,
  196. * and there was no mem/irq specified, exit */
  197. if (!mem || !irq) {
  198. if (numboards)
  199. goto fin;
  200. else {
  201. printk(KERN_INFO "ac.o: No PCI boards found.\n");
  202. printk(KERN_INFO "ac.o: For an ISA board you must supply memory and irq parameters.\n");
  203. return -ENXIO;
  204. }
  205. }
  206. /* Now try the specified ISA cards */
  207. for (i = 0; i < MAX_ISA_BOARD; i++) {
  208. RamIO = ioremap(mem + (LEN_RAM_IO * i), LEN_RAM_IO);
  209. if (!RamIO) {
  210. printk(KERN_INFO "ac.o: Failed to ioremap the ISA card's memory space (slot #%d)\n", i + 1);
  211. continue;
  212. }
  213. if (!(boardno = ac_register_board((unsigned long)mem+ (LEN_RAM_IO*i),
  214. RamIO,i+1))) {
  215. iounmap(RamIO);
  216. continue;
  217. }
  218. printk(KERN_NOTICE "Applicom ISA card found at mem 0x%lx, irq %d\n", mem + (LEN_RAM_IO*i), irq);
  219. if (!numisa) {
  220. if (request_irq(irq, &ac_interrupt, IRQF_SHARED, "Applicom ISA", &dummy)) {
  221. printk(KERN_WARNING "Could not allocate IRQ %d for ISA Applicom device.\n", irq);
  222. iounmap(RamIO);
  223. apbs[boardno - 1].RamIO = NULL;
  224. }
  225. else
  226. apbs[boardno - 1].irq = irq;
  227. }
  228. else
  229. apbs[boardno - 1].irq = 0;
  230. numisa++;
  231. }
  232. if (!numisa)
  233. printk(KERN_WARNING "ac.o: No valid ISA Applicom boards found "
  234. "at mem 0x%lx\n", mem);
  235. fin:
  236. init_waitqueue_head(&FlagSleepRec);
  237. WriteErrorCount = 0;
  238. ReadErrorCount = 0;
  239. DeviceErrorCount = 0;
  240. if (numboards) {
  241. ret = misc_register(&ac_miscdev);
  242. if (ret) {
  243. printk(KERN_WARNING "ac.o: Unable to register misc device\n");
  244. goto out;
  245. }
  246. for (i = 0; i < MAX_BOARD; i++) {
  247. int serial;
  248. char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
  249. if (!apbs[i].RamIO)
  250. continue;
  251. for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
  252. boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
  253. boardname[serial] = 0;
  254. printk(KERN_INFO "Applicom board %d: %s, PROM V%d.%d",
  255. i+1, boardname,
  256. (int)(readb(apbs[i].RamIO + VERS) >> 4),
  257. (int)(readb(apbs[i].RamIO + VERS) & 0xF));
  258. serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
  259. (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
  260. (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
  261. if (serial != 0)
  262. printk(" S/N %d\n", serial);
  263. else
  264. printk("\n");
  265. }
  266. return 0;
  267. }
  268. else
  269. return -ENXIO;
  270. out:
  271. for (i = 0; i < MAX_BOARD; i++) {
  272. if (!apbs[i].RamIO)
  273. continue;
  274. if (apbs[i].irq)
  275. free_irq(apbs[i].irq, &dummy);
  276. iounmap(apbs[i].RamIO);
  277. }
  278. return ret;
  279. }
  280. module_init(applicom_init);
  281. module_exit(applicom_exit);
  282. static ssize_t ac_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  283. {
  284. unsigned int NumCard; /* Board number 1 -> 8 */
  285. unsigned int IndexCard; /* Index board number 0 -> 7 */
  286. unsigned char TicCard; /* Board TIC to send */
  287. unsigned long flags; /* Current priority */
  288. struct st_ram_io st_loc;
  289. struct mailbox tmpmailbox;
  290. #ifdef DEBUG
  291. int c;
  292. #endif
  293. DECLARE_WAITQUEUE(wait, current);
  294. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  295. static int warncount = 5;
  296. if (warncount) {
  297. printk(KERN_INFO "Hmmm. write() of Applicom card, length %zd != expected %zd\n",
  298. count, sizeof(struct st_ram_io) + sizeof(struct mailbox));
  299. warncount--;
  300. }
  301. return -EINVAL;
  302. }
  303. if(copy_from_user(&st_loc, buf, sizeof(struct st_ram_io)))
  304. return -EFAULT;
  305. if(copy_from_user(&tmpmailbox, &buf[sizeof(struct st_ram_io)],
  306. sizeof(struct mailbox)))
  307. return -EFAULT;
  308. NumCard = st_loc.num_card; /* board number to send */
  309. TicCard = st_loc.tic_des_from_pc; /* tic number to send */
  310. IndexCard = NumCard - 1;
  311. if (IndexCard >= MAX_BOARD)
  312. return -EINVAL;
  313. IndexCard = array_index_nospec(IndexCard, MAX_BOARD);
  314. if (!apbs[IndexCard].RamIO)
  315. return -EINVAL;
  316. #ifdef DEBUG
  317. printk("Write to applicom card #%d. struct st_ram_io follows:",
  318. IndexCard+1);
  319. for (c = 0; c < sizeof(struct st_ram_io);) {
  320. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
  321. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  322. printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
  323. }
  324. }
  325. printk("\nstruct mailbox follows:");
  326. for (c = 0; c < sizeof(struct mailbox);) {
  327. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
  328. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  329. printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
  330. }
  331. }
  332. printk("\n");
  333. #endif
  334. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  335. /* Test octet ready correct */
  336. if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
  337. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  338. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  339. printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
  340. IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
  341. DeviceErrorCount++;
  342. return -EIO;
  343. }
  344. /* Place ourselves on the wait queue */
  345. set_current_state(TASK_INTERRUPTIBLE);
  346. add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  347. /* Check whether the card is ready for us */
  348. while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
  349. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  350. /* It's busy. Sleep. */
  351. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  352. schedule();
  353. if (signal_pending(current)) {
  354. remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
  355. &wait);
  356. return -EINTR;
  357. }
  358. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  359. set_current_state(TASK_INTERRUPTIBLE);
  360. }
  361. /* We may not have actually slept */
  362. set_current_state(TASK_RUNNING);
  363. remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  364. writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  365. /* Which is best - lock down the pages with rawio and then
  366. copy directly, or use bounce buffers? For now we do the latter
  367. because it works with 2.2 still */
  368. {
  369. unsigned char *from = (unsigned char *) &tmpmailbox;
  370. void __iomem *to = apbs[IndexCard].RamIO + RAM_FROM_PC;
  371. int c;
  372. for (c = 0; c < sizeof(struct mailbox); c++)
  373. writeb(*(from++), to++);
  374. }
  375. writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
  376. writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
  377. writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
  378. writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  379. writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  380. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  381. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  382. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  383. return 0;
  384. }
  385. static int do_ac_read(int IndexCard, char __user *buf,
  386. struct st_ram_io *st_loc, struct mailbox *mailbox)
  387. {
  388. void __iomem *from = apbs[IndexCard].RamIO + RAM_TO_PC;
  389. unsigned char *to = (unsigned char *)mailbox;
  390. #ifdef DEBUG
  391. int c;
  392. #endif
  393. st_loc->tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
  394. st_loc->numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  395. {
  396. int c;
  397. for (c = 0; c < sizeof(struct mailbox); c++)
  398. *(to++) = readb(from++);
  399. }
  400. writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  401. writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
  402. writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  403. writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
  404. apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
  405. writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  406. writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
  407. writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  408. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  409. #ifdef DEBUG
  410. printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
  411. for (c = 0; c < sizeof(struct st_ram_io);) {
  412. printk("\n%5.5X: %2.2X", c, ((unsigned char *)st_loc)[c]);
  413. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  414. printk(" %2.2X", ((unsigned char *)st_loc)[c]);
  415. }
  416. }
  417. printk("\nstruct mailbox follows:");
  418. for (c = 0; c < sizeof(struct mailbox);) {
  419. printk("\n%5.5X: %2.2X", c, ((unsigned char *)mailbox)[c]);
  420. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  421. printk(" %2.2X", ((unsigned char *)mailbox)[c]);
  422. }
  423. }
  424. printk("\n");
  425. #endif
  426. return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
  427. }
  428. static ssize_t ac_read (struct file *filp, char __user *buf, size_t count, loff_t *ptr)
  429. {
  430. unsigned long flags;
  431. unsigned int i;
  432. unsigned char tmp;
  433. int ret = 0;
  434. DECLARE_WAITQUEUE(wait, current);
  435. #ifdef DEBUG
  436. int loopcount=0;
  437. #endif
  438. /* No need to ratelimit this. Only root can trigger it anyway */
  439. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  440. printk( KERN_WARNING "Hmmm. read() of Applicom card, length %zd != expected %zd\n",
  441. count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
  442. return -EINVAL;
  443. }
  444. while(1) {
  445. /* Stick ourself on the wait queue */
  446. set_current_state(TASK_INTERRUPTIBLE);
  447. add_wait_queue(&FlagSleepRec, &wait);
  448. /* Scan each board, looking for one which has a packet for us */
  449. for (i=0; i < MAX_BOARD; i++) {
  450. if (!apbs[i].RamIO)
  451. continue;
  452. spin_lock_irqsave(&apbs[i].mutex, flags);
  453. tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
  454. if (tmp == 2) {
  455. struct st_ram_io st_loc;
  456. struct mailbox mailbox;
  457. /* Got a packet for us */
  458. memset(&st_loc, 0, sizeof(st_loc));
  459. ret = do_ac_read(i, buf, &st_loc, &mailbox);
  460. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  461. set_current_state(TASK_RUNNING);
  462. remove_wait_queue(&FlagSleepRec, &wait);
  463. if (copy_to_user(buf, &st_loc, sizeof(st_loc)))
  464. return -EFAULT;
  465. if (copy_to_user(buf + sizeof(st_loc), &mailbox, sizeof(mailbox)))
  466. return -EFAULT;
  467. return tmp;
  468. }
  469. if (tmp > 2) {
  470. /* Got an error */
  471. Dummy = readb(apbs[i].RamIO + VERS);
  472. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  473. set_current_state(TASK_RUNNING);
  474. remove_wait_queue(&FlagSleepRec, &wait);
  475. printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
  476. i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  477. DeviceErrorCount++;
  478. return -EIO;
  479. }
  480. /* Nothing for us. Try the next board */
  481. Dummy = readb(apbs[i].RamIO + VERS);
  482. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  483. } /* per board */
  484. /* OK - No boards had data for us. Sleep now */
  485. schedule();
  486. remove_wait_queue(&FlagSleepRec, &wait);
  487. if (signal_pending(current))
  488. return -EINTR;
  489. #ifdef DEBUG
  490. if (loopcount++ > 2) {
  491. printk(KERN_DEBUG "Looping in ac_read. loopcount %d\n", loopcount);
  492. }
  493. #endif
  494. }
  495. }
  496. static irqreturn_t ac_interrupt(int vec, void *dev_instance)
  497. {
  498. unsigned int i;
  499. unsigned int FlagInt;
  500. unsigned int LoopCount;
  501. int handled = 0;
  502. // printk("Applicom interrupt on IRQ %d occurred\n", vec);
  503. LoopCount = 0;
  504. do {
  505. FlagInt = 0;
  506. for (i = 0; i < MAX_BOARD; i++) {
  507. /* Skip if this board doesn't exist */
  508. if (!apbs[i].RamIO)
  509. continue;
  510. spin_lock(&apbs[i].mutex);
  511. /* Skip if this board doesn't want attention */
  512. if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
  513. spin_unlock(&apbs[i].mutex);
  514. continue;
  515. }
  516. handled = 1;
  517. FlagInt = 1;
  518. writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
  519. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
  520. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
  521. i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  522. DeviceErrorCount++;
  523. }
  524. if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
  525. (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
  526. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
  527. i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
  528. DeviceErrorCount++;
  529. }
  530. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) { /* mailbox sent by the card ? */
  531. if (waitqueue_active(&FlagSleepRec)) {
  532. wake_up_interruptible(&FlagSleepRec);
  533. }
  534. }
  535. if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) { /* ram i/o free for write by pc ? */
  536. if (waitqueue_active(&apbs[i].FlagSleepSend)) { /* process sleep during read ? */
  537. wake_up_interruptible(&apbs[i].FlagSleepSend);
  538. }
  539. }
  540. Dummy = readb(apbs[i].RamIO + VERS);
  541. if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
  542. /* There's another int waiting on this card */
  543. spin_unlock(&apbs[i].mutex);
  544. i--;
  545. } else {
  546. spin_unlock(&apbs[i].mutex);
  547. }
  548. }
  549. if (FlagInt)
  550. LoopCount = 0;
  551. else
  552. LoopCount++;
  553. } while(LoopCount < 2);
  554. return IRQ_RETVAL(handled);
  555. }
  556. static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  557. { /* @ ADG ou ATO selon le cas */
  558. int i;
  559. unsigned char IndexCard;
  560. void __iomem *pmem;
  561. int ret = 0;
  562. static int warncount = 10;
  563. volatile unsigned char byte_reset_it;
  564. struct st_ram_io *adgl;
  565. void __user *argp = (void __user *)arg;
  566. /* In general, the device is only openable by root anyway, so we're not
  567. particularly concerned that bogus ioctls can flood the console. */
  568. adgl = memdup_user(argp, sizeof(struct st_ram_io));
  569. if (IS_ERR(adgl))
  570. return PTR_ERR(adgl);
  571. mutex_lock(&ac_mutex);
  572. IndexCard = adgl->num_card-1;
  573. if (cmd != 6 && IndexCard >= MAX_BOARD)
  574. goto err;
  575. IndexCard = array_index_nospec(IndexCard, MAX_BOARD);
  576. if (cmd != 6 && !apbs[IndexCard].RamIO)
  577. goto err;
  578. switch (cmd) {
  579. case 0:
  580. pmem = apbs[IndexCard].RamIO;
  581. for (i = 0; i < sizeof(struct st_ram_io); i++)
  582. ((unsigned char *)adgl)[i]=readb(pmem++);
  583. if (copy_to_user(argp, adgl, sizeof(struct st_ram_io)))
  584. ret = -EFAULT;
  585. break;
  586. case 1:
  587. pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
  588. for (i = 0; i < 4; i++)
  589. adgl->conf_end_test[i] = readb(pmem++);
  590. for (i = 0; i < 2; i++)
  591. adgl->error_code[i] = readb(pmem++);
  592. for (i = 0; i < 4; i++)
  593. adgl->parameter_error[i] = readb(pmem++);
  594. pmem = apbs[IndexCard].RamIO + VERS;
  595. adgl->vers = readb(pmem);
  596. pmem = apbs[IndexCard].RamIO + TYPE_CARD;
  597. for (i = 0; i < 20; i++)
  598. adgl->reserv1[i] = readb(pmem++);
  599. *(int *)&adgl->reserv1[20] =
  600. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER) << 16) +
  601. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 1) << 8) +
  602. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 2) );
  603. if (copy_to_user(argp, adgl, sizeof(struct st_ram_io)))
  604. ret = -EFAULT;
  605. break;
  606. case 2:
  607. pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
  608. for (i = 0; i < 10; i++)
  609. writeb(0xff, pmem++);
  610. writeb(adgl->data_from_pc_ready,
  611. apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  612. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  613. for (i = 0; i < MAX_BOARD; i++) {
  614. if (apbs[i].RamIO) {
  615. byte_reset_it = readb(apbs[i].RamIO + RAM_IT_TO_PC);
  616. }
  617. }
  618. break;
  619. case 3:
  620. pmem = apbs[IndexCard].RamIO + TIC_DES_FROM_PC;
  621. writeb(adgl->tic_des_from_pc, pmem);
  622. break;
  623. case 4:
  624. pmem = apbs[IndexCard].RamIO + TIC_OWNER_TO_PC;
  625. adgl->tic_owner_to_pc = readb(pmem++);
  626. adgl->numcard_owner_to_pc = readb(pmem);
  627. if (copy_to_user(argp, adgl,sizeof(struct st_ram_io)))
  628. ret = -EFAULT;
  629. break;
  630. case 5:
  631. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  632. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  633. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  634. writeb(4, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  635. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  636. break;
  637. case 6:
  638. printk(KERN_INFO "APPLICOM driver release .... V2.8.0 ($Revision: 1.30 $)\n");
  639. printk(KERN_INFO "Number of installed boards . %d\n", (int) numboards);
  640. printk(KERN_INFO "Segment of board ........... %X\n", (int) mem);
  641. printk(KERN_INFO "Interrupt IRQ number ....... %d\n", (int) irq);
  642. for (i = 0; i < MAX_BOARD; i++) {
  643. int serial;
  644. char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
  645. if (!apbs[i].RamIO)
  646. continue;
  647. for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
  648. boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
  649. boardname[serial] = 0;
  650. printk(KERN_INFO "Prom version board %d ....... V%d.%d %s",
  651. i+1,
  652. (int)(readb(apbs[i].RamIO + VERS) >> 4),
  653. (int)(readb(apbs[i].RamIO + VERS) & 0xF),
  654. boardname);
  655. serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
  656. (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
  657. (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
  658. if (serial != 0)
  659. printk(" S/N %d\n", serial);
  660. else
  661. printk("\n");
  662. }
  663. if (DeviceErrorCount != 0)
  664. printk(KERN_INFO "DeviceErrorCount ........... %d\n", DeviceErrorCount);
  665. if (ReadErrorCount != 0)
  666. printk(KERN_INFO "ReadErrorCount ............. %d\n", ReadErrorCount);
  667. if (WriteErrorCount != 0)
  668. printk(KERN_INFO "WriteErrorCount ............ %d\n", WriteErrorCount);
  669. if (waitqueue_active(&FlagSleepRec))
  670. printk(KERN_INFO "Process in read pending\n");
  671. for (i = 0; i < MAX_BOARD; i++) {
  672. if (apbs[i].RamIO && waitqueue_active(&apbs[i].FlagSleepSend))
  673. printk(KERN_INFO "Process in write pending board %d\n",i+1);
  674. }
  675. break;
  676. default:
  677. ret = -ENOTTY;
  678. break;
  679. }
  680. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  681. kfree(adgl);
  682. mutex_unlock(&ac_mutex);
  683. return ret;
  684. err:
  685. if (warncount) {
  686. pr_warn("APPLICOM driver IOCTL, bad board number %d\n",
  687. (int)IndexCard + 1);
  688. warncount--;
  689. }
  690. kfree(adgl);
  691. mutex_unlock(&ac_mutex);
  692. return -EINVAL;
  693. }