cosa.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  75. #include <linux/module.h>
  76. #include <linux/kernel.h>
  77. #include <linux/sched.h>
  78. #include <linux/slab.h>
  79. #include <linux/poll.h>
  80. #include <linux/fs.h>
  81. #include <linux/interrupt.h>
  82. #include <linux/delay.h>
  83. #include <linux/hdlc.h>
  84. #include <linux/errno.h>
  85. #include <linux/ioport.h>
  86. #include <linux/netdevice.h>
  87. #include <linux/spinlock.h>
  88. #include <linux/mutex.h>
  89. #include <linux/device.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 DEFINE_MUTEX(cosa_chardev_mutex);
  162. static int cosa_major = 117;
  163. /*
  164. * Encoding of the minor numbers:
  165. * The lowest CARD_MINOR_BITS bits means the channel on the single card,
  166. * the highest bits means the card number.
  167. */
  168. #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
  169. * for the single card */
  170. /*
  171. * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
  172. * macro doesn't like anything other than the raw number as an argument :-(
  173. */
  174. #define MAX_CARDS 16
  175. /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
  176. #define DRIVER_RX_READY 0x0001
  177. #define DRIVER_TX_READY 0x0002
  178. #define DRIVER_TXMAP_SHIFT 2
  179. #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
  180. /*
  181. * for cosa->rxtx - indicates whether either transmit or receive is
  182. * in progress. These values are mean number of the bit.
  183. */
  184. #define TXBIT 0
  185. #define RXBIT 1
  186. #define IRQBIT 2
  187. #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
  188. #undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
  189. #undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
  190. #undef DEBUG_IO //1 /* Dump the I/O traffic */
  191. #define TX_TIMEOUT (5*HZ)
  192. /* Maybe the following should be allocated dynamically */
  193. static struct cosa_data cosa_cards[MAX_CARDS];
  194. static int nr_cards;
  195. #ifdef COSA_ISA_AUTOPROBE
  196. static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
  197. /* NOTE: DMA is not autoprobed!!! */
  198. static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
  199. #else
  200. static int io[MAX_CARDS+1];
  201. static int dma[MAX_CARDS+1];
  202. #endif
  203. /* IRQ can be safely autoprobed */
  204. static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
  205. /* for class stuff*/
  206. static struct class *cosa_class;
  207. #ifdef MODULE
  208. module_param_array(io, int, NULL, 0);
  209. MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
  210. module_param_array(irq, int, NULL, 0);
  211. MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
  212. module_param_array(dma, int, NULL, 0);
  213. MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
  214. MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
  215. MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
  216. MODULE_LICENSE("GPL");
  217. #endif
  218. /* I use this mainly for testing purposes */
  219. #ifdef COSA_SLOW_IO
  220. #define cosa_outb outb_p
  221. #define cosa_outw outw_p
  222. #define cosa_inb inb_p
  223. #define cosa_inw inw_p
  224. #else
  225. #define cosa_outb outb
  226. #define cosa_outw outw
  227. #define cosa_inb inb
  228. #define cosa_inw inw
  229. #endif
  230. #define is_8bit(cosa) (!(cosa->datareg & 0x08))
  231. #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
  232. #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
  233. #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
  234. #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
  235. #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
  236. #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
  237. /* Initialization stuff */
  238. static int cosa_probe(int ioaddr, int irq, int dma);
  239. /* HW interface */
  240. static void cosa_enable_rx(struct channel_data *chan);
  241. static void cosa_disable_rx(struct channel_data *chan);
  242. static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
  243. static void cosa_kick(struct cosa_data *cosa);
  244. static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
  245. /* Network device stuff */
  246. static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
  247. unsigned short parity);
  248. static int cosa_net_open(struct net_device *d);
  249. static int cosa_net_close(struct net_device *d);
  250. static void cosa_net_timeout(struct net_device *d);
  251. static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
  252. static char *cosa_net_setup_rx(struct channel_data *channel, int size);
  253. static int cosa_net_rx_done(struct channel_data *channel);
  254. static int cosa_net_tx_done(struct channel_data *channel, int size);
  255. static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  256. /* Character device */
  257. static char *chrdev_setup_rx(struct channel_data *channel, int size);
  258. static int chrdev_rx_done(struct channel_data *channel);
  259. static int chrdev_tx_done(struct channel_data *channel, int size);
  260. static ssize_t cosa_read(struct file *file,
  261. char __user *buf, size_t count, loff_t *ppos);
  262. static ssize_t cosa_write(struct file *file,
  263. const char __user *buf, size_t count, loff_t *ppos);
  264. static unsigned int cosa_poll(struct file *file, poll_table *poll);
  265. static int cosa_open(struct inode *inode, struct file *file);
  266. static int cosa_release(struct inode *inode, struct file *file);
  267. static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
  268. unsigned long arg);
  269. #ifdef COSA_FASYNC_WORKING
  270. static int cosa_fasync(struct inode *inode, struct file *file, int on);
  271. #endif
  272. static const struct file_operations cosa_fops = {
  273. .owner = THIS_MODULE,
  274. .llseek = no_llseek,
  275. .read = cosa_read,
  276. .write = cosa_write,
  277. .poll = cosa_poll,
  278. .unlocked_ioctl = cosa_chardev_ioctl,
  279. .open = cosa_open,
  280. .release = cosa_release,
  281. #ifdef COSA_FASYNC_WORKING
  282. .fasync = cosa_fasync,
  283. #endif
  284. };
  285. /* Ioctls */
  286. static int cosa_start(struct cosa_data *cosa, int address);
  287. static int cosa_reset(struct cosa_data *cosa);
  288. static int cosa_download(struct cosa_data *cosa, void __user *a);
  289. static int cosa_readmem(struct cosa_data *cosa, void __user *a);
  290. /* COSA/SRP ROM monitor */
  291. static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
  292. static int startmicrocode(struct cosa_data *cosa, int address);
  293. static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
  294. static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
  295. /* Auxiliary functions */
  296. static int get_wait_data(struct cosa_data *cosa);
  297. static int put_wait_data(struct cosa_data *cosa, int data);
  298. static int puthexnumber(struct cosa_data *cosa, int number);
  299. static void put_driver_status(struct cosa_data *cosa);
  300. static void put_driver_status_nolock(struct cosa_data *cosa);
  301. /* Interrupt handling */
  302. static irqreturn_t cosa_interrupt(int irq, void *cosa);
  303. /* I/O ops debugging */
  304. #ifdef DEBUG_IO
  305. static void debug_data_in(struct cosa_data *cosa, int data);
  306. static void debug_data_out(struct cosa_data *cosa, int data);
  307. static void debug_data_cmd(struct cosa_data *cosa, int data);
  308. static void debug_status_in(struct cosa_data *cosa, int status);
  309. static void debug_status_out(struct cosa_data *cosa, int status);
  310. #endif
  311. static inline struct channel_data* dev_to_chan(struct net_device *dev)
  312. {
  313. return (struct channel_data *)dev_to_hdlc(dev)->priv;
  314. }
  315. /* ---------- Initialization stuff ---------- */
  316. static int __init cosa_init(void)
  317. {
  318. int i, err = 0;
  319. if (cosa_major > 0) {
  320. if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
  321. pr_warn("unable to get major %d\n", cosa_major);
  322. err = -EIO;
  323. goto out;
  324. }
  325. } else {
  326. if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
  327. pr_warn("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. pr_warn("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. pr_info("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. pr_info("invalid I/O address 0x%x\n", base);
  405. return -1;
  406. }
  407. /* DMA should be 0,1 or 3-7 */
  408. if (dma < 0 || dma == 4 || dma > 7) {
  409. pr_info("invalid DMA %d\n", dma);
  410. return -1;
  411. }
  412. /* and finally, on 16-bit COSA DMA should be 4-7 and
  413. * I/O base should not be multiple of 0x10 */
  414. if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
  415. pr_info("8/16 bit base and DMA mismatch (base=0x%x, dma=%d)\n",
  416. base, dma);
  417. return -1;
  418. }
  419. cosa->dma = dma;
  420. cosa->datareg = base;
  421. cosa->statusreg = is_8bit(cosa)?base+1:base+2;
  422. spin_lock_init(&cosa->lock);
  423. if (!request_region(base, is_8bit(cosa)?2:4,"cosa"))
  424. return -1;
  425. if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
  426. printk(KERN_DEBUG "probe at 0x%x failed.\n", base);
  427. err = -1;
  428. goto err_out;
  429. }
  430. /* Test the validity of identification string */
  431. if (!strncmp(cosa->id_string, "SRP", 3))
  432. cosa->type = "srp";
  433. else if (!strncmp(cosa->id_string, "COSA", 4))
  434. cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
  435. else {
  436. /* Print a warning only if we are not autoprobing */
  437. #ifndef COSA_ISA_AUTOPROBE
  438. pr_info("valid signature not found at 0x%x\n", base);
  439. #endif
  440. err = -1;
  441. goto err_out;
  442. }
  443. /* Update the name of the region now we know the type of card */
  444. release_region(base, is_8bit(cosa)?2:4);
  445. if (!request_region(base, is_8bit(cosa)?2:4, cosa->type)) {
  446. printk(KERN_DEBUG "changing name at 0x%x failed.\n", base);
  447. return -1;
  448. }
  449. /* Now do IRQ autoprobe */
  450. if (irq < 0) {
  451. unsigned long irqs;
  452. /* pr_info("IRQ autoprobe\n"); */
  453. irqs = probe_irq_on();
  454. /*
  455. * Enable interrupt on tx buffer empty (it sure is)
  456. * really sure ?
  457. * FIXME: When this code is not used as module, we should
  458. * probably call udelay() instead of the interruptible sleep.
  459. */
  460. set_current_state(TASK_INTERRUPTIBLE);
  461. cosa_putstatus(cosa, SR_TX_INT_ENA);
  462. schedule_timeout(msecs_to_jiffies(300));
  463. irq = probe_irq_off(irqs);
  464. /* Disable all IRQs from the card */
  465. cosa_putstatus(cosa, 0);
  466. /* Empty the received data register */
  467. cosa_getdata8(cosa);
  468. if (irq < 0) {
  469. pr_info("multiple interrupts obtained (%d, board at 0x%x)\n",
  470. irq, cosa->datareg);
  471. err = -1;
  472. goto err_out;
  473. }
  474. if (irq == 0) {
  475. pr_info("no interrupt obtained (board at 0x%x)\n",
  476. cosa->datareg);
  477. /* return -1; */
  478. }
  479. }
  480. cosa->irq = irq;
  481. cosa->num = nr_cards;
  482. cosa->usage = 0;
  483. cosa->nchannels = 2; /* FIXME: how to determine this? */
  484. if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
  485. err = -1;
  486. goto err_out;
  487. }
  488. if (request_dma(cosa->dma, cosa->type)) {
  489. err = -1;
  490. goto err_out1;
  491. }
  492. cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
  493. if (!cosa->bouncebuf) {
  494. err = -ENOMEM;
  495. goto err_out2;
  496. }
  497. sprintf(cosa->name, "cosa%d", cosa->num);
  498. /* Initialize the per-channel data */
  499. cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
  500. if (!cosa->chan) {
  501. err = -ENOMEM;
  502. goto err_out3;
  503. }
  504. for (i = 0; i < cosa->nchannels; i++) {
  505. struct channel_data *chan = &cosa->chan[i];
  506. chan->cosa = cosa;
  507. chan->num = i;
  508. sprintf(chan->name, "cosa%dc%d", chan->cosa->num, i);
  509. /* Initialize the chardev data structures */
  510. mutex_init(&chan->rlock);
  511. sema_init(&chan->wsem, 1);
  512. /* Register the network interface */
  513. if (!(chan->netdev = alloc_hdlcdev(chan))) {
  514. pr_warn("%s: alloc_hdlcdev failed\n", chan->name);
  515. err = -ENOMEM;
  516. goto err_hdlcdev;
  517. }
  518. dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
  519. dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
  520. chan->netdev->netdev_ops = &cosa_ops;
  521. chan->netdev->watchdog_timeo = TX_TIMEOUT;
  522. chan->netdev->base_addr = chan->cosa->datareg;
  523. chan->netdev->irq = chan->cosa->irq;
  524. chan->netdev->dma = chan->cosa->dma;
  525. err = register_hdlc_device(chan->netdev);
  526. if (err) {
  527. netdev_warn(chan->netdev,
  528. "register_hdlc_device() failed\n");
  529. free_netdev(chan->netdev);
  530. goto err_hdlcdev;
  531. }
  532. }
  533. pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
  534. cosa->num, cosa->id_string, cosa->type,
  535. cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
  536. return nr_cards++;
  537. err_hdlcdev:
  538. while (i-- > 0) {
  539. unregister_hdlc_device(cosa->chan[i].netdev);
  540. free_netdev(cosa->chan[i].netdev);
  541. }
  542. kfree(cosa->chan);
  543. err_out3:
  544. kfree(cosa->bouncebuf);
  545. err_out2:
  546. free_dma(cosa->dma);
  547. err_out1:
  548. free_irq(cosa->irq, cosa);
  549. err_out:
  550. release_region(cosa->datareg,is_8bit(cosa)?2:4);
  551. pr_notice("cosa%d: allocating resources failed\n", cosa->num);
  552. return err;
  553. }
  554. /*---------- network device ---------- */
  555. static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
  556. unsigned short parity)
  557. {
  558. if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
  559. return 0;
  560. return -EINVAL;
  561. }
  562. static int cosa_net_open(struct net_device *dev)
  563. {
  564. struct channel_data *chan = dev_to_chan(dev);
  565. int err;
  566. unsigned long flags;
  567. if (!(chan->cosa->firmware_status & COSA_FW_START)) {
  568. pr_notice("%s: start the firmware first (status %d)\n",
  569. chan->cosa->name, chan->cosa->firmware_status);
  570. return -EPERM;
  571. }
  572. spin_lock_irqsave(&chan->cosa->lock, flags);
  573. if (chan->usage != 0) {
  574. pr_warn("%s: cosa_net_open called with usage count %d\n",
  575. chan->name, chan->usage);
  576. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  577. return -EBUSY;
  578. }
  579. chan->setup_rx = cosa_net_setup_rx;
  580. chan->tx_done = cosa_net_tx_done;
  581. chan->rx_done = cosa_net_rx_done;
  582. chan->usage = -1;
  583. chan->cosa->usage++;
  584. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  585. err = hdlc_open(dev);
  586. if (err) {
  587. spin_lock_irqsave(&chan->cosa->lock, flags);
  588. chan->usage = 0;
  589. chan->cosa->usage--;
  590. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  591. return err;
  592. }
  593. netif_start_queue(dev);
  594. cosa_enable_rx(chan);
  595. return 0;
  596. }
  597. static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
  598. struct net_device *dev)
  599. {
  600. struct channel_data *chan = dev_to_chan(dev);
  601. netif_stop_queue(dev);
  602. chan->tx_skb = skb;
  603. cosa_start_tx(chan, skb->data, skb->len);
  604. return NETDEV_TX_OK;
  605. }
  606. static void cosa_net_timeout(struct net_device *dev)
  607. {
  608. struct channel_data *chan = dev_to_chan(dev);
  609. if (test_bit(RXBIT, &chan->cosa->rxtx)) {
  610. chan->netdev->stats.rx_errors++;
  611. chan->netdev->stats.rx_missed_errors++;
  612. } else {
  613. chan->netdev->stats.tx_errors++;
  614. chan->netdev->stats.tx_aborted_errors++;
  615. }
  616. cosa_kick(chan->cosa);
  617. if (chan->tx_skb) {
  618. dev_kfree_skb(chan->tx_skb);
  619. chan->tx_skb = NULL;
  620. }
  621. netif_wake_queue(dev);
  622. }
  623. static int cosa_net_close(struct net_device *dev)
  624. {
  625. struct channel_data *chan = dev_to_chan(dev);
  626. unsigned long flags;
  627. netif_stop_queue(dev);
  628. hdlc_close(dev);
  629. cosa_disable_rx(chan);
  630. spin_lock_irqsave(&chan->cosa->lock, flags);
  631. if (chan->rx_skb) {
  632. kfree_skb(chan->rx_skb);
  633. chan->rx_skb = NULL;
  634. }
  635. if (chan->tx_skb) {
  636. kfree_skb(chan->tx_skb);
  637. chan->tx_skb = NULL;
  638. }
  639. chan->usage = 0;
  640. chan->cosa->usage--;
  641. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  642. return 0;
  643. }
  644. static char *cosa_net_setup_rx(struct channel_data *chan, int size)
  645. {
  646. /*
  647. * We can safely fall back to non-dma-able memory, because we have
  648. * the cosa->bouncebuf pre-allocated.
  649. */
  650. kfree_skb(chan->rx_skb);
  651. chan->rx_skb = dev_alloc_skb(size);
  652. if (chan->rx_skb == NULL) {
  653. pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
  654. chan->netdev->stats.rx_dropped++;
  655. return NULL;
  656. }
  657. netif_trans_update(chan->netdev);
  658. return skb_put(chan->rx_skb, size);
  659. }
  660. static int cosa_net_rx_done(struct channel_data *chan)
  661. {
  662. if (!chan->rx_skb) {
  663. pr_warn("%s: rx_done with empty skb!\n", chan->name);
  664. chan->netdev->stats.rx_errors++;
  665. chan->netdev->stats.rx_frame_errors++;
  666. return 0;
  667. }
  668. chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
  669. chan->rx_skb->dev = chan->netdev;
  670. skb_reset_mac_header(chan->rx_skb);
  671. chan->netdev->stats.rx_packets++;
  672. chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
  673. netif_rx(chan->rx_skb);
  674. chan->rx_skb = NULL;
  675. return 0;
  676. }
  677. /* ARGSUSED */
  678. static int cosa_net_tx_done(struct channel_data *chan, int size)
  679. {
  680. if (!chan->tx_skb) {
  681. pr_warn("%s: tx_done with empty skb!\n", 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. pr_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. chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL);
  710. if (chan->rxdata == NULL) {
  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. set_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. __set_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. __set_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. pr_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. kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA);
  780. if (kbuf == NULL) {
  781. up(&chan->wsem);
  782. return -ENOMEM;
  783. }
  784. if (copy_from_user(kbuf, buf, count)) {
  785. up(&chan->wsem);
  786. kfree(kbuf);
  787. return -EFAULT;
  788. }
  789. chan->tx_status=0;
  790. cosa_start_tx(chan, kbuf, count);
  791. spin_lock_irqsave(&cosa->lock, flags);
  792. add_wait_queue(&chan->txwaitq, &wait);
  793. while (!chan->tx_status) {
  794. set_current_state(TASK_INTERRUPTIBLE);
  795. spin_unlock_irqrestore(&cosa->lock, flags);
  796. schedule();
  797. spin_lock_irqsave(&cosa->lock, flags);
  798. if (signal_pending(current) && chan->tx_status == 0) {
  799. chan->tx_status = 1;
  800. remove_wait_queue(&chan->txwaitq, &wait);
  801. __set_current_state(TASK_RUNNING);
  802. chan->tx_status = 1;
  803. spin_unlock_irqrestore(&cosa->lock, flags);
  804. up(&chan->wsem);
  805. return -ERESTARTSYS;
  806. }
  807. }
  808. remove_wait_queue(&chan->txwaitq, &wait);
  809. __set_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. pr_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. mutex_lock(&cosa_chardev_mutex);
  838. if ((n=iminor(file_inode(file))>>CARD_MINOR_BITS)
  839. >= nr_cards) {
  840. ret = -ENODEV;
  841. goto out;
  842. }
  843. cosa = cosa_cards+n;
  844. if ((n=iminor(file_inode(file))
  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. mutex_unlock(&cosa_chardev_mutex);
  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. return fasync_helper(inode, file, on, &fasync[port]);
  886. }
  887. #endif
  888. /* ---------- Ioctls ---------- */
  889. /*
  890. * Ioctl subroutines can safely be made inline, because they are called
  891. * only from cosa_ioctl().
  892. */
  893. static inline int cosa_reset(struct cosa_data *cosa)
  894. {
  895. char idstring[COSA_MAX_ID_STRING];
  896. if (cosa->usage > 1)
  897. pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  898. cosa->num, cosa->usage);
  899. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
  900. if (cosa_reset_and_read_id(cosa, idstring) < 0) {
  901. pr_notice("cosa%d: reset failed\n", cosa->num);
  902. return -EIO;
  903. }
  904. pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
  905. cosa->firmware_status |= COSA_FW_RESET;
  906. return 0;
  907. }
  908. /* High-level function to download data into COSA memory. Calls download() */
  909. static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
  910. {
  911. struct cosa_download d;
  912. int i;
  913. if (cosa->usage > 1)
  914. pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  915. cosa->name, cosa->usage);
  916. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  917. pr_notice("%s: reset the card first (status %d)\n",
  918. cosa->name, cosa->firmware_status);
  919. return -EPERM;
  920. }
  921. if (copy_from_user(&d, arg, sizeof(d)))
  922. return -EFAULT;
  923. if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
  924. return -EINVAL;
  925. if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
  926. return -EINVAL;
  927. /* If something fails, force the user to reset the card */
  928. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
  929. i = download(cosa, d.code, d.len, d.addr);
  930. if (i < 0) {
  931. pr_notice("cosa%d: microcode download failed: %d\n",
  932. cosa->num, i);
  933. return -EIO;
  934. }
  935. pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
  936. cosa->num, d.len, d.addr);
  937. cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
  938. return 0;
  939. }
  940. /* High-level function to read COSA memory. Calls readmem() */
  941. static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
  942. {
  943. struct cosa_download d;
  944. int i;
  945. if (cosa->usage > 1)
  946. pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  947. cosa->num, cosa->usage);
  948. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  949. pr_notice("%s: reset the card first (status %d)\n",
  950. cosa->name, cosa->firmware_status);
  951. return -EPERM;
  952. }
  953. if (copy_from_user(&d, arg, sizeof(d)))
  954. return -EFAULT;
  955. /* If something fails, force the user to reset the card */
  956. cosa->firmware_status &= ~COSA_FW_RESET;
  957. i = readmem(cosa, d.code, d.len, d.addr);
  958. if (i < 0) {
  959. pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
  960. return -EIO;
  961. }
  962. pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
  963. cosa->num, d.len, d.addr);
  964. cosa->firmware_status |= COSA_FW_RESET;
  965. return 0;
  966. }
  967. /* High-level function to start microcode. Calls startmicrocode(). */
  968. static inline int cosa_start(struct cosa_data *cosa, int address)
  969. {
  970. int i;
  971. if (cosa->usage > 1)
  972. pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  973. cosa->num, cosa->usage);
  974. if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
  975. != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
  976. pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
  977. cosa->name, cosa->firmware_status);
  978. return -EPERM;
  979. }
  980. cosa->firmware_status &= ~COSA_FW_RESET;
  981. if ((i=startmicrocode(cosa, address)) < 0) {
  982. pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
  983. cosa->num, address, i);
  984. return -EIO;
  985. }
  986. pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
  987. cosa->startaddr = address;
  988. cosa->firmware_status |= COSA_FW_START;
  989. return 0;
  990. }
  991. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  992. static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
  993. {
  994. int l = strlen(cosa->id_string)+1;
  995. if (copy_to_user(string, cosa->id_string, l))
  996. return -EFAULT;
  997. return l;
  998. }
  999. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  1000. static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
  1001. {
  1002. int l = strlen(cosa->type)+1;
  1003. if (copy_to_user(string, cosa->type, l))
  1004. return -EFAULT;
  1005. return l;
  1006. }
  1007. static int cosa_ioctl_common(struct cosa_data *cosa,
  1008. struct channel_data *channel, unsigned int cmd, unsigned long arg)
  1009. {
  1010. void __user *argp = (void __user *)arg;
  1011. switch (cmd) {
  1012. case COSAIORSET: /* Reset the device */
  1013. if (!capable(CAP_NET_ADMIN))
  1014. return -EACCES;
  1015. return cosa_reset(cosa);
  1016. case COSAIOSTRT: /* Start the firmware */
  1017. if (!capable(CAP_SYS_RAWIO))
  1018. return -EACCES;
  1019. return cosa_start(cosa, arg);
  1020. case COSAIODOWNLD: /* Download the firmware */
  1021. if (!capable(CAP_SYS_RAWIO))
  1022. return -EACCES;
  1023. return cosa_download(cosa, argp);
  1024. case COSAIORMEM:
  1025. if (!capable(CAP_SYS_RAWIO))
  1026. return -EACCES;
  1027. return cosa_readmem(cosa, argp);
  1028. case COSAIORTYPE:
  1029. return cosa_gettype(cosa, argp);
  1030. case COSAIORIDSTR:
  1031. return cosa_getidstr(cosa, argp);
  1032. case COSAIONRCARDS:
  1033. return nr_cards;
  1034. case COSAIONRCHANS:
  1035. return cosa->nchannels;
  1036. case COSAIOBMSET:
  1037. if (!capable(CAP_SYS_RAWIO))
  1038. return -EACCES;
  1039. if (is_8bit(cosa))
  1040. return -EINVAL;
  1041. if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
  1042. return -EINVAL;
  1043. cosa->busmaster = arg;
  1044. return 0;
  1045. case COSAIOBMGET:
  1046. return cosa->busmaster;
  1047. }
  1048. return -ENOIOCTLCMD;
  1049. }
  1050. static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1051. {
  1052. int rv;
  1053. struct channel_data *chan = dev_to_chan(dev);
  1054. rv = cosa_ioctl_common(chan->cosa, chan, cmd,
  1055. (unsigned long)ifr->ifr_data);
  1056. if (rv != -ENOIOCTLCMD)
  1057. return rv;
  1058. return hdlc_ioctl(dev, ifr, cmd);
  1059. }
  1060. static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
  1061. unsigned long arg)
  1062. {
  1063. struct channel_data *channel = file->private_data;
  1064. struct cosa_data *cosa;
  1065. long ret;
  1066. mutex_lock(&cosa_chardev_mutex);
  1067. cosa = channel->cosa;
  1068. ret = cosa_ioctl_common(cosa, channel, cmd, arg);
  1069. mutex_unlock(&cosa_chardev_mutex);
  1070. return ret;
  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. pr_info("cosa%dc%d: starting tx(0x%x)",
  1102. chan->cosa->num, chan->num, len);
  1103. for (i=0; i<len; i++)
  1104. pr_cont(" %02x", buf[i]&0xff);
  1105. pr_cont("\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. pr_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. pr_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. pr_info("0x%04x bytes remaining\n", length);
  1315. return -11;
  1316. }
  1317. c=i;
  1318. #if 1
  1319. if (put_user(c, microcode))
  1320. return -23; /* ??? */
  1321. #else
  1322. *microcode = c;
  1323. #endif
  1324. microcode++;
  1325. }
  1326. if (get_wait_data(cosa) != '\r') return -21;
  1327. if (get_wait_data(cosa) != '\n') return -22;
  1328. if (get_wait_data(cosa) != '.') return -23;
  1329. #if 0
  1330. printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
  1331. #endif
  1332. return 0;
  1333. }
  1334. /*
  1335. * This function resets the device and reads the initial prompt
  1336. * of the device's ROM monitor.
  1337. */
  1338. static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
  1339. {
  1340. int i=0, id=0, prev=0, curr=0;
  1341. /* Reset the card ... */
  1342. cosa_putstatus(cosa, 0);
  1343. cosa_getdata8(cosa);
  1344. cosa_putstatus(cosa, SR_RST);
  1345. msleep(500);
  1346. /* Disable all IRQs from the card */
  1347. cosa_putstatus(cosa, 0);
  1348. /*
  1349. * Try to read the ID string. The card then prints out the
  1350. * identification string ended by the "\n\x2e".
  1351. *
  1352. * The following loop is indexed through i (instead of id)
  1353. * to avoid looping forever when for any reason
  1354. * the port returns '\r', '\n' or '\x2e' permanently.
  1355. */
  1356. for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
  1357. if ((curr = get_wait_data(cosa)) == -1) {
  1358. return -1;
  1359. }
  1360. curr &= 0xff;
  1361. if (curr != '\r' && curr != '\n' && curr != 0x2e)
  1362. idstring[id++] = curr;
  1363. if (curr == 0x2e && prev == '\n')
  1364. break;
  1365. }
  1366. /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
  1367. idstring[id] = '\0';
  1368. return id;
  1369. }
  1370. /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
  1371. /*
  1372. * This routine gets the data byte from the card waiting for the SR_RX_RDY
  1373. * bit to be set in a loop. It should be used in the exceptional cases
  1374. * only (for example when resetting the card or downloading the firmware.
  1375. */
  1376. static int get_wait_data(struct cosa_data *cosa)
  1377. {
  1378. int retries = 1000;
  1379. while (--retries) {
  1380. /* read data and return them */
  1381. if (cosa_getstatus(cosa) & SR_RX_RDY) {
  1382. short r;
  1383. r = cosa_getdata8(cosa);
  1384. #if 0
  1385. pr_info("get_wait_data returning after %d retries\n",
  1386. 999-retries);
  1387. #endif
  1388. return r;
  1389. }
  1390. /* sleep if not ready to read */
  1391. schedule_timeout_interruptible(1);
  1392. }
  1393. pr_info("timeout in get_wait_data (status 0x%x)\n",
  1394. cosa_getstatus(cosa));
  1395. return -1;
  1396. }
  1397. /*
  1398. * This routine puts the data byte to the card waiting for the SR_TX_RDY
  1399. * bit to be set in a loop. It should be used in the exceptional cases
  1400. * only (for example when resetting the card or downloading the firmware).
  1401. */
  1402. static int put_wait_data(struct cosa_data *cosa, int data)
  1403. {
  1404. int retries = 1000;
  1405. while (--retries) {
  1406. /* read data and return them */
  1407. if (cosa_getstatus(cosa) & SR_TX_RDY) {
  1408. cosa_putdata8(cosa, data);
  1409. #if 0
  1410. pr_info("Putdata: %d retries\n", 999-retries);
  1411. #endif
  1412. return 0;
  1413. }
  1414. #if 0
  1415. /* sleep if not ready to read */
  1416. schedule_timeout_interruptible(1);
  1417. #endif
  1418. }
  1419. pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
  1420. cosa->num, cosa_getstatus(cosa));
  1421. return -1;
  1422. }
  1423. /*
  1424. * The following routine puts the hexadecimal number into the SRP monitor
  1425. * and verifies the proper echo of the sent bytes. Returns 0 on success,
  1426. * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
  1427. * (-2,-4,-6,-8) means that reading echo failed.
  1428. */
  1429. static int puthexnumber(struct cosa_data *cosa, int number)
  1430. {
  1431. char temp[5];
  1432. int i;
  1433. /* Well, I should probably replace this by something faster. */
  1434. sprintf(temp, "%04X", number);
  1435. for (i=0; i<4; i++) {
  1436. if (put_wait_data(cosa, temp[i]) == -1) {
  1437. pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
  1438. cosa->num, i);
  1439. return -1-2*i;
  1440. }
  1441. if (get_wait_data(cosa) != temp[i]) {
  1442. pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
  1443. cosa->num, i);
  1444. return -2-2*i;
  1445. }
  1446. }
  1447. return 0;
  1448. }
  1449. /* ---------- Interrupt routines ---------- */
  1450. /*
  1451. * There are three types of interrupt:
  1452. * At the beginning of transmit - this handled is in tx_interrupt(),
  1453. * at the beginning of receive - it is in rx_interrupt() and
  1454. * at the end of transmit/receive - it is the eot_interrupt() function.
  1455. * These functions are multiplexed by cosa_interrupt() according to the
  1456. * COSA status byte. I have moved the rx/tx/eot interrupt handling into
  1457. * separate functions to make it more readable. These functions are inline,
  1458. * so there should be no overhead of function call.
  1459. *
  1460. * In the COSA bus-master mode, we need to tell the card the address of a
  1461. * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
  1462. * It's time to use the bottom half :-(
  1463. */
  1464. /*
  1465. * Transmit interrupt routine - called when COSA is willing to obtain
  1466. * data from the OS. The most tricky part of the routine is selection
  1467. * of channel we (OS) want to send packet for. For SRP we should probably
  1468. * use the round-robin approach. The newer COSA firmwares have a simple
  1469. * flow-control - in the status word has bits 2 and 3 set to 1 means that the
  1470. * channel 0 or 1 doesn't want to receive data.
  1471. *
  1472. * It seems there is a bug in COSA firmware (need to trace it further):
  1473. * When the driver status says that the kernel has no more data for transmit
  1474. * (e.g. at the end of TX DMA) and then the kernel changes its mind
  1475. * (e.g. new packet is queued to hard_start_xmit()), the card issues
  1476. * the TX interrupt but does not mark the channel as ready-to-transmit.
  1477. * The fix seems to be to push the packet to COSA despite its request.
  1478. * We first try to obey the card's opinion, and then fall back to forced TX.
  1479. */
  1480. static inline void tx_interrupt(struct cosa_data *cosa, int status)
  1481. {
  1482. unsigned long flags, flags1;
  1483. #ifdef DEBUG_IRQS
  1484. pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
  1485. #endif
  1486. spin_lock_irqsave(&cosa->lock, flags);
  1487. set_bit(TXBIT, &cosa->rxtx);
  1488. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1489. /* flow control, see the comment above */
  1490. int i=0;
  1491. if (!cosa->txbitmap) {
  1492. pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
  1493. cosa->name);
  1494. put_driver_status_nolock(cosa);
  1495. clear_bit(TXBIT, &cosa->rxtx);
  1496. spin_unlock_irqrestore(&cosa->lock, flags);
  1497. return;
  1498. }
  1499. while (1) {
  1500. cosa->txchan++;
  1501. i++;
  1502. if (cosa->txchan >= cosa->nchannels)
  1503. cosa->txchan = 0;
  1504. if (!(cosa->txbitmap & (1<<cosa->txchan)))
  1505. continue;
  1506. if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
  1507. break;
  1508. /* in second pass, accept first ready-to-TX channel */
  1509. if (i > cosa->nchannels) {
  1510. /* Can be safely ignored */
  1511. #ifdef DEBUG_IRQS
  1512. printk(KERN_DEBUG "%s: Forcing TX "
  1513. "to not-ready channel %d\n",
  1514. cosa->name, cosa->txchan);
  1515. #endif
  1516. break;
  1517. }
  1518. }
  1519. cosa->txsize = cosa->chan[cosa->txchan].txsize;
  1520. if (cosa_dma_able(cosa->chan+cosa->txchan,
  1521. cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
  1522. cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
  1523. } else {
  1524. memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
  1525. cosa->txsize);
  1526. cosa->txbuf = cosa->bouncebuf;
  1527. }
  1528. }
  1529. if (is_8bit(cosa)) {
  1530. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1531. cosa_putstatus(cosa, SR_TX_INT_ENA);
  1532. cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
  1533. ((cosa->txsize >> 8) & 0x1f));
  1534. #ifdef DEBUG_IO
  1535. debug_status_out(cosa, SR_TX_INT_ENA);
  1536. debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
  1537. ((cosa->txsize >> 8) & 0x1f));
  1538. debug_data_in(cosa, cosa_getdata8(cosa));
  1539. #else
  1540. cosa_getdata8(cosa);
  1541. #endif
  1542. set_bit(IRQBIT, &cosa->rxtx);
  1543. spin_unlock_irqrestore(&cosa->lock, flags);
  1544. return;
  1545. } else {
  1546. clear_bit(IRQBIT, &cosa->rxtx);
  1547. cosa_putstatus(cosa, 0);
  1548. cosa_putdata8(cosa, cosa->txsize&0xff);
  1549. #ifdef DEBUG_IO
  1550. debug_status_out(cosa, 0);
  1551. debug_data_out(cosa, cosa->txsize&0xff);
  1552. #endif
  1553. }
  1554. } else {
  1555. cosa_putstatus(cosa, SR_TX_INT_ENA);
  1556. cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
  1557. | (cosa->txsize & 0x1fff));
  1558. #ifdef DEBUG_IO
  1559. debug_status_out(cosa, SR_TX_INT_ENA);
  1560. debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
  1561. | (cosa->txsize & 0x1fff));
  1562. debug_data_in(cosa, cosa_getdata8(cosa));
  1563. debug_status_out(cosa, 0);
  1564. #else
  1565. cosa_getdata8(cosa);
  1566. #endif
  1567. cosa_putstatus(cosa, 0);
  1568. }
  1569. if (cosa->busmaster) {
  1570. unsigned long addr = virt_to_bus(cosa->txbuf);
  1571. int count=0;
  1572. pr_info("busmaster IRQ\n");
  1573. while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
  1574. count++;
  1575. udelay(10);
  1576. if (count > 1000) break;
  1577. }
  1578. pr_info("status %x\n", cosa_getstatus(cosa));
  1579. pr_info("ready after %d loops\n", count);
  1580. cosa_putdata16(cosa, (addr >> 16)&0xffff);
  1581. count = 0;
  1582. while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
  1583. count++;
  1584. if (count > 1000) break;
  1585. udelay(10);
  1586. }
  1587. pr_info("ready after %d loops\n", count);
  1588. cosa_putdata16(cosa, addr &0xffff);
  1589. flags1 = claim_dma_lock();
  1590. set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
  1591. enable_dma(cosa->dma);
  1592. release_dma_lock(flags1);
  1593. } else {
  1594. /* start the DMA */
  1595. flags1 = claim_dma_lock();
  1596. disable_dma(cosa->dma);
  1597. clear_dma_ff(cosa->dma);
  1598. set_dma_mode(cosa->dma, DMA_MODE_WRITE);
  1599. set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
  1600. set_dma_count(cosa->dma, cosa->txsize);
  1601. enable_dma(cosa->dma);
  1602. release_dma_lock(flags1);
  1603. }
  1604. cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
  1605. #ifdef DEBUG_IO
  1606. debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
  1607. #endif
  1608. spin_unlock_irqrestore(&cosa->lock, flags);
  1609. }
  1610. static inline void rx_interrupt(struct cosa_data *cosa, int status)
  1611. {
  1612. unsigned long flags;
  1613. #ifdef DEBUG_IRQS
  1614. pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
  1615. #endif
  1616. spin_lock_irqsave(&cosa->lock, flags);
  1617. set_bit(RXBIT, &cosa->rxtx);
  1618. if (is_8bit(cosa)) {
  1619. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1620. set_bit(IRQBIT, &cosa->rxtx);
  1621. put_driver_status_nolock(cosa);
  1622. cosa->rxsize = cosa_getdata8(cosa) <<8;
  1623. #ifdef DEBUG_IO
  1624. debug_data_in(cosa, cosa->rxsize >> 8);
  1625. #endif
  1626. spin_unlock_irqrestore(&cosa->lock, flags);
  1627. return;
  1628. } else {
  1629. clear_bit(IRQBIT, &cosa->rxtx);
  1630. cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
  1631. #ifdef DEBUG_IO
  1632. debug_data_in(cosa, cosa->rxsize & 0xff);
  1633. #endif
  1634. #if 0
  1635. pr_info("cosa%d: receive rxsize = (0x%04x)\n",
  1636. cosa->num, cosa->rxsize);
  1637. #endif
  1638. }
  1639. } else {
  1640. cosa->rxsize = cosa_getdata16(cosa);
  1641. #ifdef DEBUG_IO
  1642. debug_data_in(cosa, cosa->rxsize);
  1643. #endif
  1644. #if 0
  1645. pr_info("cosa%d: receive rxsize = (0x%04x)\n",
  1646. cosa->num, cosa->rxsize);
  1647. #endif
  1648. }
  1649. if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
  1650. pr_warn("%s: rx for unknown channel (0x%04x)\n",
  1651. cosa->name, cosa->rxsize);
  1652. spin_unlock_irqrestore(&cosa->lock, flags);
  1653. goto reject;
  1654. }
  1655. cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
  1656. cosa->rxsize &= 0x1fff;
  1657. spin_unlock_irqrestore(&cosa->lock, flags);
  1658. cosa->rxbuf = NULL;
  1659. if (cosa->rxchan->setup_rx)
  1660. cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
  1661. if (!cosa->rxbuf) {
  1662. reject: /* Reject the packet */
  1663. pr_info("cosa%d: rejecting packet on channel %d\n",
  1664. cosa->num, cosa->rxchan->num);
  1665. cosa->rxbuf = cosa->bouncebuf;
  1666. }
  1667. /* start the DMA */
  1668. flags = claim_dma_lock();
  1669. disable_dma(cosa->dma);
  1670. clear_dma_ff(cosa->dma);
  1671. set_dma_mode(cosa->dma, DMA_MODE_READ);
  1672. if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
  1673. set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
  1674. } else {
  1675. set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
  1676. }
  1677. set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
  1678. enable_dma(cosa->dma);
  1679. release_dma_lock(flags);
  1680. spin_lock_irqsave(&cosa->lock, flags);
  1681. cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
  1682. if (!is_8bit(cosa) && (status & SR_TX_RDY))
  1683. cosa_putdata8(cosa, DRIVER_RX_READY);
  1684. #ifdef DEBUG_IO
  1685. debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
  1686. if (!is_8bit(cosa) && (status & SR_TX_RDY))
  1687. debug_data_cmd(cosa, DRIVER_RX_READY);
  1688. #endif
  1689. spin_unlock_irqrestore(&cosa->lock, flags);
  1690. }
  1691. static inline void eot_interrupt(struct cosa_data *cosa, int status)
  1692. {
  1693. unsigned long flags, flags1;
  1694. spin_lock_irqsave(&cosa->lock, flags);
  1695. flags1 = claim_dma_lock();
  1696. disable_dma(cosa->dma);
  1697. clear_dma_ff(cosa->dma);
  1698. release_dma_lock(flags1);
  1699. if (test_bit(TXBIT, &cosa->rxtx)) {
  1700. struct channel_data *chan = cosa->chan+cosa->txchan;
  1701. if (chan->tx_done)
  1702. if (chan->tx_done(chan, cosa->txsize))
  1703. clear_bit(chan->num, &cosa->txbitmap);
  1704. } else if (test_bit(RXBIT, &cosa->rxtx)) {
  1705. #ifdef DEBUG_DATA
  1706. {
  1707. int i;
  1708. pr_info("cosa%dc%d: done rx(0x%x)",
  1709. cosa->num, cosa->rxchan->num, cosa->rxsize);
  1710. for (i=0; i<cosa->rxsize; i++)
  1711. pr_cont(" %02x", cosa->rxbuf[i]&0xff);
  1712. pr_cont("\n");
  1713. }
  1714. #endif
  1715. /* Packet for unknown channel? */
  1716. if (cosa->rxbuf == cosa->bouncebuf)
  1717. goto out;
  1718. if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
  1719. memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
  1720. if (cosa->rxchan->rx_done)
  1721. if (cosa->rxchan->rx_done(cosa->rxchan))
  1722. clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
  1723. } else {
  1724. pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
  1725. }
  1726. /*
  1727. * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
  1728. * cleared anyway). We should do it as soon as possible
  1729. * so that we can tell the COSA we are done and to give it a time
  1730. * for recovery.
  1731. */
  1732. out:
  1733. cosa->rxtx = 0;
  1734. put_driver_status_nolock(cosa);
  1735. spin_unlock_irqrestore(&cosa->lock, flags);
  1736. }
  1737. static irqreturn_t cosa_interrupt(int irq, void *cosa_)
  1738. {
  1739. unsigned status;
  1740. int count = 0;
  1741. struct cosa_data *cosa = cosa_;
  1742. again:
  1743. status = cosa_getstatus(cosa);
  1744. #ifdef DEBUG_IRQS
  1745. pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
  1746. #endif
  1747. #ifdef DEBUG_IO
  1748. debug_status_in(cosa, status);
  1749. #endif
  1750. switch (status & SR_CMD_FROM_SRP_MASK) {
  1751. case SR_DOWN_REQUEST:
  1752. tx_interrupt(cosa, status);
  1753. break;
  1754. case SR_UP_REQUEST:
  1755. rx_interrupt(cosa, status);
  1756. break;
  1757. case SR_END_OF_TRANSFER:
  1758. eot_interrupt(cosa, status);
  1759. break;
  1760. default:
  1761. /* We may be too fast for SRP. Try to wait a bit more. */
  1762. if (count++ < 100) {
  1763. udelay(100);
  1764. goto again;
  1765. }
  1766. pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
  1767. cosa->num, status & 0xff, count);
  1768. }
  1769. #ifdef DEBUG_IRQS
  1770. if (count)
  1771. pr_info("%s: %d-times got unknown status in IRQ\n",
  1772. cosa->name, count);
  1773. else
  1774. pr_info("%s: returning from IRQ\n", cosa->name);
  1775. #endif
  1776. return IRQ_HANDLED;
  1777. }
  1778. /* ---------- I/O debugging routines ---------- */
  1779. /*
  1780. * These routines can be used to monitor COSA/SRP I/O and to printk()
  1781. * the data being transferred on the data and status I/O port in a
  1782. * readable way.
  1783. */
  1784. #ifdef DEBUG_IO
  1785. static void debug_status_in(struct cosa_data *cosa, int status)
  1786. {
  1787. char *s;
  1788. switch (status & SR_CMD_FROM_SRP_MASK) {
  1789. case SR_UP_REQUEST:
  1790. s = "RX_REQ";
  1791. break;
  1792. case SR_DOWN_REQUEST:
  1793. s = "TX_REQ";
  1794. break;
  1795. case SR_END_OF_TRANSFER:
  1796. s = "ET_REQ";
  1797. break;
  1798. default:
  1799. s = "NO_REQ";
  1800. break;
  1801. }
  1802. pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
  1803. cosa->name,
  1804. status,
  1805. status & SR_USR_RQ ? "USR_RQ|" : "",
  1806. status & SR_TX_RDY ? "TX_RDY|" : "",
  1807. status & SR_RX_RDY ? "RX_RDY|" : "",
  1808. s);
  1809. }
  1810. static void debug_status_out(struct cosa_data *cosa, int status)
  1811. {
  1812. pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
  1813. cosa->name,
  1814. status,
  1815. status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
  1816. status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
  1817. status & SR_RST ? "RESET|" : "",
  1818. status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
  1819. status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
  1820. status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
  1821. }
  1822. static void debug_data_in(struct cosa_data *cosa, int data)
  1823. {
  1824. pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
  1825. }
  1826. static void debug_data_out(struct cosa_data *cosa, int data)
  1827. {
  1828. pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
  1829. }
  1830. static void debug_data_cmd(struct cosa_data *cosa, int data)
  1831. {
  1832. pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
  1833. cosa->name, data,
  1834. data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
  1835. data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
  1836. }
  1837. #endif
  1838. /* EOF -- this file has not been truncated */