applicom.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. /* 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 *, struct pt_regs *);
  94. static 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. #ifdef MODULE
  138. #define applicom_init init_module
  139. void cleanup_module(void)
  140. {
  141. unsigned int i;
  142. misc_deregister(&ac_miscdev);
  143. for (i = 0; i < MAX_BOARD; i++) {
  144. if (!apbs[i].RamIO)
  145. continue;
  146. if (apbs[i].irq)
  147. free_irq(apbs[i].irq, &dummy);
  148. iounmap(apbs[i].RamIO);
  149. }
  150. }
  151. #endif /* MODULE */
  152. int __init applicom_init(void)
  153. {
  154. int i, numisa = 0;
  155. struct pci_dev *dev = NULL;
  156. void __iomem *RamIO;
  157. int boardno, ret;
  158. printk(KERN_INFO "Applicom driver: $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $\n");
  159. /* No mem and irq given - check for a PCI card */
  160. while ( (dev = pci_get_class(PCI_CLASS_OTHERS << 16, dev))) {
  161. if (dev->vendor != PCI_VENDOR_ID_APPLICOM)
  162. continue;
  163. if (dev->device > MAX_PCI_DEVICE_NUM || dev->device == 0)
  164. continue;
  165. if (pci_enable_device(dev))
  166. return -EIO;
  167. RamIO = ioremap(dev->resource[0].start, LEN_RAM_IO);
  168. if (!RamIO) {
  169. printk(KERN_INFO "ac.o: Failed to ioremap PCI memory space at 0x%lx\n", dev->resource[0].start);
  170. pci_disable_device(dev);
  171. return -EIO;
  172. }
  173. printk(KERN_INFO "Applicom %s found at mem 0x%lx, irq %d\n",
  174. applicom_pci_devnames[dev->device-1], 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, SA_SHIRQ, "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, SA_SHIRQ, "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. #ifndef MODULE
  281. __initcall(applicom_init);
  282. #endif
  283. static ssize_t ac_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  284. {
  285. unsigned int NumCard; /* Board number 1 -> 8 */
  286. unsigned int IndexCard; /* Index board number 0 -> 7 */
  287. unsigned char TicCard; /* Board TIC to send */
  288. unsigned long flags; /* Current priority */
  289. struct st_ram_io st_loc;
  290. struct mailbox tmpmailbox;
  291. #ifdef DEBUG
  292. int c;
  293. #endif
  294. DECLARE_WAITQUEUE(wait, current);
  295. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  296. static int warncount = 5;
  297. if (warncount) {
  298. printk(KERN_INFO "Hmmm. write() of Applicom card, length %zd != expected %zd\n",
  299. count, sizeof(struct st_ram_io) + sizeof(struct mailbox));
  300. warncount--;
  301. }
  302. return -EINVAL;
  303. }
  304. if(copy_from_user(&st_loc, buf, sizeof(struct st_ram_io)))
  305. return -EFAULT;
  306. if(copy_from_user(&tmpmailbox, &buf[sizeof(struct st_ram_io)],
  307. sizeof(struct mailbox)))
  308. return -EFAULT;
  309. NumCard = st_loc.num_card; /* board number to send */
  310. TicCard = st_loc.tic_des_from_pc; /* tic number to send */
  311. IndexCard = NumCard - 1;
  312. if((NumCard < 1) || (NumCard > MAX_BOARD) || !apbs[IndexCard].RamIO)
  313. return -EINVAL;
  314. #ifdef DEBUG
  315. printk("Write to applicom card #%d. struct st_ram_io follows:",
  316. IndexCard+1);
  317. for (c = 0; c < sizeof(struct st_ram_io);) {
  318. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
  319. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  320. printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
  321. }
  322. }
  323. printk("\nstruct mailbox follows:");
  324. for (c = 0; c < sizeof(struct mailbox);) {
  325. printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
  326. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  327. printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
  328. }
  329. }
  330. printk("\n");
  331. #endif
  332. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  333. /* Test octet ready correct */
  334. if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
  335. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  336. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  337. printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
  338. IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
  339. DeviceErrorCount++;
  340. return -EIO;
  341. }
  342. /* Place ourselves on the wait queue */
  343. set_current_state(TASK_INTERRUPTIBLE);
  344. add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  345. /* Check whether the card is ready for us */
  346. while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
  347. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  348. /* It's busy. Sleep. */
  349. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  350. schedule();
  351. if (signal_pending(current)) {
  352. remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
  353. &wait);
  354. return -EINTR;
  355. }
  356. spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
  357. set_current_state(TASK_INTERRUPTIBLE);
  358. }
  359. /* We may not have actually slept */
  360. set_current_state(TASK_RUNNING);
  361. remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
  362. writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  363. /* Which is best - lock down the pages with rawio and then
  364. copy directly, or use bounce buffers? For now we do the latter
  365. because it works with 2.2 still */
  366. {
  367. unsigned char *from = (unsigned char *) &tmpmailbox;
  368. void __iomem *to = apbs[IndexCard].RamIO + RAM_FROM_PC;
  369. int c;
  370. for (c = 0; c < sizeof(struct mailbox); c++)
  371. writeb(*(from++), to++);
  372. }
  373. writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
  374. writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
  375. writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
  376. writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  377. writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  378. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  379. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  380. spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
  381. return 0;
  382. }
  383. static int do_ac_read(int IndexCard, char __user *buf,
  384. struct st_ram_io *st_loc, struct mailbox *mailbox)
  385. {
  386. void __iomem *from = apbs[IndexCard].RamIO + RAM_TO_PC;
  387. unsigned char *to = (unsigned char *)&mailbox;
  388. #ifdef DEBUG
  389. int c;
  390. #endif
  391. st_loc->tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
  392. st_loc->numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  393. {
  394. int c;
  395. for (c = 0; c < sizeof(struct mailbox); c++)
  396. *(to++) = readb(from++);
  397. }
  398. writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  399. writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
  400. writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  401. writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
  402. apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
  403. writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
  404. writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
  405. writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  406. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  407. #ifdef DEBUG
  408. printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
  409. for (c = 0; c < sizeof(struct st_ram_io);) {
  410. printk("\n%5.5X: %2.2X", c, ((unsigned char *)st_loc)[c]);
  411. for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
  412. printk(" %2.2X", ((unsigned char *)st_loc)[c]);
  413. }
  414. }
  415. printk("\nstruct mailbox follows:");
  416. for (c = 0; c < sizeof(struct mailbox);) {
  417. printk("\n%5.5X: %2.2X", c, ((unsigned char *)mailbox)[c]);
  418. for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
  419. printk(" %2.2X", ((unsigned char *)mailbox)[c]);
  420. }
  421. }
  422. printk("\n");
  423. #endif
  424. return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
  425. }
  426. static ssize_t ac_read (struct file *filp, char __user *buf, size_t count, loff_t *ptr)
  427. {
  428. unsigned long flags;
  429. unsigned int i;
  430. unsigned char tmp;
  431. int ret = 0;
  432. DECLARE_WAITQUEUE(wait, current);
  433. #ifdef DEBUG
  434. int loopcount=0;
  435. #endif
  436. /* No need to ratelimit this. Only root can trigger it anyway */
  437. if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
  438. printk( KERN_WARNING "Hmmm. read() of Applicom card, length %zd != expected %zd\n",
  439. count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
  440. return -EINVAL;
  441. }
  442. while(1) {
  443. /* Stick ourself on the wait queue */
  444. set_current_state(TASK_INTERRUPTIBLE);
  445. add_wait_queue(&FlagSleepRec, &wait);
  446. /* Scan each board, looking for one which has a packet for us */
  447. for (i=0; i < MAX_BOARD; i++) {
  448. if (!apbs[i].RamIO)
  449. continue;
  450. spin_lock_irqsave(&apbs[i].mutex, flags);
  451. tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
  452. if (tmp == 2) {
  453. struct st_ram_io st_loc;
  454. struct mailbox mailbox;
  455. /* Got a packet for us */
  456. ret = do_ac_read(i, buf, &st_loc, &mailbox);
  457. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  458. set_current_state(TASK_RUNNING);
  459. remove_wait_queue(&FlagSleepRec, &wait);
  460. if (copy_to_user(buf, &st_loc, sizeof(st_loc)))
  461. return -EFAULT;
  462. if (copy_to_user(buf + sizeof(st_loc), &mailbox, sizeof(mailbox)))
  463. return -EFAULT;
  464. return tmp;
  465. }
  466. if (tmp > 2) {
  467. /* Got an error */
  468. Dummy = readb(apbs[i].RamIO + VERS);
  469. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  470. set_current_state(TASK_RUNNING);
  471. remove_wait_queue(&FlagSleepRec, &wait);
  472. printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
  473. i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  474. DeviceErrorCount++;
  475. return -EIO;
  476. }
  477. /* Nothing for us. Try the next board */
  478. Dummy = readb(apbs[i].RamIO + VERS);
  479. spin_unlock_irqrestore(&apbs[i].mutex, flags);
  480. } /* per board */
  481. /* OK - No boards had data for us. Sleep now */
  482. schedule();
  483. remove_wait_queue(&FlagSleepRec, &wait);
  484. if (signal_pending(current))
  485. return -EINTR;
  486. #ifdef DEBUG
  487. if (loopcount++ > 2) {
  488. printk(KERN_DEBUG "Looping in ac_read. loopcount %d\n", loopcount);
  489. }
  490. #endif
  491. }
  492. }
  493. static irqreturn_t ac_interrupt(int vec, void *dev_instance, struct pt_regs *regs)
  494. {
  495. unsigned int i;
  496. unsigned int FlagInt;
  497. unsigned int LoopCount;
  498. int handled = 0;
  499. // printk("Applicom interrupt on IRQ %d occurred\n", vec);
  500. LoopCount = 0;
  501. do {
  502. FlagInt = 0;
  503. for (i = 0; i < MAX_BOARD; i++) {
  504. /* Skip if this board doesn't exist */
  505. if (!apbs[i].RamIO)
  506. continue;
  507. spin_lock(&apbs[i].mutex);
  508. /* Skip if this board doesn't want attention */
  509. if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
  510. spin_unlock(&apbs[i].mutex);
  511. continue;
  512. }
  513. handled = 1;
  514. FlagInt = 1;
  515. writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
  516. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
  517. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
  518. i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
  519. DeviceErrorCount++;
  520. }
  521. if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
  522. (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
  523. printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
  524. i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
  525. DeviceErrorCount++;
  526. }
  527. if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) { /* mailbox sent by the card ? */
  528. if (waitqueue_active(&FlagSleepRec)) {
  529. wake_up_interruptible(&FlagSleepRec);
  530. }
  531. }
  532. if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) { /* ram i/o free for write by pc ? */
  533. if (waitqueue_active(&apbs[i].FlagSleepSend)) { /* process sleep during read ? */
  534. wake_up_interruptible(&apbs[i].FlagSleepSend);
  535. }
  536. }
  537. Dummy = readb(apbs[i].RamIO + VERS);
  538. if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
  539. /* There's another int waiting on this card */
  540. spin_unlock(&apbs[i].mutex);
  541. i--;
  542. } else {
  543. spin_unlock(&apbs[i].mutex);
  544. }
  545. }
  546. if (FlagInt)
  547. LoopCount = 0;
  548. else
  549. LoopCount++;
  550. } while(LoopCount < 2);
  551. return IRQ_RETVAL(handled);
  552. }
  553. static int ac_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  554. { /* @ ADG ou ATO selon le cas */
  555. int i;
  556. unsigned char IndexCard;
  557. void __iomem *pmem;
  558. int ret = 0;
  559. volatile unsigned char byte_reset_it;
  560. struct st_ram_io *adgl;
  561. void __user *argp = (void __user *)arg;
  562. /* In general, the device is only openable by root anyway, so we're not
  563. particularly concerned that bogus ioctls can flood the console. */
  564. adgl = kmalloc(sizeof(struct st_ram_io), GFP_KERNEL);
  565. if (!adgl)
  566. return -ENOMEM;
  567. if (copy_from_user(adgl, argp, sizeof(struct st_ram_io))) {
  568. kfree(adgl);
  569. return -EFAULT;
  570. }
  571. IndexCard = adgl->num_card-1;
  572. if(cmd != 0 && cmd != 6 &&
  573. ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) {
  574. static int warncount = 10;
  575. if (warncount) {
  576. printk( KERN_WARNING "APPLICOM driver IOCTL, bad board number %d\n",(int)IndexCard+1);
  577. warncount--;
  578. }
  579. kfree(adgl);
  580. return -EINVAL;
  581. }
  582. switch (cmd) {
  583. case 0:
  584. pmem = apbs[IndexCard].RamIO;
  585. for (i = 0; i < sizeof(struct st_ram_io); i++)
  586. ((unsigned char *)adgl)[i]=readb(pmem++);
  587. if (copy_to_user(argp, adgl, sizeof(struct st_ram_io)))
  588. ret = -EFAULT;
  589. break;
  590. case 1:
  591. pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
  592. for (i = 0; i < 4; i++)
  593. adgl->conf_end_test[i] = readb(pmem++);
  594. for (i = 0; i < 2; i++)
  595. adgl->error_code[i] = readb(pmem++);
  596. for (i = 0; i < 4; i++)
  597. adgl->parameter_error[i] = readb(pmem++);
  598. pmem = apbs[IndexCard].RamIO + VERS;
  599. adgl->vers = readb(pmem);
  600. pmem = apbs[IndexCard].RamIO + TYPE_CARD;
  601. for (i = 0; i < 20; i++)
  602. adgl->reserv1[i] = readb(pmem++);
  603. *(int *)&adgl->reserv1[20] =
  604. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER) << 16) +
  605. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 1) << 8) +
  606. (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 2) );
  607. if (copy_to_user(argp, adgl, sizeof(struct st_ram_io)))
  608. ret = -EFAULT;
  609. break;
  610. case 2:
  611. pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
  612. for (i = 0; i < 10; i++)
  613. writeb(0xff, pmem++);
  614. writeb(adgl->data_from_pc_ready,
  615. apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  616. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  617. for (i = 0; i < MAX_BOARD; i++) {
  618. if (apbs[i].RamIO) {
  619. byte_reset_it = readb(apbs[i].RamIO + RAM_IT_TO_PC);
  620. }
  621. }
  622. break;
  623. case 3:
  624. pmem = apbs[IndexCard].RamIO + TIC_DES_FROM_PC;
  625. writeb(adgl->tic_des_from_pc, pmem);
  626. break;
  627. case 4:
  628. pmem = apbs[IndexCard].RamIO + TIC_OWNER_TO_PC;
  629. adgl->tic_owner_to_pc = readb(pmem++);
  630. adgl->numcard_owner_to_pc = readb(pmem);
  631. if (copy_to_user(argp, adgl,sizeof(struct st_ram_io)))
  632. ret = -EFAULT;
  633. break;
  634. case 5:
  635. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
  636. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
  637. writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
  638. writeb(4, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
  639. writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
  640. break;
  641. case 6:
  642. printk(KERN_INFO "APPLICOM driver release .... V2.8.0 ($Revision: 1.30 $)\n");
  643. printk(KERN_INFO "Number of installed boards . %d\n", (int) numboards);
  644. printk(KERN_INFO "Segment of board ........... %X\n", (int) mem);
  645. printk(KERN_INFO "Interrupt IRQ number ....... %d\n", (int) irq);
  646. for (i = 0; i < MAX_BOARD; i++) {
  647. int serial;
  648. char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
  649. if (!apbs[i].RamIO)
  650. continue;
  651. for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
  652. boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
  653. boardname[serial] = 0;
  654. printk(KERN_INFO "Prom version board %d ....... V%d.%d %s",
  655. i+1,
  656. (int)(readb(apbs[IndexCard].RamIO + VERS) >> 4),
  657. (int)(readb(apbs[IndexCard].RamIO + VERS) & 0xF),
  658. boardname);
  659. serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
  660. (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
  661. (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
  662. if (serial != 0)
  663. printk(" S/N %d\n", serial);
  664. else
  665. printk("\n");
  666. }
  667. if (DeviceErrorCount != 0)
  668. printk(KERN_INFO "DeviceErrorCount ........... %d\n", DeviceErrorCount);
  669. if (ReadErrorCount != 0)
  670. printk(KERN_INFO "ReadErrorCount ............. %d\n", ReadErrorCount);
  671. if (WriteErrorCount != 0)
  672. printk(KERN_INFO "WriteErrorCount ............ %d\n", WriteErrorCount);
  673. if (waitqueue_active(&FlagSleepRec))
  674. printk(KERN_INFO "Process in read pending\n");
  675. for (i = 0; i < MAX_BOARD; i++) {
  676. if (apbs[i].RamIO && waitqueue_active(&apbs[i].FlagSleepSend))
  677. printk(KERN_INFO "Process in write pending board %d\n",i+1);
  678. }
  679. break;
  680. default:
  681. printk(KERN_INFO "APPLICOM driver ioctl, unknown function code %d\n",cmd) ;
  682. ret = -EINVAL;
  683. break;
  684. }
  685. Dummy = readb(apbs[IndexCard].RamIO + VERS);
  686. kfree(adgl);
  687. return 0;
  688. }
  689. #ifndef MODULE
  690. static int __init applicom_setup(char *str)
  691. {
  692. int ints[4];
  693. (void) get_options(str, 4, ints);
  694. if (ints[0] > 2) {
  695. printk(KERN_WARNING "Too many arguments to 'applicom=', expected mem,irq only.\n");
  696. }
  697. if (ints[0] < 2) {
  698. printk(KERN_INFO"applicom numargs: %d\n", ints[0]);
  699. return 0;
  700. }
  701. mem = ints[1];
  702. irq = ints[2];
  703. return 1;
  704. }
  705. __setup("applicom=", applicom_setup);
  706. #endif /* MODULE */