applicom.c 24 KB

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