cosa.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  1. /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
  2. /*
  3. * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /*
  20. * The driver for the SRP and COSA synchronous serial cards.
  21. *
  22. * HARDWARE INFO
  23. *
  24. * Both cards are developed at the Institute of Computer Science,
  25. * Masaryk University (http://www.ics.muni.cz/). The hardware is
  26. * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
  27. * and the photo of both cards is available at
  28. * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
  29. * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
  30. * For Linux-specific utilities, see below in the "Software info" section.
  31. * If you want to order the card, contact Jiri Novotny.
  32. *
  33. * The SRP (serial port?, the Czech word "srp" means "sickle") card
  34. * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
  35. * with V.24 interfaces up to 80kb/s each.
  36. *
  37. * The COSA (communication serial adapter?, the Czech word "kosa" means
  38. * "scythe") is a next-generation sync/async board with two interfaces
  39. * - currently any of V.24, X.21, V.35 and V.36 can be selected.
  40. * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
  41. * The 8-channels version is in development.
  42. *
  43. * Both types have downloadable firmware and communicate via ISA DMA.
  44. * COSA can be also a bus-mastering device.
  45. *
  46. * SOFTWARE INFO
  47. *
  48. * The homepage of the Linux driver is at http://www.fi.muni.cz/~kas/cosa/.
  49. * The CVS tree of Linux driver can be viewed there, as well as the
  50. * firmware binaries and user-space utilities for downloading the firmware
  51. * into the card and setting up the card.
  52. *
  53. * The Linux driver (unlike the present *BSD drivers :-) can work even
  54. * for the COSA and SRP in one computer and allows each channel to work
  55. * in one of the three modes (character device, Cisco HDLC, Sync PPP).
  56. *
  57. * AUTHOR
  58. *
  59. * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
  60. *
  61. * You can mail me bugfixes and even success reports. I am especially
  62. * interested in the SMP and/or muliti-channel success/failure reports
  63. * (I wonder if I did the locking properly :-).
  64. *
  65. * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
  66. *
  67. * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
  68. * The skeleton.c by Donald Becker
  69. * The SDL Riscom/N2 driver by Mike Natale
  70. * The Comtrol Hostess SV11 driver by Alan Cox
  71. * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
  72. */
  73. /*
  74. * 5/25/1999 : Marcelo Tosatti <marcelo@conectiva.com.br>
  75. * fixed a deadlock in cosa_sppp_open
  76. */
  77. /* ---------- Headers, macros, data structures ---------- */
  78. #include <linux/module.h>
  79. #include <linux/kernel.h>
  80. #include <linux/slab.h>
  81. #include <linux/poll.h>
  82. #include <linux/fs.h>
  83. #include <linux/interrupt.h>
  84. #include <linux/delay.h>
  85. #include <linux/errno.h>
  86. #include <linux/ioport.h>
  87. #include <linux/netdevice.h>
  88. #include <linux/spinlock.h>
  89. #include <linux/device.h>
  90. #undef COSA_SLOW_IO /* for testing purposes only */
  91. #include <asm/io.h>
  92. #include <asm/dma.h>
  93. #include <asm/byteorder.h>
  94. #include <net/syncppp.h>
  95. #include "cosa.h"
  96. /* Maximum length of the identification string. */
  97. #define COSA_MAX_ID_STRING 128
  98. /* Maximum length of the channel name */
  99. #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
  100. /* Per-channel data structure */
  101. struct channel_data {
  102. void *if_ptr; /* General purpose pointer (used by SPPP) */
  103. int usage; /* Usage count; >0 for chrdev, -1 for netdev */
  104. int num; /* Number of the channel */
  105. struct cosa_data *cosa; /* Pointer to the per-card structure */
  106. int txsize; /* Size of transmitted data */
  107. char *txbuf; /* Transmit buffer */
  108. char name[COSA_MAX_NAME]; /* channel name */
  109. /* The HW layer interface */
  110. /* routine called from the RX interrupt */
  111. char *(*setup_rx)(struct channel_data *channel, int size);
  112. /* routine called when the RX is done (from the EOT interrupt) */
  113. int (*rx_done)(struct channel_data *channel);
  114. /* routine called when the TX is done (from the EOT interrupt) */
  115. int (*tx_done)(struct channel_data *channel, int size);
  116. /* Character device parts */
  117. struct semaphore rsem, wsem;
  118. char *rxdata;
  119. int rxsize;
  120. wait_queue_head_t txwaitq, rxwaitq;
  121. int tx_status, rx_status;
  122. /* SPPP/HDLC device parts */
  123. struct ppp_device pppdev;
  124. struct sk_buff *rx_skb, *tx_skb;
  125. struct net_device_stats stats;
  126. };
  127. /* cosa->firmware_status bits */
  128. #define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
  129. #define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
  130. #define COSA_FW_START (1<<2) /* Is the microcode running? */
  131. struct cosa_data {
  132. int num; /* Card number */
  133. char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
  134. unsigned int datareg, statusreg; /* I/O ports */
  135. unsigned short irq, dma; /* IRQ and DMA number */
  136. unsigned short startaddr; /* Firmware start address */
  137. unsigned short busmaster; /* Use busmastering? */
  138. int nchannels; /* # of channels on this card */
  139. int driver_status; /* For communicating with firmware */
  140. int firmware_status; /* Downloaded, reseted, etc. */
  141. unsigned long rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
  142. unsigned long rxtx; /* RX or TX in progress? */
  143. int enabled;
  144. int usage; /* usage count */
  145. int txchan, txsize, rxsize;
  146. struct channel_data *rxchan;
  147. char *bouncebuf;
  148. char *txbuf, *rxbuf;
  149. struct channel_data *chan;
  150. spinlock_t lock; /* For exclusive operations on this structure */
  151. char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
  152. char *type; /* card type */
  153. };
  154. /*
  155. * Define this if you want all the possible ports to be autoprobed.
  156. * It is here but it probably is not a good idea to use this.
  157. */
  158. /* #define COSA_ISA_AUTOPROBE 1 */
  159. /*
  160. * Character device major number. 117 was allocated for us.
  161. * The value of 0 means to allocate a first free one.
  162. */
  163. static int cosa_major = 117;
  164. /*
  165. * Encoding of the minor numbers:
  166. * The lowest CARD_MINOR_BITS bits means the channel on the single card,
  167. * the highest bits means the card number.
  168. */
  169. #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
  170. * for the single card */
  171. /*
  172. * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
  173. * macro doesn't like anything other than the raw number as an argument :-(
  174. */
  175. #define MAX_CARDS 16
  176. /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
  177. #define DRIVER_RX_READY 0x0001
  178. #define DRIVER_TX_READY 0x0002
  179. #define DRIVER_TXMAP_SHIFT 2
  180. #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
  181. /*
  182. * for cosa->rxtx - indicates whether either transmit or receive is
  183. * in progress. These values are mean number of the bit.
  184. */
  185. #define TXBIT 0
  186. #define RXBIT 1
  187. #define IRQBIT 2
  188. #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
  189. #undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
  190. #undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
  191. #undef DEBUG_IO //1 /* Dump the I/O traffic */
  192. #define TX_TIMEOUT (5*HZ)
  193. /* Maybe the following should be allocated dynamically */
  194. static struct cosa_data cosa_cards[MAX_CARDS];
  195. static int nr_cards;
  196. #ifdef COSA_ISA_AUTOPROBE
  197. static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
  198. /* NOTE: DMA is not autoprobed!!! */
  199. static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
  200. #else
  201. static int io[MAX_CARDS+1];
  202. static int dma[MAX_CARDS+1];
  203. #endif
  204. /* IRQ can be safely autoprobed */
  205. static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
  206. /* for class stuff*/
  207. static struct class *cosa_class;
  208. #ifdef MODULE
  209. module_param_array(io, int, NULL, 0);
  210. MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
  211. module_param_array(irq, int, NULL, 0);
  212. MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
  213. module_param_array(dma, int, NULL, 0);
  214. MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
  215. MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
  216. MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
  217. MODULE_LICENSE("GPL");
  218. #endif
  219. /* I use this mainly for testing purposes */
  220. #ifdef COSA_SLOW_IO
  221. #define cosa_outb outb_p
  222. #define cosa_outw outw_p
  223. #define cosa_inb inb_p
  224. #define cosa_inw inw_p
  225. #else
  226. #define cosa_outb outb
  227. #define cosa_outw outw
  228. #define cosa_inb inb
  229. #define cosa_inw inw
  230. #endif
  231. #define is_8bit(cosa) (!(cosa->datareg & 0x08))
  232. #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
  233. #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
  234. #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
  235. #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
  236. #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
  237. #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
  238. /* Initialization stuff */
  239. static int cosa_probe(int ioaddr, int irq, int dma);
  240. /* HW interface */
  241. static void cosa_enable_rx(struct channel_data *chan);
  242. static void cosa_disable_rx(struct channel_data *chan);
  243. static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
  244. static void cosa_kick(struct cosa_data *cosa);
  245. static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
  246. /* SPPP/HDLC stuff */
  247. static void sppp_channel_init(struct channel_data *chan);
  248. static void sppp_channel_delete(struct channel_data *chan);
  249. static int cosa_sppp_open(struct net_device *d);
  250. static int cosa_sppp_close(struct net_device *d);
  251. static void cosa_sppp_timeout(struct net_device *d);
  252. static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *d);
  253. static char *sppp_setup_rx(struct channel_data *channel, int size);
  254. static int sppp_rx_done(struct channel_data *channel);
  255. static int sppp_tx_done(struct channel_data *channel, int size);
  256. static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  257. static struct net_device_stats *cosa_net_stats(struct net_device *dev);
  258. /* Character device */
  259. static void chardev_channel_init(struct channel_data *chan);
  260. static char *chrdev_setup_rx(struct channel_data *channel, int size);
  261. static int chrdev_rx_done(struct channel_data *channel);
  262. static int chrdev_tx_done(struct channel_data *channel, int size);
  263. static ssize_t cosa_read(struct file *file,
  264. char __user *buf, size_t count, loff_t *ppos);
  265. static ssize_t cosa_write(struct file *file,
  266. const char __user *buf, size_t count, loff_t *ppos);
  267. static unsigned int cosa_poll(struct file *file, poll_table *poll);
  268. static int cosa_open(struct inode *inode, struct file *file);
  269. static int cosa_release(struct inode *inode, struct file *file);
  270. static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
  271. unsigned int cmd, unsigned long arg);
  272. #ifdef COSA_FASYNC_WORKING
  273. static int cosa_fasync(struct inode *inode, struct file *file, int on);
  274. #endif
  275. static const struct file_operations cosa_fops = {
  276. .owner = THIS_MODULE,
  277. .llseek = no_llseek,
  278. .read = cosa_read,
  279. .write = cosa_write,
  280. .poll = cosa_poll,
  281. .ioctl = cosa_chardev_ioctl,
  282. .open = cosa_open,
  283. .release = cosa_release,
  284. #ifdef COSA_FASYNC_WORKING
  285. .fasync = cosa_fasync,
  286. #endif
  287. };
  288. /* Ioctls */
  289. static int cosa_start(struct cosa_data *cosa, int address);
  290. static int cosa_reset(struct cosa_data *cosa);
  291. static int cosa_download(struct cosa_data *cosa, void __user *a);
  292. static int cosa_readmem(struct cosa_data *cosa, void __user *a);
  293. /* COSA/SRP ROM monitor */
  294. static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
  295. static int startmicrocode(struct cosa_data *cosa, int address);
  296. static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
  297. static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
  298. /* Auxilliary functions */
  299. static int get_wait_data(struct cosa_data *cosa);
  300. static int put_wait_data(struct cosa_data *cosa, int data);
  301. static int puthexnumber(struct cosa_data *cosa, int number);
  302. static void put_driver_status(struct cosa_data *cosa);
  303. static void put_driver_status_nolock(struct cosa_data *cosa);
  304. /* Interrupt handling */
  305. static irqreturn_t cosa_interrupt(int irq, void *cosa);
  306. /* I/O ops debugging */
  307. #ifdef DEBUG_IO
  308. static void debug_data_in(struct cosa_data *cosa, int data);
  309. static void debug_data_out(struct cosa_data *cosa, int data);
  310. static void debug_data_cmd(struct cosa_data *cosa, int data);
  311. static void debug_status_in(struct cosa_data *cosa, int status);
  312. static void debug_status_out(struct cosa_data *cosa, int status);
  313. #endif
  314. /* ---------- Initialization stuff ---------- */
  315. static int __init cosa_init(void)
  316. {
  317. int i, err = 0;
  318. printk(KERN_INFO "cosa v1.08 (c) 1997-2000 Jan Kasprzak <kas@fi.muni.cz>\n");
  319. #ifdef CONFIG_SMP
  320. printk(KERN_INFO "cosa: SMP found. Please mail any success/failure reports to the author.\n");
  321. #endif
  322. if (cosa_major > 0) {
  323. if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
  324. printk(KERN_WARNING "cosa: unable to get major %d\n",
  325. cosa_major);
  326. err = -EIO;
  327. goto out;
  328. }
  329. } else {
  330. if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
  331. printk(KERN_WARNING "cosa: unable to register chardev\n");
  332. err = -EIO;
  333. goto out;
  334. }
  335. }
  336. for (i=0; i<MAX_CARDS; i++)
  337. cosa_cards[i].num = -1;
  338. for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
  339. cosa_probe(io[i], irq[i], dma[i]);
  340. if (!nr_cards) {
  341. printk(KERN_WARNING "cosa: no devices found.\n");
  342. unregister_chrdev(cosa_major, "cosa");
  343. err = -ENODEV;
  344. goto out;
  345. }
  346. cosa_class = class_create(THIS_MODULE, "cosa");
  347. if (IS_ERR(cosa_class)) {
  348. err = PTR_ERR(cosa_class);
  349. goto out_chrdev;
  350. }
  351. for (i=0; i<nr_cards; i++) {
  352. class_device_create(cosa_class, NULL, MKDEV(cosa_major, i),
  353. NULL, "cosa%d", i);
  354. }
  355. err = 0;
  356. goto out;
  357. out_chrdev:
  358. unregister_chrdev(cosa_major, "cosa");
  359. out:
  360. return err;
  361. }
  362. module_init(cosa_init);
  363. static void __exit cosa_exit(void)
  364. {
  365. struct cosa_data *cosa;
  366. int i;
  367. printk(KERN_INFO "Unloading the cosa module\n");
  368. for (i=0; i<nr_cards; i++)
  369. class_device_destroy(cosa_class, MKDEV(cosa_major, i));
  370. class_destroy(cosa_class);
  371. for (cosa=cosa_cards; nr_cards--; cosa++) {
  372. /* Clean up the per-channel data */
  373. for (i=0; i<cosa->nchannels; i++) {
  374. /* Chardev driver has no alloc'd per-channel data */
  375. sppp_channel_delete(cosa->chan+i);
  376. }
  377. /* Clean up the per-card data */
  378. kfree(cosa->chan);
  379. kfree(cosa->bouncebuf);
  380. free_irq(cosa->irq, cosa);
  381. free_dma(cosa->dma);
  382. release_region(cosa->datareg,is_8bit(cosa)?2:4);
  383. }
  384. unregister_chrdev(cosa_major, "cosa");
  385. }
  386. module_exit(cosa_exit);
  387. /*
  388. * This function should register all the net devices needed for the
  389. * single channel.
  390. */
  391. static __inline__ void channel_init(struct channel_data *chan)
  392. {
  393. sprintf(chan->name, "cosa%dc%d", chan->cosa->num, chan->num);
  394. /* Initialize the chardev data structures */
  395. chardev_channel_init(chan);
  396. /* Register the sppp interface */
  397. sppp_channel_init(chan);
  398. }
  399. static int cosa_probe(int base, int irq, int dma)
  400. {
  401. struct cosa_data *cosa = cosa_cards+nr_cards;
  402. int i, err = 0;
  403. memset(cosa, 0, sizeof(struct cosa_data));
  404. /* Checking validity of parameters: */
  405. /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
  406. if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
  407. printk (KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
  408. return -1;
  409. }
  410. /* I/O address should be between 0x100 and 0x3ff and should be
  411. * multiple of 8. */
  412. if (base < 0x100 || base > 0x3ff || base & 0x7) {
  413. printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
  414. base);
  415. return -1;
  416. }
  417. /* DMA should be 0,1 or 3-7 */
  418. if (dma < 0 || dma == 4 || dma > 7) {
  419. printk (KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
  420. return -1;
  421. }
  422. /* and finally, on 16-bit COSA DMA should be 4-7 and
  423. * I/O base should not be multiple of 0x10 */
  424. if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
  425. printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
  426. " (base=0x%x, dma=%d)\n", base, dma);
  427. return -1;
  428. }
  429. cosa->dma = dma;
  430. cosa->datareg = base;
  431. cosa->statusreg = is_8bit(cosa)?base+1:base+2;
  432. spin_lock_init(&cosa->lock);
  433. if (!request_region(base, is_8bit(cosa)?2:4,"cosa"))
  434. return -1;
  435. if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
  436. printk(KERN_DEBUG "cosa: probe at 0x%x failed.\n", base);
  437. err = -1;
  438. goto err_out;
  439. }
  440. /* Test the validity of identification string */
  441. if (!strncmp(cosa->id_string, "SRP", 3))
  442. cosa->type = "srp";
  443. else if (!strncmp(cosa->id_string, "COSA", 4))
  444. cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
  445. else {
  446. /* Print a warning only if we are not autoprobing */
  447. #ifndef COSA_ISA_AUTOPROBE
  448. printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
  449. base);
  450. #endif
  451. err = -1;
  452. goto err_out;
  453. }
  454. /* Update the name of the region now we know the type of card */
  455. release_region(base, is_8bit(cosa)?2:4);
  456. if (!request_region(base, is_8bit(cosa)?2:4, cosa->type)) {
  457. printk(KERN_DEBUG "cosa: changing name at 0x%x failed.\n", base);
  458. return -1;
  459. }
  460. /* Now do IRQ autoprobe */
  461. if (irq < 0) {
  462. unsigned long irqs;
  463. /* printk(KERN_INFO "IRQ autoprobe\n"); */
  464. irqs = probe_irq_on();
  465. /*
  466. * Enable interrupt on tx buffer empty (it sure is)
  467. * really sure ?
  468. * FIXME: When this code is not used as module, we should
  469. * probably call udelay() instead of the interruptible sleep.
  470. */
  471. set_current_state(TASK_INTERRUPTIBLE);
  472. cosa_putstatus(cosa, SR_TX_INT_ENA);
  473. schedule_timeout(30);
  474. irq = probe_irq_off(irqs);
  475. /* Disable all IRQs from the card */
  476. cosa_putstatus(cosa, 0);
  477. /* Empty the received data register */
  478. cosa_getdata8(cosa);
  479. if (irq < 0) {
  480. printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
  481. irq, cosa->datareg);
  482. err = -1;
  483. goto err_out;
  484. }
  485. if (irq == 0) {
  486. printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
  487. cosa->datareg);
  488. /* return -1; */
  489. }
  490. }
  491. cosa->irq = irq;
  492. cosa->num = nr_cards;
  493. cosa->usage = 0;
  494. cosa->nchannels = 2; /* FIXME: how to determine this? */
  495. if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
  496. err = -1;
  497. goto err_out;
  498. }
  499. if (request_dma(cosa->dma, cosa->type)) {
  500. err = -1;
  501. goto err_out1;
  502. }
  503. cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
  504. if (!cosa->bouncebuf) {
  505. err = -ENOMEM;
  506. goto err_out2;
  507. }
  508. sprintf(cosa->name, "cosa%d", cosa->num);
  509. /* Initialize the per-channel data */
  510. cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
  511. if (!cosa->chan) {
  512. err = -ENOMEM;
  513. goto err_out3;
  514. }
  515. for (i=0; i<cosa->nchannels; i++) {
  516. cosa->chan[i].cosa = cosa;
  517. cosa->chan[i].num = i;
  518. channel_init(cosa->chan+i);
  519. }
  520. printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
  521. cosa->num, cosa->id_string, cosa->type,
  522. cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
  523. return nr_cards++;
  524. err_out3:
  525. kfree(cosa->bouncebuf);
  526. err_out2:
  527. free_dma(cosa->dma);
  528. err_out1:
  529. free_irq(cosa->irq, cosa);
  530. err_out:
  531. release_region(cosa->datareg,is_8bit(cosa)?2:4);
  532. printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
  533. cosa->num);
  534. return err;
  535. }
  536. /*---------- SPPP/HDLC netdevice ---------- */
  537. static void cosa_setup(struct net_device *d)
  538. {
  539. d->open = cosa_sppp_open;
  540. d->stop = cosa_sppp_close;
  541. d->hard_start_xmit = cosa_sppp_tx;
  542. d->do_ioctl = cosa_sppp_ioctl;
  543. d->get_stats = cosa_net_stats;
  544. d->tx_timeout = cosa_sppp_timeout;
  545. d->watchdog_timeo = TX_TIMEOUT;
  546. }
  547. static void sppp_channel_init(struct channel_data *chan)
  548. {
  549. struct net_device *d;
  550. chan->if_ptr = &chan->pppdev;
  551. d = alloc_netdev(0, chan->name, cosa_setup);
  552. if (!d) {
  553. printk(KERN_WARNING "%s: alloc_netdev failed.\n", chan->name);
  554. return;
  555. }
  556. chan->pppdev.dev = d;
  557. d->base_addr = chan->cosa->datareg;
  558. d->irq = chan->cosa->irq;
  559. d->dma = chan->cosa->dma;
  560. d->priv = chan;
  561. sppp_attach(&chan->pppdev);
  562. if (register_netdev(d)) {
  563. printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
  564. sppp_detach(d);
  565. free_netdev(d);
  566. chan->pppdev.dev = NULL;
  567. return;
  568. }
  569. }
  570. static void sppp_channel_delete(struct channel_data *chan)
  571. {
  572. unregister_netdev(chan->pppdev.dev);
  573. sppp_detach(chan->pppdev.dev);
  574. free_netdev(chan->pppdev.dev);
  575. chan->pppdev.dev = NULL;
  576. }
  577. static int cosa_sppp_open(struct net_device *d)
  578. {
  579. struct channel_data *chan = d->priv;
  580. int err;
  581. unsigned long flags;
  582. if (!(chan->cosa->firmware_status & COSA_FW_START)) {
  583. printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
  584. chan->cosa->name, chan->cosa->firmware_status);
  585. return -EPERM;
  586. }
  587. spin_lock_irqsave(&chan->cosa->lock, flags);
  588. if (chan->usage != 0) {
  589. printk(KERN_WARNING "%s: sppp_open called with usage count %d\n",
  590. chan->name, chan->usage);
  591. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  592. return -EBUSY;
  593. }
  594. chan->setup_rx = sppp_setup_rx;
  595. chan->tx_done = sppp_tx_done;
  596. chan->rx_done = sppp_rx_done;
  597. chan->usage=-1;
  598. chan->cosa->usage++;
  599. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  600. err = sppp_open(d);
  601. if (err) {
  602. spin_lock_irqsave(&chan->cosa->lock, flags);
  603. chan->usage=0;
  604. chan->cosa->usage--;
  605. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  606. return err;
  607. }
  608. netif_start_queue(d);
  609. cosa_enable_rx(chan);
  610. return 0;
  611. }
  612. static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *dev)
  613. {
  614. struct channel_data *chan = dev->priv;
  615. netif_stop_queue(dev);
  616. chan->tx_skb = skb;
  617. cosa_start_tx(chan, skb->data, skb->len);
  618. return 0;
  619. }
  620. static void cosa_sppp_timeout(struct net_device *dev)
  621. {
  622. struct channel_data *chan = dev->priv;
  623. if (test_bit(RXBIT, &chan->cosa->rxtx)) {
  624. chan->stats.rx_errors++;
  625. chan->stats.rx_missed_errors++;
  626. } else {
  627. chan->stats.tx_errors++;
  628. chan->stats.tx_aborted_errors++;
  629. }
  630. cosa_kick(chan->cosa);
  631. if (chan->tx_skb) {
  632. dev_kfree_skb(chan->tx_skb);
  633. chan->tx_skb = NULL;
  634. }
  635. netif_wake_queue(dev);
  636. }
  637. static int cosa_sppp_close(struct net_device *d)
  638. {
  639. struct channel_data *chan = d->priv;
  640. unsigned long flags;
  641. netif_stop_queue(d);
  642. sppp_close(d);
  643. cosa_disable_rx(chan);
  644. spin_lock_irqsave(&chan->cosa->lock, flags);
  645. if (chan->rx_skb) {
  646. kfree_skb(chan->rx_skb);
  647. chan->rx_skb = NULL;
  648. }
  649. if (chan->tx_skb) {
  650. kfree_skb(chan->tx_skb);
  651. chan->tx_skb = NULL;
  652. }
  653. chan->usage=0;
  654. chan->cosa->usage--;
  655. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  656. return 0;
  657. }
  658. static char *sppp_setup_rx(struct channel_data *chan, int size)
  659. {
  660. /*
  661. * We can safely fall back to non-dma-able memory, because we have
  662. * the cosa->bouncebuf pre-allocated.
  663. */
  664. if (chan->rx_skb)
  665. kfree_skb(chan->rx_skb);
  666. chan->rx_skb = dev_alloc_skb(size);
  667. if (chan->rx_skb == NULL) {
  668. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
  669. chan->name);
  670. chan->stats.rx_dropped++;
  671. return NULL;
  672. }
  673. chan->pppdev.dev->trans_start = jiffies;
  674. return skb_put(chan->rx_skb, size);
  675. }
  676. static int sppp_rx_done(struct channel_data *chan)
  677. {
  678. if (!chan->rx_skb) {
  679. printk(KERN_WARNING "%s: rx_done with empty skb!\n",
  680. chan->name);
  681. chan->stats.rx_errors++;
  682. chan->stats.rx_frame_errors++;
  683. return 0;
  684. }
  685. chan->rx_skb->protocol = htons(ETH_P_WAN_PPP);
  686. chan->rx_skb->dev = chan->pppdev.dev;
  687. skb_reset_mac_header(chan->rx_skb);
  688. chan->stats.rx_packets++;
  689. chan->stats.rx_bytes += chan->cosa->rxsize;
  690. netif_rx(chan->rx_skb);
  691. chan->rx_skb = NULL;
  692. chan->pppdev.dev->last_rx = jiffies;
  693. return 0;
  694. }
  695. /* ARGSUSED */
  696. static int sppp_tx_done(struct channel_data *chan, int size)
  697. {
  698. if (!chan->tx_skb) {
  699. printk(KERN_WARNING "%s: tx_done with empty skb!\n",
  700. chan->name);
  701. chan->stats.tx_errors++;
  702. chan->stats.tx_aborted_errors++;
  703. return 1;
  704. }
  705. dev_kfree_skb_irq(chan->tx_skb);
  706. chan->tx_skb = NULL;
  707. chan->stats.tx_packets++;
  708. chan->stats.tx_bytes += size;
  709. netif_wake_queue(chan->pppdev.dev);
  710. return 1;
  711. }
  712. static struct net_device_stats *cosa_net_stats(struct net_device *dev)
  713. {
  714. struct channel_data *chan = dev->priv;
  715. return &chan->stats;
  716. }
  717. /*---------- Character device ---------- */
  718. static void chardev_channel_init(struct channel_data *chan)
  719. {
  720. init_MUTEX(&chan->rsem);
  721. init_MUTEX(&chan->wsem);
  722. }
  723. static ssize_t cosa_read(struct file *file,
  724. char __user *buf, size_t count, loff_t *ppos)
  725. {
  726. DECLARE_WAITQUEUE(wait, current);
  727. unsigned long flags;
  728. struct channel_data *chan = file->private_data;
  729. struct cosa_data *cosa = chan->cosa;
  730. char *kbuf;
  731. if (!(cosa->firmware_status & COSA_FW_START)) {
  732. printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
  733. cosa->name, cosa->firmware_status);
  734. return -EPERM;
  735. }
  736. if (down_interruptible(&chan->rsem))
  737. return -ERESTARTSYS;
  738. if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
  739. printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
  740. up(&chan->rsem);
  741. return -ENOMEM;
  742. }
  743. chan->rx_status = 0;
  744. cosa_enable_rx(chan);
  745. spin_lock_irqsave(&cosa->lock, flags);
  746. add_wait_queue(&chan->rxwaitq, &wait);
  747. while(!chan->rx_status) {
  748. current->state = TASK_INTERRUPTIBLE;
  749. spin_unlock_irqrestore(&cosa->lock, flags);
  750. schedule();
  751. spin_lock_irqsave(&cosa->lock, flags);
  752. if (signal_pending(current) && chan->rx_status == 0) {
  753. chan->rx_status = 1;
  754. remove_wait_queue(&chan->rxwaitq, &wait);
  755. current->state = TASK_RUNNING;
  756. spin_unlock_irqrestore(&cosa->lock, flags);
  757. up(&chan->rsem);
  758. return -ERESTARTSYS;
  759. }
  760. }
  761. remove_wait_queue(&chan->rxwaitq, &wait);
  762. current->state = TASK_RUNNING;
  763. kbuf = chan->rxdata;
  764. count = chan->rxsize;
  765. spin_unlock_irqrestore(&cosa->lock, flags);
  766. up(&chan->rsem);
  767. if (copy_to_user(buf, kbuf, count)) {
  768. kfree(kbuf);
  769. return -EFAULT;
  770. }
  771. kfree(kbuf);
  772. return count;
  773. }
  774. static char *chrdev_setup_rx(struct channel_data *chan, int size)
  775. {
  776. /* Expect size <= COSA_MTU */
  777. chan->rxsize = size;
  778. return chan->rxdata;
  779. }
  780. static int chrdev_rx_done(struct channel_data *chan)
  781. {
  782. if (chan->rx_status) { /* Reader has died */
  783. kfree(chan->rxdata);
  784. up(&chan->wsem);
  785. }
  786. chan->rx_status = 1;
  787. wake_up_interruptible(&chan->rxwaitq);
  788. return 1;
  789. }
  790. static ssize_t cosa_write(struct file *file,
  791. const char __user *buf, size_t count, loff_t *ppos)
  792. {
  793. DECLARE_WAITQUEUE(wait, current);
  794. struct channel_data *chan = file->private_data;
  795. struct cosa_data *cosa = chan->cosa;
  796. unsigned long flags;
  797. char *kbuf;
  798. if (!(cosa->firmware_status & COSA_FW_START)) {
  799. printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
  800. cosa->name, cosa->firmware_status);
  801. return -EPERM;
  802. }
  803. if (down_interruptible(&chan->wsem))
  804. return -ERESTARTSYS;
  805. if (count > COSA_MTU)
  806. count = COSA_MTU;
  807. /* Allocate the buffer */
  808. if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
  809. printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
  810. cosa->name);
  811. up(&chan->wsem);
  812. return -ENOMEM;
  813. }
  814. if (copy_from_user(kbuf, buf, count)) {
  815. up(&chan->wsem);
  816. kfree(kbuf);
  817. return -EFAULT;
  818. }
  819. chan->tx_status=0;
  820. cosa_start_tx(chan, kbuf, count);
  821. spin_lock_irqsave(&cosa->lock, flags);
  822. add_wait_queue(&chan->txwaitq, &wait);
  823. while(!chan->tx_status) {
  824. current->state = TASK_INTERRUPTIBLE;
  825. spin_unlock_irqrestore(&cosa->lock, flags);
  826. schedule();
  827. spin_lock_irqsave(&cosa->lock, flags);
  828. if (signal_pending(current) && chan->tx_status == 0) {
  829. chan->tx_status = 1;
  830. remove_wait_queue(&chan->txwaitq, &wait);
  831. current->state = TASK_RUNNING;
  832. chan->tx_status = 1;
  833. spin_unlock_irqrestore(&cosa->lock, flags);
  834. return -ERESTARTSYS;
  835. }
  836. }
  837. remove_wait_queue(&chan->txwaitq, &wait);
  838. current->state = TASK_RUNNING;
  839. up(&chan->wsem);
  840. spin_unlock_irqrestore(&cosa->lock, flags);
  841. kfree(kbuf);
  842. return count;
  843. }
  844. static int chrdev_tx_done(struct channel_data *chan, int size)
  845. {
  846. if (chan->tx_status) { /* Writer was interrupted */
  847. kfree(chan->txbuf);
  848. up(&chan->wsem);
  849. }
  850. chan->tx_status = 1;
  851. wake_up_interruptible(&chan->txwaitq);
  852. return 1;
  853. }
  854. static unsigned int cosa_poll(struct file *file, poll_table *poll)
  855. {
  856. printk(KERN_INFO "cosa_poll is here\n");
  857. return 0;
  858. }
  859. static int cosa_open(struct inode *inode, struct file *file)
  860. {
  861. struct cosa_data *cosa;
  862. struct channel_data *chan;
  863. unsigned long flags;
  864. int n;
  865. if ((n=iminor(file->f_path.dentry->d_inode)>>CARD_MINOR_BITS)
  866. >= nr_cards)
  867. return -ENODEV;
  868. cosa = cosa_cards+n;
  869. if ((n=iminor(file->f_path.dentry->d_inode)
  870. & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels)
  871. return -ENODEV;
  872. chan = cosa->chan + n;
  873. file->private_data = chan;
  874. spin_lock_irqsave(&cosa->lock, flags);
  875. if (chan->usage < 0) { /* in netdev mode */
  876. spin_unlock_irqrestore(&cosa->lock, flags);
  877. return -EBUSY;
  878. }
  879. cosa->usage++;
  880. chan->usage++;
  881. chan->tx_done = chrdev_tx_done;
  882. chan->setup_rx = chrdev_setup_rx;
  883. chan->rx_done = chrdev_rx_done;
  884. spin_unlock_irqrestore(&cosa->lock, flags);
  885. return 0;
  886. }
  887. static int cosa_release(struct inode *inode, struct file *file)
  888. {
  889. struct channel_data *channel = file->private_data;
  890. struct cosa_data *cosa;
  891. unsigned long flags;
  892. cosa = channel->cosa;
  893. spin_lock_irqsave(&cosa->lock, flags);
  894. cosa->usage--;
  895. channel->usage--;
  896. spin_unlock_irqrestore(&cosa->lock, flags);
  897. return 0;
  898. }
  899. #ifdef COSA_FASYNC_WORKING
  900. static struct fasync_struct *fasync[256] = { NULL, };
  901. /* To be done ... */
  902. static int cosa_fasync(struct inode *inode, struct file *file, int on)
  903. {
  904. int port = iminor(inode);
  905. int rv = fasync_helper(inode, file, on, &fasync[port]);
  906. return rv < 0 ? rv : 0;
  907. }
  908. #endif
  909. /* ---------- Ioctls ---------- */
  910. /*
  911. * Ioctl subroutines can safely be made inline, because they are called
  912. * only from cosa_ioctl().
  913. */
  914. static inline int cosa_reset(struct cosa_data *cosa)
  915. {
  916. char idstring[COSA_MAX_ID_STRING];
  917. if (cosa->usage > 1)
  918. printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  919. cosa->num, cosa->usage);
  920. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
  921. if (cosa_reset_and_read_id(cosa, idstring) < 0) {
  922. printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
  923. return -EIO;
  924. }
  925. printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
  926. idstring);
  927. cosa->firmware_status |= COSA_FW_RESET;
  928. return 0;
  929. }
  930. /* High-level function to download data into COSA memory. Calls download() */
  931. static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
  932. {
  933. struct cosa_download d;
  934. int i;
  935. if (cosa->usage > 1)
  936. printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  937. cosa->name, cosa->usage);
  938. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  939. printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
  940. cosa->name, cosa->firmware_status);
  941. return -EPERM;
  942. }
  943. if (copy_from_user(&d, arg, sizeof(d)))
  944. return -EFAULT;
  945. if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
  946. return -EINVAL;
  947. if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
  948. return -EINVAL;
  949. /* If something fails, force the user to reset the card */
  950. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
  951. i = download(cosa, d.code, d.len, d.addr);
  952. if (i < 0) {
  953. printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
  954. cosa->num, i);
  955. return -EIO;
  956. }
  957. printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
  958. cosa->num, d.len, d.addr);
  959. cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
  960. return 0;
  961. }
  962. /* High-level function to read COSA memory. Calls readmem() */
  963. static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
  964. {
  965. struct cosa_download d;
  966. int i;
  967. if (cosa->usage > 1)
  968. printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
  969. "cosa->usage > 1 (%d). Odd things may happen.\n",
  970. cosa->num, cosa->usage);
  971. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  972. printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
  973. cosa->name, cosa->firmware_status);
  974. return -EPERM;
  975. }
  976. if (copy_from_user(&d, arg, sizeof(d)))
  977. return -EFAULT;
  978. /* If something fails, force the user to reset the card */
  979. cosa->firmware_status &= ~COSA_FW_RESET;
  980. i = readmem(cosa, d.code, d.len, d.addr);
  981. if (i < 0) {
  982. printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
  983. cosa->num, i);
  984. return -EIO;
  985. }
  986. printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
  987. cosa->num, d.len, d.addr);
  988. cosa->firmware_status |= COSA_FW_RESET;
  989. return 0;
  990. }
  991. /* High-level function to start microcode. Calls startmicrocode(). */
  992. static inline int cosa_start(struct cosa_data *cosa, int address)
  993. {
  994. int i;
  995. if (cosa->usage > 1)
  996. printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  997. cosa->num, cosa->usage);
  998. if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
  999. != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
  1000. printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
  1001. cosa->name, cosa->firmware_status);
  1002. return -EPERM;
  1003. }
  1004. cosa->firmware_status &= ~COSA_FW_RESET;
  1005. if ((i=startmicrocode(cosa, address)) < 0) {
  1006. printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
  1007. cosa->num, address, i);
  1008. return -EIO;
  1009. }
  1010. printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
  1011. cosa->num, address);
  1012. cosa->startaddr = address;
  1013. cosa->firmware_status |= COSA_FW_START;
  1014. return 0;
  1015. }
  1016. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  1017. static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
  1018. {
  1019. int l = strlen(cosa->id_string)+1;
  1020. if (copy_to_user(string, cosa->id_string, l))
  1021. return -EFAULT;
  1022. return l;
  1023. }
  1024. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  1025. static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
  1026. {
  1027. int l = strlen(cosa->type)+1;
  1028. if (copy_to_user(string, cosa->type, l))
  1029. return -EFAULT;
  1030. return l;
  1031. }
  1032. static int cosa_ioctl_common(struct cosa_data *cosa,
  1033. struct channel_data *channel, unsigned int cmd, unsigned long arg)
  1034. {
  1035. void __user *argp = (void __user *)arg;
  1036. switch(cmd) {
  1037. case COSAIORSET: /* Reset the device */
  1038. if (!capable(CAP_NET_ADMIN))
  1039. return -EACCES;
  1040. return cosa_reset(cosa);
  1041. case COSAIOSTRT: /* Start the firmware */
  1042. if (!capable(CAP_SYS_RAWIO))
  1043. return -EACCES;
  1044. return cosa_start(cosa, arg);
  1045. case COSAIODOWNLD: /* Download the firmware */
  1046. if (!capable(CAP_SYS_RAWIO))
  1047. return -EACCES;
  1048. return cosa_download(cosa, argp);
  1049. case COSAIORMEM:
  1050. if (!capable(CAP_SYS_RAWIO))
  1051. return -EACCES;
  1052. return cosa_readmem(cosa, argp);
  1053. case COSAIORTYPE:
  1054. return cosa_gettype(cosa, argp);
  1055. case COSAIORIDSTR:
  1056. return cosa_getidstr(cosa, argp);
  1057. case COSAIONRCARDS:
  1058. return nr_cards;
  1059. case COSAIONRCHANS:
  1060. return cosa->nchannels;
  1061. case COSAIOBMSET:
  1062. if (!capable(CAP_SYS_RAWIO))
  1063. return -EACCES;
  1064. if (is_8bit(cosa))
  1065. return -EINVAL;
  1066. if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
  1067. return -EINVAL;
  1068. cosa->busmaster = arg;
  1069. return 0;
  1070. case COSAIOBMGET:
  1071. return cosa->busmaster;
  1072. }
  1073. return -ENOIOCTLCMD;
  1074. }
  1075. static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr,
  1076. int cmd)
  1077. {
  1078. int rv;
  1079. struct channel_data *chan = dev->priv;
  1080. rv = cosa_ioctl_common(chan->cosa, chan, cmd, (unsigned long)ifr->ifr_data);
  1081. if (rv == -ENOIOCTLCMD) {
  1082. return sppp_do_ioctl(dev, ifr, cmd);
  1083. }
  1084. return rv;
  1085. }
  1086. static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
  1087. unsigned int cmd, unsigned long arg)
  1088. {
  1089. struct channel_data *channel = file->private_data;
  1090. struct cosa_data *cosa = channel->cosa;
  1091. return cosa_ioctl_common(cosa, channel, cmd, arg);
  1092. }
  1093. /*---------- HW layer interface ---------- */
  1094. /*
  1095. * The higher layer can bind itself to the HW layer by setting the callbacks
  1096. * in the channel_data structure and by using these routines.
  1097. */
  1098. static void cosa_enable_rx(struct channel_data *chan)
  1099. {
  1100. struct cosa_data *cosa = chan->cosa;
  1101. if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
  1102. put_driver_status(cosa);
  1103. }
  1104. static void cosa_disable_rx(struct channel_data *chan)
  1105. {
  1106. struct cosa_data *cosa = chan->cosa;
  1107. if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
  1108. put_driver_status(cosa);
  1109. }
  1110. /*
  1111. * FIXME: This routine probably should check for cosa_start_tx() called when
  1112. * the previous transmit is still unfinished. In this case the non-zero
  1113. * return value should indicate to the caller that the queuing(sp?) up
  1114. * the transmit has failed.
  1115. */
  1116. static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
  1117. {
  1118. struct cosa_data *cosa = chan->cosa;
  1119. unsigned long flags;
  1120. #ifdef DEBUG_DATA
  1121. int i;
  1122. printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
  1123. chan->num, len);
  1124. for (i=0; i<len; i++)
  1125. printk(" %02x", buf[i]&0xff);
  1126. printk("\n");
  1127. #endif
  1128. spin_lock_irqsave(&cosa->lock, flags);
  1129. chan->txbuf = buf;
  1130. chan->txsize = len;
  1131. if (len > COSA_MTU)
  1132. chan->txsize = COSA_MTU;
  1133. spin_unlock_irqrestore(&cosa->lock, flags);
  1134. /* Tell the firmware we are ready */
  1135. set_bit(chan->num, &cosa->txbitmap);
  1136. put_driver_status(cosa);
  1137. return 0;
  1138. }
  1139. static void put_driver_status(struct cosa_data *cosa)
  1140. {
  1141. unsigned long flags;
  1142. int status;
  1143. spin_lock_irqsave(&cosa->lock, flags);
  1144. status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
  1145. | (cosa->txbitmap ? DRIVER_TX_READY : 0)
  1146. | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
  1147. &DRIVER_TXMAP_MASK : 0);
  1148. if (!cosa->rxtx) {
  1149. if (cosa->rxbitmap|cosa->txbitmap) {
  1150. if (!cosa->enabled) {
  1151. cosa_putstatus(cosa, SR_RX_INT_ENA);
  1152. #ifdef DEBUG_IO
  1153. debug_status_out(cosa, SR_RX_INT_ENA);
  1154. #endif
  1155. cosa->enabled = 1;
  1156. }
  1157. } else if (cosa->enabled) {
  1158. cosa->enabled = 0;
  1159. cosa_putstatus(cosa, 0);
  1160. #ifdef DEBUG_IO
  1161. debug_status_out(cosa, 0);
  1162. #endif
  1163. }
  1164. cosa_putdata8(cosa, status);
  1165. #ifdef DEBUG_IO
  1166. debug_data_cmd(cosa, status);
  1167. #endif
  1168. }
  1169. spin_unlock_irqrestore(&cosa->lock, flags);
  1170. }
  1171. static void put_driver_status_nolock(struct cosa_data *cosa)
  1172. {
  1173. int status;
  1174. status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
  1175. | (cosa->txbitmap ? DRIVER_TX_READY : 0)
  1176. | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
  1177. &DRIVER_TXMAP_MASK : 0);
  1178. if (cosa->rxbitmap|cosa->txbitmap) {
  1179. cosa_putstatus(cosa, SR_RX_INT_ENA);
  1180. #ifdef DEBUG_IO
  1181. debug_status_out(cosa, SR_RX_INT_ENA);
  1182. #endif
  1183. cosa->enabled = 1;
  1184. } else {
  1185. cosa_putstatus(cosa, 0);
  1186. #ifdef DEBUG_IO
  1187. debug_status_out(cosa, 0);
  1188. #endif
  1189. cosa->enabled = 0;
  1190. }
  1191. cosa_putdata8(cosa, status);
  1192. #ifdef DEBUG_IO
  1193. debug_data_cmd(cosa, status);
  1194. #endif
  1195. }
  1196. /*
  1197. * The "kickme" function: When the DMA times out, this is called to
  1198. * clean up the driver status.
  1199. * FIXME: Preliminary support, the interface is probably wrong.
  1200. */
  1201. static void cosa_kick(struct cosa_data *cosa)
  1202. {
  1203. unsigned long flags, flags1;
  1204. char *s = "(probably) IRQ";
  1205. if (test_bit(RXBIT, &cosa->rxtx))
  1206. s = "RX DMA";
  1207. if (test_bit(TXBIT, &cosa->rxtx))
  1208. s = "TX DMA";
  1209. printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
  1210. spin_lock_irqsave(&cosa->lock, flags);
  1211. cosa->rxtx = 0;
  1212. flags1 = claim_dma_lock();
  1213. disable_dma(cosa->dma);
  1214. clear_dma_ff(cosa->dma);
  1215. release_dma_lock(flags1);
  1216. /* FIXME: Anything else? */
  1217. udelay(100);
  1218. cosa_putstatus(cosa, 0);
  1219. udelay(100);
  1220. (void) cosa_getdata8(cosa);
  1221. udelay(100);
  1222. cosa_putdata8(cosa, 0);
  1223. udelay(100);
  1224. put_driver_status_nolock(cosa);
  1225. spin_unlock_irqrestore(&cosa->lock, flags);
  1226. }
  1227. /*
  1228. * Check if the whole buffer is DMA-able. It means it is below the 16M of
  1229. * physical memory and doesn't span the 64k boundary. For now it seems
  1230. * SKB's never do this, but we'll check this anyway.
  1231. */
  1232. static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
  1233. {
  1234. static int count;
  1235. unsigned long b = (unsigned long)buf;
  1236. if (b+len >= MAX_DMA_ADDRESS)
  1237. return 0;
  1238. if ((b^ (b+len)) & 0x10000) {
  1239. if (count++ < 5)
  1240. printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
  1241. chan->name);
  1242. return 0;
  1243. }
  1244. return 1;
  1245. }
  1246. /* ---------- The SRP/COSA ROM monitor functions ---------- */
  1247. /*
  1248. * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
  1249. * drivers need to say 4-digit hex number meaning start address of the microcode
  1250. * separated by a single space. Monitor replies by saying " =". Now driver
  1251. * has to write 4-digit hex number meaning the last byte address ended
  1252. * by a single space. Monitor has to reply with a space. Now the download
  1253. * begins. After the download monitor replies with "\r\n." (CR LF dot).
  1254. */
  1255. static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
  1256. {
  1257. int i;
  1258. if (put_wait_data(cosa, 'w') == -1) return -1;
  1259. if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
  1260. if (get_wait_data(cosa) != '=') return -3;
  1261. if (puthexnumber(cosa, address) < 0) return -4;
  1262. if (put_wait_data(cosa, ' ') == -1) return -10;
  1263. if (get_wait_data(cosa) != ' ') return -11;
  1264. if (get_wait_data(cosa) != '=') return -12;
  1265. if (puthexnumber(cosa, address+length-1) < 0) return -13;
  1266. if (put_wait_data(cosa, ' ') == -1) return -18;
  1267. if (get_wait_data(cosa) != ' ') return -19;
  1268. while (length--) {
  1269. char c;
  1270. #ifndef SRP_DOWNLOAD_AT_BOOT
  1271. if (get_user(c, microcode))
  1272. return -23; /* ??? */
  1273. #else
  1274. c = *microcode;
  1275. #endif
  1276. if (put_wait_data(cosa, c) == -1)
  1277. return -20;
  1278. microcode++;
  1279. }
  1280. if (get_wait_data(cosa) != '\r') return -21;
  1281. if (get_wait_data(cosa) != '\n') return -22;
  1282. if (get_wait_data(cosa) != '.') return -23;
  1283. #if 0
  1284. printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
  1285. #endif
  1286. return 0;
  1287. }
  1288. /*
  1289. * Starting microcode is done via the "g" command of the SRP monitor.
  1290. * The chat should be the following: "g" "g=" "<addr><CR>"
  1291. * "<CR><CR><LF><CR><LF>".
  1292. */
  1293. static int startmicrocode(struct cosa_data *cosa, int address)
  1294. {
  1295. if (put_wait_data(cosa, 'g') == -1) return -1;
  1296. if (get_wait_data(cosa) != 'g') return -2;
  1297. if (get_wait_data(cosa) != '=') return -3;
  1298. if (puthexnumber(cosa, address) < 0) return -4;
  1299. if (put_wait_data(cosa, '\r') == -1) return -5;
  1300. if (get_wait_data(cosa) != '\r') return -6;
  1301. if (get_wait_data(cosa) != '\r') return -7;
  1302. if (get_wait_data(cosa) != '\n') return -8;
  1303. if (get_wait_data(cosa) != '\r') return -9;
  1304. if (get_wait_data(cosa) != '\n') return -10;
  1305. #if 0
  1306. printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
  1307. #endif
  1308. return 0;
  1309. }
  1310. /*
  1311. * Reading memory is done via the "r" command of the SRP monitor.
  1312. * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
  1313. * Then driver can read the data and the conversation is finished
  1314. * by SRP monitor sending "<CR><LF>." (dot at the end).
  1315. *
  1316. * This routine is not needed during the normal operation and serves
  1317. * for debugging purposes only.
  1318. */
  1319. static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
  1320. {
  1321. if (put_wait_data(cosa, 'r') == -1) return -1;
  1322. if ((get_wait_data(cosa)) != 'r') return -2;
  1323. if ((get_wait_data(cosa)) != '=') return -3;
  1324. if (puthexnumber(cosa, address) < 0) return -4;
  1325. if (put_wait_data(cosa, ' ') == -1) return -5;
  1326. if (get_wait_data(cosa) != ' ') return -6;
  1327. if (get_wait_data(cosa) != '=') return -7;
  1328. if (puthexnumber(cosa, address+length-1) < 0) return -8;
  1329. if (put_wait_data(cosa, ' ') == -1) return -9;
  1330. if (get_wait_data(cosa) != ' ') return -10;
  1331. while (length--) {
  1332. char c;
  1333. int i;
  1334. if ((i=get_wait_data(cosa)) == -1) {
  1335. printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
  1336. length);
  1337. return -11;
  1338. }
  1339. c=i;
  1340. #if 1
  1341. if (put_user(c, microcode))
  1342. return -23; /* ??? */
  1343. #else
  1344. *microcode = c;
  1345. #endif
  1346. microcode++;
  1347. }
  1348. if (get_wait_data(cosa) != '\r') return -21;
  1349. if (get_wait_data(cosa) != '\n') return -22;
  1350. if (get_wait_data(cosa) != '.') return -23;
  1351. #if 0
  1352. printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
  1353. #endif
  1354. return 0;
  1355. }
  1356. /*
  1357. * This function resets the device and reads the initial prompt
  1358. * of the device's ROM monitor.
  1359. */
  1360. static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
  1361. {
  1362. int i=0, id=0, prev=0, curr=0;
  1363. /* Reset the card ... */
  1364. cosa_putstatus(cosa, 0);
  1365. cosa_getdata8(cosa);
  1366. cosa_putstatus(cosa, SR_RST);
  1367. #ifdef MODULE
  1368. msleep(500);
  1369. #else
  1370. udelay(5*100000);
  1371. #endif
  1372. /* Disable all IRQs from the card */
  1373. cosa_putstatus(cosa, 0);
  1374. /*
  1375. * Try to read the ID string. The card then prints out the
  1376. * identification string ended by the "\n\x2e".
  1377. *
  1378. * The following loop is indexed through i (instead of id)
  1379. * to avoid looping forever when for any reason
  1380. * the port returns '\r', '\n' or '\x2e' permanently.
  1381. */
  1382. for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
  1383. if ((curr = get_wait_data(cosa)) == -1) {
  1384. return -1;
  1385. }
  1386. curr &= 0xff;
  1387. if (curr != '\r' && curr != '\n' && curr != 0x2e)
  1388. idstring[id++] = curr;
  1389. if (curr == 0x2e && prev == '\n')
  1390. break;
  1391. }
  1392. /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
  1393. idstring[id] = '\0';
  1394. return id;
  1395. }
  1396. /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
  1397. /*
  1398. * This routine gets the data byte from the card waiting for the SR_RX_RDY
  1399. * bit to be set in a loop. It should be used in the exceptional cases
  1400. * only (for example when resetting the card or downloading the firmware.
  1401. */
  1402. static int get_wait_data(struct cosa_data *cosa)
  1403. {
  1404. int retries = 1000;
  1405. while (--retries) {
  1406. /* read data and return them */
  1407. if (cosa_getstatus(cosa) & SR_RX_RDY) {
  1408. short r;
  1409. r = cosa_getdata8(cosa);
  1410. #if 0
  1411. printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
  1412. #endif
  1413. return r;
  1414. }
  1415. /* sleep if not ready to read */
  1416. schedule_timeout_interruptible(1);
  1417. }
  1418. printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
  1419. cosa_getstatus(cosa));
  1420. return -1;
  1421. }
  1422. /*
  1423. * This routine puts the data byte to the card waiting for the SR_TX_RDY
  1424. * bit to be set in a loop. It should be used in the exceptional cases
  1425. * only (for example when resetting the card or downloading the firmware).
  1426. */
  1427. static int put_wait_data(struct cosa_data *cosa, int data)
  1428. {
  1429. int retries = 1000;
  1430. while (--retries) {
  1431. /* read data and return them */
  1432. if (cosa_getstatus(cosa) & SR_TX_RDY) {
  1433. cosa_putdata8(cosa, data);
  1434. #if 0
  1435. printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
  1436. #endif
  1437. return 0;
  1438. }
  1439. #if 0
  1440. /* sleep if not ready to read */
  1441. schedule_timeout_interruptible(1);
  1442. #endif
  1443. }
  1444. printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
  1445. cosa->num, cosa_getstatus(cosa));
  1446. return -1;
  1447. }
  1448. /*
  1449. * The following routine puts the hexadecimal number into the SRP monitor
  1450. * and verifies the proper echo of the sent bytes. Returns 0 on success,
  1451. * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
  1452. * (-2,-4,-6,-8) means that reading echo failed.
  1453. */
  1454. static int puthexnumber(struct cosa_data *cosa, int number)
  1455. {
  1456. char temp[5];
  1457. int i;
  1458. /* Well, I should probably replace this by something faster. */
  1459. sprintf(temp, "%04X", number);
  1460. for (i=0; i<4; i++) {
  1461. if (put_wait_data(cosa, temp[i]) == -1) {
  1462. printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
  1463. cosa->num, i);
  1464. return -1-2*i;
  1465. }
  1466. if (get_wait_data(cosa) != temp[i]) {
  1467. printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
  1468. cosa->num, i);
  1469. return -2-2*i;
  1470. }
  1471. }
  1472. return 0;
  1473. }
  1474. /* ---------- Interrupt routines ---------- */
  1475. /*
  1476. * There are three types of interrupt:
  1477. * At the beginning of transmit - this handled is in tx_interrupt(),
  1478. * at the beginning of receive - it is in rx_interrupt() and
  1479. * at the end of transmit/receive - it is the eot_interrupt() function.
  1480. * These functions are multiplexed by cosa_interrupt() according to the
  1481. * COSA status byte. I have moved the rx/tx/eot interrupt handling into
  1482. * separate functions to make it more readable. These functions are inline,
  1483. * so there should be no overhead of function call.
  1484. *
  1485. * In the COSA bus-master mode, we need to tell the card the address of a
  1486. * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
  1487. * It's time to use the bottom half :-(
  1488. */
  1489. /*
  1490. * Transmit interrupt routine - called when COSA is willing to obtain
  1491. * data from the OS. The most tricky part of the routine is selection
  1492. * of channel we (OS) want to send packet for. For SRP we should probably
  1493. * use the round-robin approach. The newer COSA firmwares have a simple
  1494. * flow-control - in the status word has bits 2 and 3 set to 1 means that the
  1495. * channel 0 or 1 doesn't want to receive data.
  1496. *
  1497. * It seems there is a bug in COSA firmware (need to trace it further):
  1498. * When the driver status says that the kernel has no more data for transmit
  1499. * (e.g. at the end of TX DMA) and then the kernel changes its mind
  1500. * (e.g. new packet is queued to hard_start_xmit()), the card issues
  1501. * the TX interrupt but does not mark the channel as ready-to-transmit.
  1502. * The fix seems to be to push the packet to COSA despite its request.
  1503. * We first try to obey the card's opinion, and then fall back to forced TX.
  1504. */
  1505. static inline void tx_interrupt(struct cosa_data *cosa, int status)
  1506. {
  1507. unsigned long flags, flags1;
  1508. #ifdef DEBUG_IRQS
  1509. printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
  1510. cosa->num, status);
  1511. #endif
  1512. spin_lock_irqsave(&cosa->lock, flags);
  1513. set_bit(TXBIT, &cosa->rxtx);
  1514. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1515. /* flow control, see the comment above */
  1516. int i=0;
  1517. if (!cosa->txbitmap) {
  1518. printk(KERN_WARNING "%s: No channel wants data "
  1519. "in TX IRQ. Expect DMA timeout.",
  1520. cosa->name);
  1521. put_driver_status_nolock(cosa);
  1522. clear_bit(TXBIT, &cosa->rxtx);
  1523. spin_unlock_irqrestore(&cosa->lock, flags);
  1524. return;
  1525. }
  1526. while(1) {
  1527. cosa->txchan++;
  1528. i++;
  1529. if (cosa->txchan >= cosa->nchannels)
  1530. cosa->txchan = 0;
  1531. if (!(cosa->txbitmap & (1<<cosa->txchan)))
  1532. continue;
  1533. if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
  1534. break;
  1535. /* in second pass, accept first ready-to-TX channel */
  1536. if (i > cosa->nchannels) {
  1537. /* Can be safely ignored */
  1538. #ifdef DEBUG_IRQS
  1539. printk(KERN_DEBUG "%s: Forcing TX "
  1540. "to not-ready channel %d\n",
  1541. cosa->name, cosa->txchan);
  1542. #endif
  1543. break;
  1544. }
  1545. }
  1546. cosa->txsize = cosa->chan[cosa->txchan].txsize;
  1547. if (cosa_dma_able(cosa->chan+cosa->txchan,
  1548. cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
  1549. cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
  1550. } else {
  1551. memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
  1552. cosa->txsize);
  1553. cosa->txbuf = cosa->bouncebuf;
  1554. }
  1555. }
  1556. if (is_8bit(cosa)) {
  1557. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1558. cosa_putstatus(cosa, SR_TX_INT_ENA);
  1559. cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
  1560. ((cosa->txsize >> 8) & 0x1f));
  1561. #ifdef DEBUG_IO
  1562. debug_status_out(cosa, SR_TX_INT_ENA);
  1563. debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
  1564. ((cosa->txsize >> 8) & 0x1f));
  1565. debug_data_in(cosa, cosa_getdata8(cosa));
  1566. #else
  1567. cosa_getdata8(cosa);
  1568. #endif
  1569. set_bit(IRQBIT, &cosa->rxtx);
  1570. spin_unlock_irqrestore(&cosa->lock, flags);
  1571. return;
  1572. } else {
  1573. clear_bit(IRQBIT, &cosa->rxtx);
  1574. cosa_putstatus(cosa, 0);
  1575. cosa_putdata8(cosa, cosa->txsize&0xff);
  1576. #ifdef DEBUG_IO
  1577. debug_status_out(cosa, 0);
  1578. debug_data_out(cosa, cosa->txsize&0xff);
  1579. #endif
  1580. }
  1581. } else {
  1582. cosa_putstatus(cosa, SR_TX_INT_ENA);
  1583. cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
  1584. | (cosa->txsize & 0x1fff));
  1585. #ifdef DEBUG_IO
  1586. debug_status_out(cosa, SR_TX_INT_ENA);
  1587. debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
  1588. | (cosa->txsize & 0x1fff));
  1589. debug_data_in(cosa, cosa_getdata8(cosa));
  1590. debug_status_out(cosa, 0);
  1591. #else
  1592. cosa_getdata8(cosa);
  1593. #endif
  1594. cosa_putstatus(cosa, 0);
  1595. }
  1596. if (cosa->busmaster) {
  1597. unsigned long addr = virt_to_bus(cosa->txbuf);
  1598. int count=0;
  1599. printk(KERN_INFO "busmaster IRQ\n");
  1600. while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
  1601. count++;
  1602. udelay(10);
  1603. if (count > 1000) break;
  1604. }
  1605. printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
  1606. printk(KERN_INFO "ready after %d loops\n", count);
  1607. cosa_putdata16(cosa, (addr >> 16)&0xffff);
  1608. count = 0;
  1609. while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
  1610. count++;
  1611. if (count > 1000) break;
  1612. udelay(10);
  1613. }
  1614. printk(KERN_INFO "ready after %d loops\n", count);
  1615. cosa_putdata16(cosa, addr &0xffff);
  1616. flags1 = claim_dma_lock();
  1617. set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
  1618. enable_dma(cosa->dma);
  1619. release_dma_lock(flags1);
  1620. } else {
  1621. /* start the DMA */
  1622. flags1 = claim_dma_lock();
  1623. disable_dma(cosa->dma);
  1624. clear_dma_ff(cosa->dma);
  1625. set_dma_mode(cosa->dma, DMA_MODE_WRITE);
  1626. set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
  1627. set_dma_count(cosa->dma, cosa->txsize);
  1628. enable_dma(cosa->dma);
  1629. release_dma_lock(flags1);
  1630. }
  1631. cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
  1632. #ifdef DEBUG_IO
  1633. debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
  1634. #endif
  1635. spin_unlock_irqrestore(&cosa->lock, flags);
  1636. }
  1637. static inline void rx_interrupt(struct cosa_data *cosa, int status)
  1638. {
  1639. unsigned long flags;
  1640. #ifdef DEBUG_IRQS
  1641. printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
  1642. #endif
  1643. spin_lock_irqsave(&cosa->lock, flags);
  1644. set_bit(RXBIT, &cosa->rxtx);
  1645. if (is_8bit(cosa)) {
  1646. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1647. set_bit(IRQBIT, &cosa->rxtx);
  1648. put_driver_status_nolock(cosa);
  1649. cosa->rxsize = cosa_getdata8(cosa) <<8;
  1650. #ifdef DEBUG_IO
  1651. debug_data_in(cosa, cosa->rxsize >> 8);
  1652. #endif
  1653. spin_unlock_irqrestore(&cosa->lock, flags);
  1654. return;
  1655. } else {
  1656. clear_bit(IRQBIT, &cosa->rxtx);
  1657. cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
  1658. #ifdef DEBUG_IO
  1659. debug_data_in(cosa, cosa->rxsize & 0xff);
  1660. #endif
  1661. #if 0
  1662. printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
  1663. cosa->num, cosa->rxsize);
  1664. #endif
  1665. }
  1666. } else {
  1667. cosa->rxsize = cosa_getdata16(cosa);
  1668. #ifdef DEBUG_IO
  1669. debug_data_in(cosa, cosa->rxsize);
  1670. #endif
  1671. #if 0
  1672. printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
  1673. cosa->num, cosa->rxsize);
  1674. #endif
  1675. }
  1676. if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
  1677. printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
  1678. cosa->name, cosa->rxsize);
  1679. spin_unlock_irqrestore(&cosa->lock, flags);
  1680. goto reject;
  1681. }
  1682. cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
  1683. cosa->rxsize &= 0x1fff;
  1684. spin_unlock_irqrestore(&cosa->lock, flags);
  1685. cosa->rxbuf = NULL;
  1686. if (cosa->rxchan->setup_rx)
  1687. cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
  1688. if (!cosa->rxbuf) {
  1689. reject: /* Reject the packet */
  1690. printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
  1691. cosa->num, cosa->rxchan->num);
  1692. cosa->rxbuf = cosa->bouncebuf;
  1693. }
  1694. /* start the DMA */
  1695. flags = claim_dma_lock();
  1696. disable_dma(cosa->dma);
  1697. clear_dma_ff(cosa->dma);
  1698. set_dma_mode(cosa->dma, DMA_MODE_READ);
  1699. if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
  1700. set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
  1701. } else {
  1702. set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
  1703. }
  1704. set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
  1705. enable_dma(cosa->dma);
  1706. release_dma_lock(flags);
  1707. spin_lock_irqsave(&cosa->lock, flags);
  1708. cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
  1709. if (!is_8bit(cosa) && (status & SR_TX_RDY))
  1710. cosa_putdata8(cosa, DRIVER_RX_READY);
  1711. #ifdef DEBUG_IO
  1712. debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
  1713. if (!is_8bit(cosa) && (status & SR_TX_RDY))
  1714. debug_data_cmd(cosa, DRIVER_RX_READY);
  1715. #endif
  1716. spin_unlock_irqrestore(&cosa->lock, flags);
  1717. }
  1718. static inline void eot_interrupt(struct cosa_data *cosa, int status)
  1719. {
  1720. unsigned long flags, flags1;
  1721. spin_lock_irqsave(&cosa->lock, flags);
  1722. flags1 = claim_dma_lock();
  1723. disable_dma(cosa->dma);
  1724. clear_dma_ff(cosa->dma);
  1725. release_dma_lock(flags1);
  1726. if (test_bit(TXBIT, &cosa->rxtx)) {
  1727. struct channel_data *chan = cosa->chan+cosa->txchan;
  1728. if (chan->tx_done)
  1729. if (chan->tx_done(chan, cosa->txsize))
  1730. clear_bit(chan->num, &cosa->txbitmap);
  1731. } else if (test_bit(RXBIT, &cosa->rxtx)) {
  1732. #ifdef DEBUG_DATA
  1733. {
  1734. int i;
  1735. printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
  1736. cosa->rxchan->num, cosa->rxsize);
  1737. for (i=0; i<cosa->rxsize; i++)
  1738. printk (" %02x", cosa->rxbuf[i]&0xff);
  1739. printk("\n");
  1740. }
  1741. #endif
  1742. /* Packet for unknown channel? */
  1743. if (cosa->rxbuf == cosa->bouncebuf)
  1744. goto out;
  1745. if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
  1746. memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
  1747. if (cosa->rxchan->rx_done)
  1748. if (cosa->rxchan->rx_done(cosa->rxchan))
  1749. clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
  1750. } else {
  1751. printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
  1752. cosa->num);
  1753. }
  1754. /*
  1755. * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
  1756. * cleared anyway). We should do it as soon as possible
  1757. * so that we can tell the COSA we are done and to give it a time
  1758. * for recovery.
  1759. */
  1760. out:
  1761. cosa->rxtx = 0;
  1762. put_driver_status_nolock(cosa);
  1763. spin_unlock_irqrestore(&cosa->lock, flags);
  1764. }
  1765. static irqreturn_t cosa_interrupt(int irq, void *cosa_)
  1766. {
  1767. unsigned status;
  1768. int count = 0;
  1769. struct cosa_data *cosa = cosa_;
  1770. again:
  1771. status = cosa_getstatus(cosa);
  1772. #ifdef DEBUG_IRQS
  1773. printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
  1774. status & 0xff);
  1775. #endif
  1776. #ifdef DEBUG_IO
  1777. debug_status_in(cosa, status);
  1778. #endif
  1779. switch (status & SR_CMD_FROM_SRP_MASK) {
  1780. case SR_DOWN_REQUEST:
  1781. tx_interrupt(cosa, status);
  1782. break;
  1783. case SR_UP_REQUEST:
  1784. rx_interrupt(cosa, status);
  1785. break;
  1786. case SR_END_OF_TRANSFER:
  1787. eot_interrupt(cosa, status);
  1788. break;
  1789. default:
  1790. /* We may be too fast for SRP. Try to wait a bit more. */
  1791. if (count++ < 100) {
  1792. udelay(100);
  1793. goto again;
  1794. }
  1795. printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
  1796. cosa->num, status & 0xff, count);
  1797. }
  1798. #ifdef DEBUG_IRQS
  1799. if (count)
  1800. printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
  1801. cosa->name, count);
  1802. else
  1803. printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
  1804. #endif
  1805. return IRQ_HANDLED;
  1806. }
  1807. /* ---------- I/O debugging routines ---------- */
  1808. /*
  1809. * These routines can be used to monitor COSA/SRP I/O and to printk()
  1810. * the data being transferred on the data and status I/O port in a
  1811. * readable way.
  1812. */
  1813. #ifdef DEBUG_IO
  1814. static void debug_status_in(struct cosa_data *cosa, int status)
  1815. {
  1816. char *s;
  1817. switch(status & SR_CMD_FROM_SRP_MASK) {
  1818. case SR_UP_REQUEST:
  1819. s = "RX_REQ";
  1820. break;
  1821. case SR_DOWN_REQUEST:
  1822. s = "TX_REQ";
  1823. break;
  1824. case SR_END_OF_TRANSFER:
  1825. s = "ET_REQ";
  1826. break;
  1827. default:
  1828. s = "NO_REQ";
  1829. break;
  1830. }
  1831. printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
  1832. cosa->name,
  1833. status,
  1834. status & SR_USR_RQ ? "USR_RQ|":"",
  1835. status & SR_TX_RDY ? "TX_RDY|":"",
  1836. status & SR_RX_RDY ? "RX_RDY|":"",
  1837. s);
  1838. }
  1839. static void debug_status_out(struct cosa_data *cosa, int status)
  1840. {
  1841. printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
  1842. cosa->name,
  1843. status,
  1844. status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
  1845. status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
  1846. status & SR_RST ? "RESET|":"",
  1847. status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
  1848. status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
  1849. status & SR_RX_INT_ENA ? "RXINT":"!rxint");
  1850. }
  1851. static void debug_data_in(struct cosa_data *cosa, int data)
  1852. {
  1853. printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
  1854. }
  1855. static void debug_data_out(struct cosa_data *cosa, int data)
  1856. {
  1857. printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
  1858. }
  1859. static void debug_data_cmd(struct cosa_data *cosa, int data)
  1860. {
  1861. printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
  1862. cosa->name, data,
  1863. data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
  1864. data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
  1865. }
  1866. #endif
  1867. /* EOF -- this file has not been truncated */