applicom.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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: applicom.c,v 1.1.1.1 2007/06/12 07:27:10 eyryu 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. /* adresses 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/slab.h>
  25. #include <linux/errno.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/pci.h>
  28. #include <linux/wait.h>
  29. #include <linux/init.h>
  30. #include <linux/fs.h>
  31. #include <asm/io.h>
  32. #include <asm/uaccess.h>
  33. #include "applicom.h"
  34. /* NOTE: We use for loops with {write,read}b() instead of
  35. memcpy_{from,to}io throughout this driver. This is because
  36. the board doesn't correctly handle word accesses - only
  37. bytes.
  38. */
  39. #undef DEBUG
  40. #define MAX_BOARD 8 /* maximum of pc board possible */
  41. #define MAX_ISA_BOARD 4
  42. #define LEN_RAM_IO 0x800
  43. #define AC_MINOR 157
  44. #ifndef PCI_VENDOR_ID_APPLICOM
  45. #define PCI_VENDOR_ID_APPLICOM 0x1389
  46. #define PCI_DEVICE_ID_APPLICOM_PCIGENERIC 0x0001
  47. #define PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN 0x0002
  48. #define PCI_DEVICE_ID_APPLICOM_PCI2000PFB 0x0003
  49. #endif
  50. #define MAX_PCI_DEVICE_NUM 3
  51. static char *applicom_pci_devnames[] = {
  52. "PCI board",
  53. "PCI2000IBS / PCI2000CAN",
  54. "PCI2000PFB"
  55. };
  56. static struct pci_device_id applicom_pci_tbl[] = {
  57. { PCI_VENDOR_ID_APPLICOM, PCI_DEVICE_ID_APPLICOM_PCIGENERIC,
  58. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  59. { PCI_VENDOR_ID_APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN,
  60. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  61. { PCI_VENDOR_ID_APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000PFB,
  62. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  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_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 int ac_ioctl(struct inode *, struct file *, unsigned int,
  92. 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. .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: applicom.c,v 1.1.1.1 2007/06/12 07:27:10 eyryu 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 (dev->vendor != PCI_VENDOR_ID_APPLICOM)
  159. continue;
  160. if (dev->device > MAX_PCI_DEVICE_NUM || dev->device == 0)
  161. continue;
  162. if (pci_enable_device(dev))
  163. return -EIO;
  164. RamIO = ioremap(dev->resource[0].start, LEN_RAM_IO);
  165. if (!RamIO) {
  166. printk(KERN_INFO "ac.o: Failed to ioremap PCI memory "
  167. "space at 0x%llx\n",
  168. (unsigned long long)dev->resource[0].start);
  169. pci_disable_device(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)dev->resource[0].start,
  175. dev->irq);
  176. boardno = ac_register_board(dev->resource[0].start, 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. pci_disable_device(dev);
  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((NumCard < 1) || (NumCard > MAX_BOARD) || !apbs[IndexCard].RamIO)
  312. return -EINVAL;
  313. #ifdef DEBUG
  314. printk("Write to applicom card #%d. struct st_ram_io follows:",
  315. IndexCard+1);
  316. for (c = 0; c < sizeof(struct st_ram_io);) {
  317. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
  318. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  319. printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
  320. }
  321. }
  322. printk("\nstruct mailbox follows:");
  323. for (c = 0; c < sizeof(struct mailbox);) {
  324. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
  325. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  326. printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
  327. }
  328. }
  329. printk("\n");
  330. #endif
  331. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  332. /* Test octet ready correct */
  333. if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
  334. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  335. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  336. printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
  337. IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
  338. DeviceErrorCount++;
  339. return -EIO;
  340. }
  341. /* Place ourselves on the wait queue */
  342. set_current_state(TASK_INTERRUPTIBLE);
  343. add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  344. /* Check whether the card is ready for us */
  345. while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
  346. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  347. /* It's busy. Sleep. */
  348. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  349. schedule();
  350. if (signal_pending(current)) {
  351. remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
  352. &wait);
  353. return -EINTR;
  354. }
  355. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  356. set_current_state(TASK_INTERRUPTIBLE);
  357. }
  358. /* We may not have actually slept */
  359. set_current_state(TASK_RUNNING);
  360. remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  361. writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  362. /* Which is best - lock down the pages with rawio and then
  363. copy directly, or use bounce buffers? For now we do the latter
  364. because it works with 2.2 still */
  365. {
  366. unsigned char *from = (unsigned char *) &tmpmailbox;
  367. void __iomem *to = apbs[IndexCard].RamIO + RAM_FROM_PC;
  368. int c;
  369. for (c = 0; c < sizeof(struct mailbox); c++)
  370. writeb(*(from++), to++);
  371. }
  372. writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
  373. writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
  374. writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
  375. writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  376. writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  377. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  378. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  379. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  380. return 0;
  381. }
  382. static int do_ac_read(int IndexCard, char __user *buf,
  383. struct st_ram_io *st_loc, struct mailbox *mailbox)
  384. {
  385. void __iomem *from = apbs[IndexCard].RamIO + RAM_TO_PC;
  386. unsigned char *to = (unsigned char *)&mailbox;
  387. #ifdef DEBUG
  388. int c;
  389. #endif
  390. st_loc->tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
  391. st_loc->numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  392. {
  393. int c;
  394. for (c = 0; c < sizeof(struct mailbox); c++)
  395. *(to++) = readb(from++);
  396. }
  397. writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  398. writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
  399. writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  400. writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
  401. apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
  402. writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  403. writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
  404. writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  405. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  406. #ifdef DEBUG
  407. printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
  408. for (c = 0; c < sizeof(struct st_ram_io);) {
  409. printk("\n%5.5X: %2.2X", c, ((unsigned char *)st_loc)[c]);
  410. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  411. printk(" %2.2X", ((unsigned char *)st_loc)[c]);
  412. }
  413. }
  414. printk("\nstruct mailbox follows:");
  415. for (c = 0; c < sizeof(struct mailbox);) {
  416. printk("\n%5.5X: %2.2X", c, ((unsigned char *)mailbox)[c]);
  417. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  418. printk(" %2.2X", ((unsigned char *)mailbox)[c]);
  419. }
  420. }
  421. printk("\n");
  422. #endif
  423. return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
  424. }
  425. static ssize_t ac_read (struct file *filp, char __user *buf, size_t count, loff_t *ptr)
  426. {
  427. unsigned long flags;
  428. unsigned int i;
  429. unsigned char tmp;
  430. int ret = 0;
  431. DECLARE_WAITQUEUE(wait, current);
  432. #ifdef DEBUG
  433. int loopcount=0;
  434. #endif
  435. /* No need to ratelimit this. Only root can trigger it anyway */
  436. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  437. printk( KERN_WARNING "Hmmm. read() of Applicom card, length %zd != expected %zd\n",
  438. count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
  439. return -EINVAL;
  440. }
  441. while(1) {
  442. /* Stick ourself on the wait queue */
  443. set_current_state(TASK_INTERRUPTIBLE);
  444. add_wait_queue(&FlagSleepRec, &wait);
  445. /* Scan each board, looking for one which has a packet for us */
  446. for (i=0; i < MAX_BOARD; i++) {
  447. if (!apbs[i].RamIO)
  448. continue;
  449. spin_lock_irqsave(&apbs[i].mutex, flags);
  450. tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
  451. if (tmp == 2) {
  452. struct st_ram_io st_loc;
  453. struct mailbox mailbox;
  454. /* Got a packet for us */
  455. ret = do_ac_read(i, buf, &st_loc, &mailbox);
  456. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  457. set_current_state(TASK_RUNNING);
  458. remove_wait_queue(&FlagSleepRec, &wait);
  459. if (copy_to_user(buf, &st_loc, sizeof(st_loc)))
  460. return -EFAULT;
  461. if (copy_to_user(buf + sizeof(st_loc), &mailbox, sizeof(mailbox)))
  462. return -EFAULT;
  463. return tmp;
  464. }
  465. if (tmp > 2) {
  466. /* Got an error */
  467. Dummy = readb(apbs[i].RamIO + VERS);
  468. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  469. set_current_state(TASK_RUNNING);
  470. remove_wait_queue(&FlagSleepRec, &wait);
  471. printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
  472. i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  473. DeviceErrorCount++;
  474. return -EIO;
  475. }
  476. /* Nothing for us. Try the next board */
  477. Dummy = readb(apbs[i].RamIO + VERS);
  478. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  479. } /* per board */
  480. /* OK - No boards had data for us. Sleep now */
  481. schedule();
  482. remove_wait_queue(&FlagSleepRec, &wait);
  483. if (signal_pending(current))
  484. return -EINTR;
  485. #ifdef DEBUG
  486. if (loopcount++ > 2) {
  487. printk(KERN_DEBUG "Looping in ac_read. loopcount %d\n", loopcount);
  488. }
  489. #endif
  490. }
  491. }
  492. static irqreturn_t ac_interrupt(int vec, void *dev_instance)
  493. {
  494. unsigned int i;
  495. unsigned int FlagInt;
  496. unsigned int LoopCount;
  497. int handled = 0;
  498. // printk("Applicom interrupt on IRQ %d occurred\n", vec);
  499. LoopCount = 0;
  500. do {
  501. FlagInt = 0;
  502. for (i = 0; i < MAX_BOARD; i++) {
  503. /* Skip if this board doesn't exist */
  504. if (!apbs[i].RamIO)
  505. continue;
  506. spin_lock(&apbs[i].mutex);
  507. /* Skip if this board doesn't want attention */
  508. if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
  509. spin_unlock(&apbs[i].mutex);
  510. continue;
  511. }
  512. handled = 1;
  513. FlagInt = 1;
  514. writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
  515. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
  516. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
  517. i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  518. DeviceErrorCount++;
  519. }
  520. if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
  521. (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
  522. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
  523. i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
  524. DeviceErrorCount++;
  525. }
  526. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) { /* mailbox sent by the card ? */
  527. if (waitqueue_active(&FlagSleepRec)) {
  528. wake_up_interruptible(&FlagSleepRec);
  529. }
  530. }
  531. if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) { /* ram i/o free for write by pc ? */
  532. if (waitqueue_active(&apbs[i].FlagSleepSend)) { /* process sleep during read ? */
  533. wake_up_interruptible(&apbs[i].FlagSleepSend);
  534. }
  535. }
  536. Dummy = readb(apbs[i].RamIO + VERS);
  537. if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
  538. /* There's another int waiting on this card */
  539. spin_unlock(&apbs[i].mutex);
  540. i--;
  541. } else {
  542. spin_unlock(&apbs[i].mutex);
  543. }
  544. }
  545. if (FlagInt)
  546. LoopCount = 0;
  547. else
  548. LoopCount++;
  549. } while(LoopCount < 2);
  550. return IRQ_RETVAL(handled);
  551. }
  552. static int ac_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  553. { /* @ ADG ou ATO selon le cas */
  554. int i;
  555. unsigned char IndexCard;
  556. void __iomem *pmem;
  557. int ret = 0;
  558. volatile unsigned char byte_reset_it;
  559. struct st_ram_io *adgl;
  560. void __user *argp = (void __user *)arg;
  561. /* In general, the device is only openable by root anyway, so we're not
  562. particularly concerned that bogus ioctls can flood the console. */
  563. adgl = kmalloc(sizeof(struct st_ram_io), GFP_KERNEL);
  564. if (!adgl)
  565. return -ENOMEM;
  566. if (copy_from_user(adgl, argp, sizeof(struct st_ram_io))) {
  567. kfree(adgl);
  568. return -EFAULT;
  569. }
  570. IndexCard = adgl->num_card-1;
  571. if(cmd != 0 && cmd != 6 &&
  572. ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) {
  573. static int warncount = 10;
  574. if (warncount) {
  575. printk( KERN_WARNING "APPLICOM driver IOCTL, bad board number %d\n",(int)IndexCard+1);
  576. warncount--;
  577. }
  578. kfree(adgl);
  579. return -EINVAL;
  580. }
  581. switch (cmd) {
  582. case 0:
  583. pmem = apbs[IndexCard].RamIO;
  584. for (i = 0; i < sizeof(struct st_ram_io); i++)
  585. ((unsigned char *)adgl)[i]=readb(pmem++);
  586. if (copy_to_user(argp, adgl, sizeof(struct st_ram_io)))
  587. ret = -EFAULT;
  588. break;
  589. case 1:
  590. pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
  591. for (i = 0; i < 4; i++)
  592. adgl->conf_end_test[i] = readb(pmem++);
  593. for (i = 0; i < 2; i++)
  594. adgl->error_code[i] = readb(pmem++);
  595. for (i = 0; i < 4; i++)
  596. adgl->parameter_error[i] = readb(pmem++);
  597. pmem = apbs[IndexCard].RamIO + VERS;
  598. adgl->vers = readb(pmem);
  599. pmem = apbs[IndexCard].RamIO + TYPE_CARD;
  600. for (i = 0; i < 20; i++)
  601. adgl->reserv1[i] = readb(pmem++);
  602. *(int *)&adgl->reserv1[20] =
  603. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER) << 16) +
  604. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 1) << 8) +
  605. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 2) );
  606. if (copy_to_user(argp, adgl, sizeof(struct st_ram_io)))
  607. ret = -EFAULT;
  608. break;
  609. case 2:
  610. pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
  611. for (i = 0; i < 10; i++)
  612. writeb(0xff, pmem++);
  613. writeb(adgl->data_from_pc_ready,
  614. apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  615. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  616. for (i = 0; i < MAX_BOARD; i++) {
  617. if (apbs[i].RamIO) {
  618. byte_reset_it = readb(apbs[i].RamIO + RAM_IT_TO_PC);
  619. }
  620. }
  621. break;
  622. case 3:
  623. pmem = apbs[IndexCard].RamIO + TIC_DES_FROM_PC;
  624. writeb(adgl->tic_des_from_pc, pmem);
  625. break;
  626. case 4:
  627. pmem = apbs[IndexCard].RamIO + TIC_OWNER_TO_PC;
  628. adgl->tic_owner_to_pc = readb(pmem++);
  629. adgl->numcard_owner_to_pc = readb(pmem);
  630. if (copy_to_user(argp, adgl,sizeof(struct st_ram_io)))
  631. ret = -EFAULT;
  632. break;
  633. case 5:
  634. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  635. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  636. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  637. writeb(4, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  638. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  639. break;
  640. case 6:
  641. printk(KERN_INFO "APPLICOM driver release .... V2.8.0 ($Revision: 1.1.1.1 $)\n");
  642. printk(KERN_INFO "Number of installed boards . %d\n", (int) numboards);
  643. printk(KERN_INFO "Segment of board ........... %X\n", (int) mem);
  644. printk(KERN_INFO "Interrupt IRQ number ....... %d\n", (int) irq);
  645. for (i = 0; i < MAX_BOARD; i++) {
  646. int serial;
  647. char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
  648. if (!apbs[i].RamIO)
  649. continue;
  650. for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
  651. boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
  652. boardname[serial] = 0;
  653. printk(KERN_INFO "Prom version board %d ....... V%d.%d %s",
  654. i+1,
  655. (int)(readb(apbs[IndexCard].RamIO + VERS) >> 4),
  656. (int)(readb(apbs[IndexCard].RamIO + VERS) & 0xF),
  657. boardname);
  658. serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
  659. (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
  660. (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
  661. if (serial != 0)
  662. printk(" S/N %d\n", serial);
  663. else
  664. printk("\n");
  665. }
  666. if (DeviceErrorCount != 0)
  667. printk(KERN_INFO "DeviceErrorCount ........... %d\n", DeviceErrorCount);
  668. if (ReadErrorCount != 0)
  669. printk(KERN_INFO "ReadErrorCount ............. %d\n", ReadErrorCount);
  670. if (WriteErrorCount != 0)
  671. printk(KERN_INFO "WriteErrorCount ............ %d\n", WriteErrorCount);
  672. if (waitqueue_active(&FlagSleepRec))
  673. printk(KERN_INFO "Process in read pending\n");
  674. for (i = 0; i < MAX_BOARD; i++) {
  675. if (apbs[i].RamIO && waitqueue_active(&apbs[i].FlagSleepSend))
  676. printk(KERN_INFO "Process in write pending board %d\n",i+1);
  677. }
  678. break;
  679. default:
  680. printk(KERN_INFO "APPLICOM driver ioctl, unknown function code %d\n",cmd) ;
  681. ret = -EINVAL;
  682. break;
  683. }
  684. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  685. kfree(adgl);
  686. return 0;
  687. }