cosa.c 58 KB

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