cosa.c 58 KB

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