cosa.c 59 KB

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