cosa.c 58 KB

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