applicom.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /* Derived from Applicom driver ac.c for SCO Unix */
  2. /* Ported by David Woodhouse, Axiom (Cambridge) Ltd. */
  3. /* dwmw2@infradead.org 30/8/98 */
  4. /* $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $ */
  5. /* This module is for Linux 2.1 and 2.2 series kernels. */
  6. /*****************************************************************************/
  7. /* J PAGET 18/02/94 passage V2.4.2 ioctl avec code 2 reset to les interrupt */
  8. /* ceci pour reseter correctement apres une sortie sauvage */
  9. /* J PAGET 02/05/94 passage V2.4.3 dans le traitement de d'interruption, */
  10. /* LoopCount n'etait pas initialise a 0. */
  11. /* F LAFORSE 04/07/95 version V2.6.0 lecture bidon apres acces a une carte */
  12. /* pour liberer le bus */
  13. /* J.PAGET 19/11/95 version V2.6.1 Nombre, addresse,irq n'est plus configure */
  14. /* et passe en argument a acinit, mais est scrute sur le bus pour s'adapter */
  15. /* au nombre de cartes presentes sur le bus. IOCL code 6 affichait V2.4.3 */
  16. /* F.LAFORSE 28/11/95 creation de fichiers acXX.o avec les differentes */
  17. /* 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. return ret;
  276. }
  277. module_init(applicom_init);
  278. module_exit(applicom_exit);
  279. static ssize_t ac_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  280. {
  281. unsigned int NumCard; /* Board number 1 -> 8 */
  282. unsigned int IndexCard; /* Index board number 0 -> 7 */
  283. unsigned char TicCard; /* Board TIC to send */
  284. unsigned long flags; /* Current priority */
  285. struct st_ram_io st_loc;
  286. struct mailbox tmpmailbox;
  287. #ifdef DEBUG
  288. int c;
  289. #endif
  290. DECLARE_WAITQUEUE(wait, current);
  291. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  292. static int warncount = 5;
  293. if (warncount) {
  294. printk(KERN_INFO "Hmmm. write() of Applicom card, length %zd != expected %zd\n",
  295. count, sizeof(struct st_ram_io) + sizeof(struct mailbox));
  296. warncount--;
  297. }
  298. return -EINVAL;
  299. }
  300. if(copy_from_user(&st_loc, buf, sizeof(struct st_ram_io)))
  301. return -EFAULT;
  302. if(copy_from_user(&tmpmailbox, &buf[sizeof(struct st_ram_io)],
  303. sizeof(struct mailbox)))
  304. return -EFAULT;
  305. NumCard = st_loc.num_card; /* board number to send */
  306. TicCard = st_loc.tic_des_from_pc; /* tic number to send */
  307. IndexCard = NumCard - 1;
  308. if((NumCard < 1) || (NumCard > MAX_BOARD) || !apbs[IndexCard].RamIO)
  309. return -EINVAL;
  310. #ifdef DEBUG
  311. printk("Write to applicom card #%d. struct st_ram_io follows:",
  312. IndexCard+1);
  313. for (c = 0; c < sizeof(struct st_ram_io);) {
  314. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
  315. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  316. printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
  317. }
  318. }
  319. printk("\nstruct mailbox follows:");
  320. for (c = 0; c < sizeof(struct mailbox);) {
  321. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
  322. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  323. printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
  324. }
  325. }
  326. printk("\n");
  327. #endif
  328. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  329. /* Test octet ready correct */
  330. if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
  331. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  332. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  333. printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
  334. IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
  335. DeviceErrorCount++;
  336. return -EIO;
  337. }
  338. /* Place ourselves on the wait queue */
  339. set_current_state(TASK_INTERRUPTIBLE);
  340. add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  341. /* Check whether the card is ready for us */
  342. while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
  343. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  344. /* It's busy. Sleep. */
  345. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  346. schedule();
  347. if (signal_pending(current)) {
  348. remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
  349. &wait);
  350. return -EINTR;
  351. }
  352. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  353. set_current_state(TASK_INTERRUPTIBLE);
  354. }
  355. /* We may not have actually slept */
  356. set_current_state(TASK_RUNNING);
  357. remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  358. writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  359. /* Which is best - lock down the pages with rawio and then
  360. copy directly, or use bounce buffers? For now we do the latter
  361. because it works with 2.2 still */
  362. {
  363. unsigned char *from = (unsigned char *) &tmpmailbox;
  364. void __iomem *to = apbs[IndexCard].RamIO + RAM_FROM_PC;
  365. int c;
  366. for (c = 0; c < sizeof(struct mailbox); c++)
  367. writeb(*(from++), to++);
  368. }
  369. writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
  370. writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
  371. writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
  372. writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  373. writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  374. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  375. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  376. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  377. return 0;
  378. }
  379. static int do_ac_read(int IndexCard, char __user *buf,
  380. struct st_ram_io *st_loc, struct mailbox *mailbox)
  381. {
  382. void __iomem *from = apbs[IndexCard].RamIO + RAM_TO_PC;
  383. unsigned char *to = (unsigned char *)mailbox;
  384. #ifdef DEBUG
  385. int c;
  386. #endif
  387. st_loc->tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
  388. st_loc->numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  389. {
  390. int c;
  391. for (c = 0; c < sizeof(struct mailbox); c++)
  392. *(to++) = readb(from++);
  393. }
  394. writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  395. writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
  396. writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  397. writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
  398. apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
  399. writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  400. writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
  401. writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  402. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  403. #ifdef DEBUG
  404. printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
  405. for (c = 0; c < sizeof(struct st_ram_io);) {
  406. printk("\n%5.5X: %2.2X", c, ((unsigned char *)st_loc)[c]);
  407. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  408. printk(" %2.2X", ((unsigned char *)st_loc)[c]);
  409. }
  410. }
  411. printk("\nstruct mailbox follows:");
  412. for (c = 0; c < sizeof(struct mailbox);) {
  413. printk("\n%5.5X: %2.2X", c, ((unsigned char *)mailbox)[c]);
  414. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  415. printk(" %2.2X", ((unsigned char *)mailbox)[c]);
  416. }
  417. }
  418. printk("\n");
  419. #endif
  420. return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
  421. }
  422. static ssize_t ac_read (struct file *filp, char __user *buf, size_t count, loff_t *ptr)
  423. {
  424. unsigned long flags;
  425. unsigned int i;
  426. unsigned char tmp;
  427. int ret = 0;
  428. DECLARE_WAITQUEUE(wait, current);
  429. #ifdef DEBUG
  430. int loopcount=0;
  431. #endif
  432. /* No need to ratelimit this. Only root can trigger it anyway */
  433. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  434. printk( KERN_WARNING "Hmmm. read() of Applicom card, length %zd != expected %zd\n",
  435. count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
  436. return -EINVAL;
  437. }
  438. while(1) {
  439. /* Stick ourself on the wait queue */
  440. set_current_state(TASK_INTERRUPTIBLE);
  441. add_wait_queue(&FlagSleepRec, &wait);
  442. /* Scan each board, looking for one which has a packet for us */
  443. for (i=0; i < MAX_BOARD; i++) {
  444. if (!apbs[i].RamIO)
  445. continue;
  446. spin_lock_irqsave(&apbs[i].mutex, flags);
  447. tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
  448. if (tmp == 2) {
  449. struct st_ram_io st_loc;
  450. struct mailbox mailbox;
  451. /* Got a packet for us */
  452. memset(&st_loc, 0, sizeof(st_loc));
  453. ret = do_ac_read(i, buf, &st_loc, &mailbox);
  454. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  455. set_current_state(TASK_RUNNING);
  456. remove_wait_queue(&FlagSleepRec, &wait);
  457. if (copy_to_user(buf, &st_loc, sizeof(st_loc)))
  458. return -EFAULT;
  459. if (copy_to_user(buf + sizeof(st_loc), &mailbox, sizeof(mailbox)))
  460. return -EFAULT;
  461. return tmp;
  462. }
  463. if (tmp > 2) {
  464. /* Got an error */
  465. Dummy = readb(apbs[i].RamIO + VERS);
  466. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  467. set_current_state(TASK_RUNNING);
  468. remove_wait_queue(&FlagSleepRec, &wait);
  469. printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
  470. i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  471. DeviceErrorCount++;
  472. return -EIO;
  473. }
  474. /* Nothing for us. Try the next board */
  475. Dummy = readb(apbs[i].RamIO + VERS);
  476. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  477. } /* per board */
  478. /* OK - No boards had data for us. Sleep now */
  479. schedule();
  480. remove_wait_queue(&FlagSleepRec, &wait);
  481. if (signal_pending(current))
  482. return -EINTR;
  483. #ifdef DEBUG
  484. if (loopcount++ > 2) {
  485. printk(KERN_DEBUG "Looping in ac_read. loopcount %d\n", loopcount);
  486. }
  487. #endif
  488. }
  489. }
  490. static irqreturn_t ac_interrupt(int vec, void *dev_instance)
  491. {
  492. unsigned int i;
  493. unsigned int FlagInt;
  494. unsigned int LoopCount;
  495. int handled = 0;
  496. // printk("Applicom interrupt on IRQ %d occurred\n", vec);
  497. LoopCount = 0;
  498. do {
  499. FlagInt = 0;
  500. for (i = 0; i < MAX_BOARD; i++) {
  501. /* Skip if this board doesn't exist */
  502. if (!apbs[i].RamIO)
  503. continue;
  504. spin_lock(&apbs[i].mutex);
  505. /* Skip if this board doesn't want attention */
  506. if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
  507. spin_unlock(&apbs[i].mutex);
  508. continue;
  509. }
  510. handled = 1;
  511. FlagInt = 1;
  512. writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
  513. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
  514. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
  515. i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  516. DeviceErrorCount++;
  517. }
  518. if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
  519. (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
  520. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
  521. i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
  522. DeviceErrorCount++;
  523. }
  524. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) { /* mailbox sent by the card ? */
  525. if (waitqueue_active(&FlagSleepRec)) {
  526. wake_up_interruptible(&FlagSleepRec);
  527. }
  528. }
  529. if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) { /* ram i/o free for write by pc ? */
  530. if (waitqueue_active(&apbs[i].FlagSleepSend)) { /* process sleep during read ? */
  531. wake_up_interruptible(&apbs[i].FlagSleepSend);
  532. }
  533. }
  534. Dummy = readb(apbs[i].RamIO + VERS);
  535. if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
  536. /* There's another int waiting on this card */
  537. spin_unlock(&apbs[i].mutex);
  538. i--;
  539. } else {
  540. spin_unlock(&apbs[i].mutex);
  541. }
  542. }
  543. if (FlagInt)
  544. LoopCount = 0;
  545. else
  546. LoopCount++;
  547. } while(LoopCount < 2);
  548. return IRQ_RETVAL(handled);
  549. }
  550. static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  551. { /* @ ADG ou ATO selon le cas */
  552. int i;
  553. unsigned char IndexCard;
  554. void __iomem *pmem;
  555. int ret = 0;
  556. volatile unsigned char byte_reset_it;
  557. struct st_ram_io *adgl;
  558. void __user *argp = (void __user *)arg;
  559. /* In general, the device is only openable by root anyway, so we're not
  560. particularly concerned that bogus ioctls can flood the console. */
  561. adgl = memdup_user(argp, sizeof(struct st_ram_io));
  562. if (IS_ERR(adgl))
  563. return PTR_ERR(adgl);
  564. mutex_lock(&ac_mutex);
  565. IndexCard = adgl->num_card-1;
  566. if(cmd != 6 && ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) {
  567. static int warncount = 10;
  568. if (warncount) {
  569. printk( KERN_WARNING "APPLICOM driver IOCTL, bad board number %d\n",(int)IndexCard+1);
  570. warncount--;
  571. }
  572. kfree(adgl);
  573. mutex_unlock(&ac_mutex);
  574. return -EINVAL;
  575. }
  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. }