cosa.c 58 KB

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