cosa.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  1. /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
  2. /*
  3. * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
  4. * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. * The driver for the SRP and COSA synchronous serial cards.
  22. *
  23. * HARDWARE INFO
  24. *
  25. * Both cards are developed at the Institute of Computer Science,
  26. * Masaryk University (http://www.ics.muni.cz/). The hardware is
  27. * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
  28. * and the photo of both cards is available at
  29. * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
  30. * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
  31. * For Linux-specific utilities, see below in the "Software info" section.
  32. * If you want to order the card, contact Jiri Novotny.
  33. *
  34. * The SRP (serial port?, the Czech word "srp" means "sickle") card
  35. * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
  36. * with V.24 interfaces up to 80kb/s each.
  37. *
  38. * The COSA (communication serial adapter?, the Czech word "kosa" means
  39. * "scythe") is a next-generation sync/async board with two interfaces
  40. * - currently any of V.24, X.21, V.35 and V.36 can be selected.
  41. * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
  42. * The 8-channels version is in development.
  43. *
  44. * Both types have downloadable firmware and communicate via ISA DMA.
  45. * COSA can be also a bus-mastering device.
  46. *
  47. * SOFTWARE INFO
  48. *
  49. * The homepage of the Linux driver is at http://www.fi.muni.cz/~kas/cosa/.
  50. * The CVS tree of Linux driver can be viewed there, as well as the
  51. * firmware binaries and user-space utilities for downloading the firmware
  52. * into the card and setting up the card.
  53. *
  54. * The Linux driver (unlike the present *BSD drivers :-) can work even
  55. * for the COSA and SRP in one computer and allows each channel to work
  56. * in one of the two modes (character or network device).
  57. *
  58. * AUTHOR
  59. *
  60. * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
  61. *
  62. * You can mail me bugfixes and even success reports. I am especially
  63. * interested in the SMP and/or muliti-channel success/failure reports
  64. * (I wonder if I did the locking properly :-).
  65. *
  66. * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
  67. *
  68. * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
  69. * The skeleton.c by Donald Becker
  70. * The SDL Riscom/N2 driver by Mike Natale
  71. * The Comtrol Hostess SV11 driver by Alan Cox
  72. * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
  73. */
  74. #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(30);
  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. goto err_hdlcdev;
  516. }
  517. dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
  518. dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
  519. chan->netdev->netdev_ops = &cosa_ops;
  520. chan->netdev->watchdog_timeo = TX_TIMEOUT;
  521. chan->netdev->base_addr = chan->cosa->datareg;
  522. chan->netdev->irq = chan->cosa->irq;
  523. chan->netdev->dma = chan->cosa->dma;
  524. if (register_hdlc_device(chan->netdev)) {
  525. netdev_warn(chan->netdev,
  526. "register_hdlc_device() failed\n");
  527. free_netdev(chan->netdev);
  528. goto err_hdlcdev;
  529. }
  530. }
  531. pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
  532. cosa->num, cosa->id_string, cosa->type,
  533. cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
  534. return nr_cards++;
  535. err_hdlcdev:
  536. while (i-- > 0) {
  537. unregister_hdlc_device(cosa->chan[i].netdev);
  538. free_netdev(cosa->chan[i].netdev);
  539. }
  540. kfree(cosa->chan);
  541. err_out3:
  542. kfree(cosa->bouncebuf);
  543. err_out2:
  544. free_dma(cosa->dma);
  545. err_out1:
  546. free_irq(cosa->irq, cosa);
  547. err_out:
  548. release_region(cosa->datareg,is_8bit(cosa)?2:4);
  549. pr_notice("cosa%d: allocating resources failed\n", cosa->num);
  550. return err;
  551. }
  552. /*---------- network device ---------- */
  553. static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
  554. unsigned short parity)
  555. {
  556. if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
  557. return 0;
  558. return -EINVAL;
  559. }
  560. static int cosa_net_open(struct net_device *dev)
  561. {
  562. struct channel_data *chan = dev_to_chan(dev);
  563. int err;
  564. unsigned long flags;
  565. if (!(chan->cosa->firmware_status & COSA_FW_START)) {
  566. pr_notice("%s: start the firmware first (status %d)\n",
  567. chan->cosa->name, chan->cosa->firmware_status);
  568. return -EPERM;
  569. }
  570. spin_lock_irqsave(&chan->cosa->lock, flags);
  571. if (chan->usage != 0) {
  572. pr_warn("%s: cosa_net_open called with usage count %d\n",
  573. chan->name, chan->usage);
  574. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  575. return -EBUSY;
  576. }
  577. chan->setup_rx = cosa_net_setup_rx;
  578. chan->tx_done = cosa_net_tx_done;
  579. chan->rx_done = cosa_net_rx_done;
  580. chan->usage = -1;
  581. chan->cosa->usage++;
  582. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  583. err = hdlc_open(dev);
  584. if (err) {
  585. spin_lock_irqsave(&chan->cosa->lock, flags);
  586. chan->usage = 0;
  587. chan->cosa->usage--;
  588. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  589. return err;
  590. }
  591. netif_start_queue(dev);
  592. cosa_enable_rx(chan);
  593. return 0;
  594. }
  595. static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
  596. struct net_device *dev)
  597. {
  598. struct channel_data *chan = dev_to_chan(dev);
  599. netif_stop_queue(dev);
  600. chan->tx_skb = skb;
  601. cosa_start_tx(chan, skb->data, skb->len);
  602. return NETDEV_TX_OK;
  603. }
  604. static void cosa_net_timeout(struct net_device *dev)
  605. {
  606. struct channel_data *chan = dev_to_chan(dev);
  607. if (test_bit(RXBIT, &chan->cosa->rxtx)) {
  608. chan->netdev->stats.rx_errors++;
  609. chan->netdev->stats.rx_missed_errors++;
  610. } else {
  611. chan->netdev->stats.tx_errors++;
  612. chan->netdev->stats.tx_aborted_errors++;
  613. }
  614. cosa_kick(chan->cosa);
  615. if (chan->tx_skb) {
  616. dev_kfree_skb(chan->tx_skb);
  617. chan->tx_skb = NULL;
  618. }
  619. netif_wake_queue(dev);
  620. }
  621. static int cosa_net_close(struct net_device *dev)
  622. {
  623. struct channel_data *chan = dev_to_chan(dev);
  624. unsigned long flags;
  625. netif_stop_queue(dev);
  626. hdlc_close(dev);
  627. cosa_disable_rx(chan);
  628. spin_lock_irqsave(&chan->cosa->lock, flags);
  629. if (chan->rx_skb) {
  630. kfree_skb(chan->rx_skb);
  631. chan->rx_skb = NULL;
  632. }
  633. if (chan->tx_skb) {
  634. kfree_skb(chan->tx_skb);
  635. chan->tx_skb = NULL;
  636. }
  637. chan->usage = 0;
  638. chan->cosa->usage--;
  639. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  640. return 0;
  641. }
  642. static char *cosa_net_setup_rx(struct channel_data *chan, int size)
  643. {
  644. /*
  645. * We can safely fall back to non-dma-able memory, because we have
  646. * the cosa->bouncebuf pre-allocated.
  647. */
  648. kfree_skb(chan->rx_skb);
  649. chan->rx_skb = dev_alloc_skb(size);
  650. if (chan->rx_skb == NULL) {
  651. pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
  652. chan->netdev->stats.rx_dropped++;
  653. return NULL;
  654. }
  655. chan->netdev->trans_start = jiffies;
  656. return skb_put(chan->rx_skb, size);
  657. }
  658. static int cosa_net_rx_done(struct channel_data *chan)
  659. {
  660. if (!chan->rx_skb) {
  661. pr_warn("%s: rx_done with empty skb!\n", chan->name);
  662. chan->netdev->stats.rx_errors++;
  663. chan->netdev->stats.rx_frame_errors++;
  664. return 0;
  665. }
  666. chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
  667. chan->rx_skb->dev = chan->netdev;
  668. skb_reset_mac_header(chan->rx_skb);
  669. chan->netdev->stats.rx_packets++;
  670. chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
  671. netif_rx(chan->rx_skb);
  672. chan->rx_skb = NULL;
  673. return 0;
  674. }
  675. /* ARGSUSED */
  676. static int cosa_net_tx_done(struct channel_data *chan, int size)
  677. {
  678. if (!chan->tx_skb) {
  679. pr_warn("%s: tx_done with empty skb!\n", chan->name);
  680. chan->netdev->stats.tx_errors++;
  681. chan->netdev->stats.tx_aborted_errors++;
  682. return 1;
  683. }
  684. dev_kfree_skb_irq(chan->tx_skb);
  685. chan->tx_skb = NULL;
  686. chan->netdev->stats.tx_packets++;
  687. chan->netdev->stats.tx_bytes += size;
  688. netif_wake_queue(chan->netdev);
  689. return 1;
  690. }
  691. /*---------- Character device ---------- */
  692. static ssize_t cosa_read(struct file *file,
  693. char __user *buf, size_t count, loff_t *ppos)
  694. {
  695. DECLARE_WAITQUEUE(wait, current);
  696. unsigned long flags;
  697. struct channel_data *chan = file->private_data;
  698. struct cosa_data *cosa = chan->cosa;
  699. char *kbuf;
  700. if (!(cosa->firmware_status & COSA_FW_START)) {
  701. pr_notice("%s: start the firmware first (status %d)\n",
  702. cosa->name, cosa->firmware_status);
  703. return -EPERM;
  704. }
  705. if (mutex_lock_interruptible(&chan->rlock))
  706. return -ERESTARTSYS;
  707. if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
  708. pr_info("%s: cosa_read() - OOM\n", cosa->name);
  709. mutex_unlock(&chan->rlock);
  710. return -ENOMEM;
  711. }
  712. chan->rx_status = 0;
  713. cosa_enable_rx(chan);
  714. spin_lock_irqsave(&cosa->lock, flags);
  715. add_wait_queue(&chan->rxwaitq, &wait);
  716. while (!chan->rx_status) {
  717. current->state = TASK_INTERRUPTIBLE;
  718. spin_unlock_irqrestore(&cosa->lock, flags);
  719. schedule();
  720. spin_lock_irqsave(&cosa->lock, flags);
  721. if (signal_pending(current) && chan->rx_status == 0) {
  722. chan->rx_status = 1;
  723. remove_wait_queue(&chan->rxwaitq, &wait);
  724. current->state = TASK_RUNNING;
  725. spin_unlock_irqrestore(&cosa->lock, flags);
  726. mutex_unlock(&chan->rlock);
  727. return -ERESTARTSYS;
  728. }
  729. }
  730. remove_wait_queue(&chan->rxwaitq, &wait);
  731. current->state = TASK_RUNNING;
  732. kbuf = chan->rxdata;
  733. count = chan->rxsize;
  734. spin_unlock_irqrestore(&cosa->lock, flags);
  735. mutex_unlock(&chan->rlock);
  736. if (copy_to_user(buf, kbuf, count)) {
  737. kfree(kbuf);
  738. return -EFAULT;
  739. }
  740. kfree(kbuf);
  741. return count;
  742. }
  743. static char *chrdev_setup_rx(struct channel_data *chan, int size)
  744. {
  745. /* Expect size <= COSA_MTU */
  746. chan->rxsize = size;
  747. return chan->rxdata;
  748. }
  749. static int chrdev_rx_done(struct channel_data *chan)
  750. {
  751. if (chan->rx_status) { /* Reader has died */
  752. kfree(chan->rxdata);
  753. up(&chan->wsem);
  754. }
  755. chan->rx_status = 1;
  756. wake_up_interruptible(&chan->rxwaitq);
  757. return 1;
  758. }
  759. static ssize_t cosa_write(struct file *file,
  760. const char __user *buf, size_t count, loff_t *ppos)
  761. {
  762. DECLARE_WAITQUEUE(wait, current);
  763. struct channel_data *chan = file->private_data;
  764. struct cosa_data *cosa = chan->cosa;
  765. unsigned long flags;
  766. char *kbuf;
  767. if (!(cosa->firmware_status & COSA_FW_START)) {
  768. pr_notice("%s: start the firmware first (status %d)\n",
  769. cosa->name, cosa->firmware_status);
  770. return -EPERM;
  771. }
  772. if (down_interruptible(&chan->wsem))
  773. return -ERESTARTSYS;
  774. if (count > COSA_MTU)
  775. count = COSA_MTU;
  776. /* Allocate the buffer */
  777. if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
  778. pr_notice("%s: cosa_write() OOM - dropping packet\n",
  779. cosa->name);
  780. up(&chan->wsem);
  781. return -ENOMEM;
  782. }
  783. if (copy_from_user(kbuf, buf, count)) {
  784. up(&chan->wsem);
  785. kfree(kbuf);
  786. return -EFAULT;
  787. }
  788. chan->tx_status=0;
  789. cosa_start_tx(chan, kbuf, count);
  790. spin_lock_irqsave(&cosa->lock, flags);
  791. add_wait_queue(&chan->txwaitq, &wait);
  792. while (!chan->tx_status) {
  793. current->state = TASK_INTERRUPTIBLE;
  794. spin_unlock_irqrestore(&cosa->lock, flags);
  795. schedule();
  796. spin_lock_irqsave(&cosa->lock, flags);
  797. if (signal_pending(current) && chan->tx_status == 0) {
  798. chan->tx_status = 1;
  799. remove_wait_queue(&chan->txwaitq, &wait);
  800. current->state = TASK_RUNNING;
  801. chan->tx_status = 1;
  802. spin_unlock_irqrestore(&cosa->lock, flags);
  803. up(&chan->wsem);
  804. return -ERESTARTSYS;
  805. }
  806. }
  807. remove_wait_queue(&chan->txwaitq, &wait);
  808. current->state = TASK_RUNNING;
  809. up(&chan->wsem);
  810. spin_unlock_irqrestore(&cosa->lock, flags);
  811. kfree(kbuf);
  812. return count;
  813. }
  814. static int chrdev_tx_done(struct channel_data *chan, int size)
  815. {
  816. if (chan->tx_status) { /* Writer was interrupted */
  817. kfree(chan->txbuf);
  818. up(&chan->wsem);
  819. }
  820. chan->tx_status = 1;
  821. wake_up_interruptible(&chan->txwaitq);
  822. return 1;
  823. }
  824. static unsigned int cosa_poll(struct file *file, poll_table *poll)
  825. {
  826. pr_info("cosa_poll is here\n");
  827. return 0;
  828. }
  829. static int cosa_open(struct inode *inode, struct file *file)
  830. {
  831. struct cosa_data *cosa;
  832. struct channel_data *chan;
  833. unsigned long flags;
  834. int n;
  835. int ret = 0;
  836. mutex_lock(&cosa_chardev_mutex);
  837. if ((n=iminor(file->f_path.dentry->d_inode)>>CARD_MINOR_BITS)
  838. >= nr_cards) {
  839. ret = -ENODEV;
  840. goto out;
  841. }
  842. cosa = cosa_cards+n;
  843. if ((n=iminor(file->f_path.dentry->d_inode)
  844. & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
  845. ret = -ENODEV;
  846. goto out;
  847. }
  848. chan = cosa->chan + n;
  849. file->private_data = chan;
  850. spin_lock_irqsave(&cosa->lock, flags);
  851. if (chan->usage < 0) { /* in netdev mode */
  852. spin_unlock_irqrestore(&cosa->lock, flags);
  853. ret = -EBUSY;
  854. goto out;
  855. }
  856. cosa->usage++;
  857. chan->usage++;
  858. chan->tx_done = chrdev_tx_done;
  859. chan->setup_rx = chrdev_setup_rx;
  860. chan->rx_done = chrdev_rx_done;
  861. spin_unlock_irqrestore(&cosa->lock, flags);
  862. out:
  863. mutex_unlock(&cosa_chardev_mutex);
  864. return ret;
  865. }
  866. static int cosa_release(struct inode *inode, struct file *file)
  867. {
  868. struct channel_data *channel = file->private_data;
  869. struct cosa_data *cosa;
  870. unsigned long flags;
  871. cosa = channel->cosa;
  872. spin_lock_irqsave(&cosa->lock, flags);
  873. cosa->usage--;
  874. channel->usage--;
  875. spin_unlock_irqrestore(&cosa->lock, flags);
  876. return 0;
  877. }
  878. #ifdef COSA_FASYNC_WORKING
  879. static struct fasync_struct *fasync[256] = { NULL, };
  880. /* To be done ... */
  881. static int cosa_fasync(struct inode *inode, struct file *file, int on)
  882. {
  883. int port = iminor(inode);
  884. return fasync_helper(inode, file, on, &fasync[port]);
  885. }
  886. #endif
  887. /* ---------- Ioctls ---------- */
  888. /*
  889. * Ioctl subroutines can safely be made inline, because they are called
  890. * only from cosa_ioctl().
  891. */
  892. static inline int cosa_reset(struct cosa_data *cosa)
  893. {
  894. char idstring[COSA_MAX_ID_STRING];
  895. if (cosa->usage > 1)
  896. pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  897. cosa->num, cosa->usage);
  898. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
  899. if (cosa_reset_and_read_id(cosa, idstring) < 0) {
  900. pr_notice("cosa%d: reset failed\n", cosa->num);
  901. return -EIO;
  902. }
  903. pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
  904. cosa->firmware_status |= COSA_FW_RESET;
  905. return 0;
  906. }
  907. /* High-level function to download data into COSA memory. Calls download() */
  908. static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
  909. {
  910. struct cosa_download d;
  911. int i;
  912. if (cosa->usage > 1)
  913. pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  914. cosa->name, cosa->usage);
  915. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  916. pr_notice("%s: reset the card first (status %d)\n",
  917. cosa->name, cosa->firmware_status);
  918. return -EPERM;
  919. }
  920. if (copy_from_user(&d, arg, sizeof(d)))
  921. return -EFAULT;
  922. if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
  923. return -EINVAL;
  924. if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
  925. return -EINVAL;
  926. /* If something fails, force the user to reset the card */
  927. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
  928. i = download(cosa, d.code, d.len, d.addr);
  929. if (i < 0) {
  930. pr_notice("cosa%d: microcode download failed: %d\n",
  931. cosa->num, i);
  932. return -EIO;
  933. }
  934. pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
  935. cosa->num, d.len, d.addr);
  936. cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
  937. return 0;
  938. }
  939. /* High-level function to read COSA memory. Calls readmem() */
  940. static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
  941. {
  942. struct cosa_download d;
  943. int i;
  944. if (cosa->usage > 1)
  945. pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  946. cosa->num, cosa->usage);
  947. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  948. pr_notice("%s: reset the card first (status %d)\n",
  949. cosa->name, cosa->firmware_status);
  950. return -EPERM;
  951. }
  952. if (copy_from_user(&d, arg, sizeof(d)))
  953. return -EFAULT;
  954. /* If something fails, force the user to reset the card */
  955. cosa->firmware_status &= ~COSA_FW_RESET;
  956. i = readmem(cosa, d.code, d.len, d.addr);
  957. if (i < 0) {
  958. pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
  959. return -EIO;
  960. }
  961. pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
  962. cosa->num, d.len, d.addr);
  963. cosa->firmware_status |= COSA_FW_RESET;
  964. return 0;
  965. }
  966. /* High-level function to start microcode. Calls startmicrocode(). */
  967. static inline int cosa_start(struct cosa_data *cosa, int address)
  968. {
  969. int i;
  970. if (cosa->usage > 1)
  971. pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  972. cosa->num, cosa->usage);
  973. if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
  974. != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
  975. pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
  976. cosa->name, cosa->firmware_status);
  977. return -EPERM;
  978. }
  979. cosa->firmware_status &= ~COSA_FW_RESET;
  980. if ((i=startmicrocode(cosa, address)) < 0) {
  981. pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
  982. cosa->num, address, i);
  983. return -EIO;
  984. }
  985. pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
  986. cosa->startaddr = address;
  987. cosa->firmware_status |= COSA_FW_START;
  988. return 0;
  989. }
  990. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  991. static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
  992. {
  993. int l = strlen(cosa->id_string)+1;
  994. if (copy_to_user(string, cosa->id_string, l))
  995. return -EFAULT;
  996. return l;
  997. }
  998. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  999. static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
  1000. {
  1001. int l = strlen(cosa->type)+1;
  1002. if (copy_to_user(string, cosa->type, l))
  1003. return -EFAULT;
  1004. return l;
  1005. }
  1006. static int cosa_ioctl_common(struct cosa_data *cosa,
  1007. struct channel_data *channel, unsigned int cmd, unsigned long arg)
  1008. {
  1009. void __user *argp = (void __user *)arg;
  1010. switch (cmd) {
  1011. case COSAIORSET: /* Reset the device */
  1012. if (!capable(CAP_NET_ADMIN))
  1013. return -EACCES;
  1014. return cosa_reset(cosa);
  1015. case COSAIOSTRT: /* Start the firmware */
  1016. if (!capable(CAP_SYS_RAWIO))
  1017. return -EACCES;
  1018. return cosa_start(cosa, arg);
  1019. case COSAIODOWNLD: /* Download the firmware */
  1020. if (!capable(CAP_SYS_RAWIO))
  1021. return -EACCES;
  1022. return cosa_download(cosa, argp);
  1023. case COSAIORMEM:
  1024. if (!capable(CAP_SYS_RAWIO))
  1025. return -EACCES;
  1026. return cosa_readmem(cosa, argp);
  1027. case COSAIORTYPE:
  1028. return cosa_gettype(cosa, argp);
  1029. case COSAIORIDSTR:
  1030. return cosa_getidstr(cosa, argp);
  1031. case COSAIONRCARDS:
  1032. return nr_cards;
  1033. case COSAIONRCHANS:
  1034. return cosa->nchannels;
  1035. case COSAIOBMSET:
  1036. if (!capable(CAP_SYS_RAWIO))
  1037. return -EACCES;
  1038. if (is_8bit(cosa))
  1039. return -EINVAL;
  1040. if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
  1041. return -EINVAL;
  1042. cosa->busmaster = arg;
  1043. return 0;
  1044. case COSAIOBMGET:
  1045. return cosa->busmaster;
  1046. }
  1047. return -ENOIOCTLCMD;
  1048. }
  1049. static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1050. {
  1051. int rv;
  1052. struct channel_data *chan = dev_to_chan(dev);
  1053. rv = cosa_ioctl_common(chan->cosa, chan, cmd,
  1054. (unsigned long)ifr->ifr_data);
  1055. if (rv != -ENOIOCTLCMD)
  1056. return rv;
  1057. return hdlc_ioctl(dev, ifr, cmd);
  1058. }
  1059. static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
  1060. unsigned long arg)
  1061. {
  1062. struct channel_data *channel = file->private_data;
  1063. struct cosa_data *cosa;
  1064. long ret;
  1065. mutex_lock(&cosa_chardev_mutex);
  1066. cosa = channel->cosa;
  1067. ret = cosa_ioctl_common(cosa, channel, cmd, arg);
  1068. mutex_unlock(&cosa_chardev_mutex);
  1069. return ret;
  1070. }
  1071. /*---------- HW layer interface ---------- */
  1072. /*
  1073. * The higher layer can bind itself to the HW layer by setting the callbacks
  1074. * in the channel_data structure and by using these routines.
  1075. */
  1076. static void cosa_enable_rx(struct channel_data *chan)
  1077. {
  1078. struct cosa_data *cosa = chan->cosa;
  1079. if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
  1080. put_driver_status(cosa);
  1081. }
  1082. static void cosa_disable_rx(struct channel_data *chan)
  1083. {
  1084. struct cosa_data *cosa = chan->cosa;
  1085. if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
  1086. put_driver_status(cosa);
  1087. }
  1088. /*
  1089. * FIXME: This routine probably should check for cosa_start_tx() called when
  1090. * the previous transmit is still unfinished. In this case the non-zero
  1091. * return value should indicate to the caller that the queuing(sp?) up
  1092. * the transmit has failed.
  1093. */
  1094. static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
  1095. {
  1096. struct cosa_data *cosa = chan->cosa;
  1097. unsigned long flags;
  1098. #ifdef DEBUG_DATA
  1099. int i;
  1100. pr_info("cosa%dc%d: starting tx(0x%x)",
  1101. chan->cosa->num, chan->num, len);
  1102. for (i=0; i<len; i++)
  1103. pr_cont(" %02x", buf[i]&0xff);
  1104. pr_cont("\n");
  1105. #endif
  1106. spin_lock_irqsave(&cosa->lock, flags);
  1107. chan->txbuf = buf;
  1108. chan->txsize = len;
  1109. if (len > COSA_MTU)
  1110. chan->txsize = COSA_MTU;
  1111. spin_unlock_irqrestore(&cosa->lock, flags);
  1112. /* Tell the firmware we are ready */
  1113. set_bit(chan->num, &cosa->txbitmap);
  1114. put_driver_status(cosa);
  1115. return 0;
  1116. }
  1117. static void put_driver_status(struct cosa_data *cosa)
  1118. {
  1119. unsigned long flags;
  1120. int status;
  1121. spin_lock_irqsave(&cosa->lock, flags);
  1122. status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
  1123. | (cosa->txbitmap ? DRIVER_TX_READY : 0)
  1124. | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
  1125. &DRIVER_TXMAP_MASK : 0);
  1126. if (!cosa->rxtx) {
  1127. if (cosa->rxbitmap|cosa->txbitmap) {
  1128. if (!cosa->enabled) {
  1129. cosa_putstatus(cosa, SR_RX_INT_ENA);
  1130. #ifdef DEBUG_IO
  1131. debug_status_out(cosa, SR_RX_INT_ENA);
  1132. #endif
  1133. cosa->enabled = 1;
  1134. }
  1135. } else if (cosa->enabled) {
  1136. cosa->enabled = 0;
  1137. cosa_putstatus(cosa, 0);
  1138. #ifdef DEBUG_IO
  1139. debug_status_out(cosa, 0);
  1140. #endif
  1141. }
  1142. cosa_putdata8(cosa, status);
  1143. #ifdef DEBUG_IO
  1144. debug_data_cmd(cosa, status);
  1145. #endif
  1146. }
  1147. spin_unlock_irqrestore(&cosa->lock, flags);
  1148. }
  1149. static void put_driver_status_nolock(struct cosa_data *cosa)
  1150. {
  1151. int status;
  1152. status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
  1153. | (cosa->txbitmap ? DRIVER_TX_READY : 0)
  1154. | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
  1155. &DRIVER_TXMAP_MASK : 0);
  1156. if (cosa->rxbitmap|cosa->txbitmap) {
  1157. cosa_putstatus(cosa, SR_RX_INT_ENA);
  1158. #ifdef DEBUG_IO
  1159. debug_status_out(cosa, SR_RX_INT_ENA);
  1160. #endif
  1161. cosa->enabled = 1;
  1162. } else {
  1163. cosa_putstatus(cosa, 0);
  1164. #ifdef DEBUG_IO
  1165. debug_status_out(cosa, 0);
  1166. #endif
  1167. cosa->enabled = 0;
  1168. }
  1169. cosa_putdata8(cosa, status);
  1170. #ifdef DEBUG_IO
  1171. debug_data_cmd(cosa, status);
  1172. #endif
  1173. }
  1174. /*
  1175. * The "kickme" function: When the DMA times out, this is called to
  1176. * clean up the driver status.
  1177. * FIXME: Preliminary support, the interface is probably wrong.
  1178. */
  1179. static void cosa_kick(struct cosa_data *cosa)
  1180. {
  1181. unsigned long flags, flags1;
  1182. char *s = "(probably) IRQ";
  1183. if (test_bit(RXBIT, &cosa->rxtx))
  1184. s = "RX DMA";
  1185. if (test_bit(TXBIT, &cosa->rxtx))
  1186. s = "TX DMA";
  1187. pr_info("%s: %s timeout - restarting\n", cosa->name, s);
  1188. spin_lock_irqsave(&cosa->lock, flags);
  1189. cosa->rxtx = 0;
  1190. flags1 = claim_dma_lock();
  1191. disable_dma(cosa->dma);
  1192. clear_dma_ff(cosa->dma);
  1193. release_dma_lock(flags1);
  1194. /* FIXME: Anything else? */
  1195. udelay(100);
  1196. cosa_putstatus(cosa, 0);
  1197. udelay(100);
  1198. (void) cosa_getdata8(cosa);
  1199. udelay(100);
  1200. cosa_putdata8(cosa, 0);
  1201. udelay(100);
  1202. put_driver_status_nolock(cosa);
  1203. spin_unlock_irqrestore(&cosa->lock, flags);
  1204. }
  1205. /*
  1206. * Check if the whole buffer is DMA-able. It means it is below the 16M of
  1207. * physical memory and doesn't span the 64k boundary. For now it seems
  1208. * SKB's never do this, but we'll check this anyway.
  1209. */
  1210. static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
  1211. {
  1212. static int count;
  1213. unsigned long b = (unsigned long)buf;
  1214. if (b+len >= MAX_DMA_ADDRESS)
  1215. return 0;
  1216. if ((b^ (b+len)) & 0x10000) {
  1217. if (count++ < 5)
  1218. pr_info("%s: packet spanning a 64k boundary\n",
  1219. chan->name);
  1220. return 0;
  1221. }
  1222. return 1;
  1223. }
  1224. /* ---------- The SRP/COSA ROM monitor functions ---------- */
  1225. /*
  1226. * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
  1227. * drivers need to say 4-digit hex number meaning start address of the microcode
  1228. * separated by a single space. Monitor replies by saying " =". Now driver
  1229. * has to write 4-digit hex number meaning the last byte address ended
  1230. * by a single space. Monitor has to reply with a space. Now the download
  1231. * begins. After the download monitor replies with "\r\n." (CR LF dot).
  1232. */
  1233. static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
  1234. {
  1235. int i;
  1236. if (put_wait_data(cosa, 'w') == -1) return -1;
  1237. if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
  1238. if (get_wait_data(cosa) != '=') return -3;
  1239. if (puthexnumber(cosa, address) < 0) return -4;
  1240. if (put_wait_data(cosa, ' ') == -1) return -10;
  1241. if (get_wait_data(cosa) != ' ') return -11;
  1242. if (get_wait_data(cosa) != '=') return -12;
  1243. if (puthexnumber(cosa, address+length-1) < 0) return -13;
  1244. if (put_wait_data(cosa, ' ') == -1) return -18;
  1245. if (get_wait_data(cosa) != ' ') return -19;
  1246. while (length--) {
  1247. char c;
  1248. #ifndef SRP_DOWNLOAD_AT_BOOT
  1249. if (get_user(c, microcode))
  1250. return -23; /* ??? */
  1251. #else
  1252. c = *microcode;
  1253. #endif
  1254. if (put_wait_data(cosa, c) == -1)
  1255. return -20;
  1256. microcode++;
  1257. }
  1258. if (get_wait_data(cosa) != '\r') return -21;
  1259. if (get_wait_data(cosa) != '\n') return -22;
  1260. if (get_wait_data(cosa) != '.') return -23;
  1261. #if 0
  1262. printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
  1263. #endif
  1264. return 0;
  1265. }
  1266. /*
  1267. * Starting microcode is done via the "g" command of the SRP monitor.
  1268. * The chat should be the following: "g" "g=" "<addr><CR>"
  1269. * "<CR><CR><LF><CR><LF>".
  1270. */
  1271. static int startmicrocode(struct cosa_data *cosa, int address)
  1272. {
  1273. if (put_wait_data(cosa, 'g') == -1) return -1;
  1274. if (get_wait_data(cosa) != 'g') return -2;
  1275. if (get_wait_data(cosa) != '=') return -3;
  1276. if (puthexnumber(cosa, address) < 0) return -4;
  1277. if (put_wait_data(cosa, '\r') == -1) return -5;
  1278. if (get_wait_data(cosa) != '\r') return -6;
  1279. if (get_wait_data(cosa) != '\r') return -7;
  1280. if (get_wait_data(cosa) != '\n') return -8;
  1281. if (get_wait_data(cosa) != '\r') return -9;
  1282. if (get_wait_data(cosa) != '\n') return -10;
  1283. #if 0
  1284. printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
  1285. #endif
  1286. return 0;
  1287. }
  1288. /*
  1289. * Reading memory is done via the "r" command of the SRP monitor.
  1290. * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
  1291. * Then driver can read the data and the conversation is finished
  1292. * by SRP monitor sending "<CR><LF>." (dot at the end).
  1293. *
  1294. * This routine is not needed during the normal operation and serves
  1295. * for debugging purposes only.
  1296. */
  1297. static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
  1298. {
  1299. if (put_wait_data(cosa, 'r') == -1) return -1;
  1300. if ((get_wait_data(cosa)) != 'r') return -2;
  1301. if ((get_wait_data(cosa)) != '=') return -3;
  1302. if (puthexnumber(cosa, address) < 0) return -4;
  1303. if (put_wait_data(cosa, ' ') == -1) return -5;
  1304. if (get_wait_data(cosa) != ' ') return -6;
  1305. if (get_wait_data(cosa) != '=') return -7;
  1306. if (puthexnumber(cosa, address+length-1) < 0) return -8;
  1307. if (put_wait_data(cosa, ' ') == -1) return -9;
  1308. if (get_wait_data(cosa) != ' ') return -10;
  1309. while (length--) {
  1310. char c;
  1311. int i;
  1312. if ((i=get_wait_data(cosa)) == -1) {
  1313. pr_info("0x%04x bytes remaining\n", length);
  1314. return -11;
  1315. }
  1316. c=i;
  1317. #if 1
  1318. if (put_user(c, microcode))
  1319. return -23; /* ??? */
  1320. #else
  1321. *microcode = c;
  1322. #endif
  1323. microcode++;
  1324. }
  1325. if (get_wait_data(cosa) != '\r') return -21;
  1326. if (get_wait_data(cosa) != '\n') return -22;
  1327. if (get_wait_data(cosa) != '.') return -23;
  1328. #if 0
  1329. printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
  1330. #endif
  1331. return 0;
  1332. }
  1333. /*
  1334. * This function resets the device and reads the initial prompt
  1335. * of the device's ROM monitor.
  1336. */
  1337. static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
  1338. {
  1339. int i=0, id=0, prev=0, curr=0;
  1340. /* Reset the card ... */
  1341. cosa_putstatus(cosa, 0);
  1342. cosa_getdata8(cosa);
  1343. cosa_putstatus(cosa, SR_RST);
  1344. #ifdef MODULE
  1345. msleep(500);
  1346. #else
  1347. udelay(5*100000);
  1348. #endif
  1349. /* Disable all IRQs from the card */
  1350. cosa_putstatus(cosa, 0);
  1351. /*
  1352. * Try to read the ID string. The card then prints out the
  1353. * identification string ended by the "\n\x2e".
  1354. *
  1355. * The following loop is indexed through i (instead of id)
  1356. * to avoid looping forever when for any reason
  1357. * the port returns '\r', '\n' or '\x2e' permanently.
  1358. */
  1359. for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
  1360. if ((curr = get_wait_data(cosa)) == -1) {
  1361. return -1;
  1362. }
  1363. curr &= 0xff;
  1364. if (curr != '\r' && curr != '\n' && curr != 0x2e)
  1365. idstring[id++] = curr;
  1366. if (curr == 0x2e && prev == '\n')
  1367. break;
  1368. }
  1369. /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
  1370. idstring[id] = '\0';
  1371. return id;
  1372. }
  1373. /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
  1374. /*
  1375. * This routine gets the data byte from the card waiting for the SR_RX_RDY
  1376. * bit to be set in a loop. It should be used in the exceptional cases
  1377. * only (for example when resetting the card or downloading the firmware.
  1378. */
  1379. static int get_wait_data(struct cosa_data *cosa)
  1380. {
  1381. int retries = 1000;
  1382. while (--retries) {
  1383. /* read data and return them */
  1384. if (cosa_getstatus(cosa) & SR_RX_RDY) {
  1385. short r;
  1386. r = cosa_getdata8(cosa);
  1387. #if 0
  1388. pr_info("get_wait_data returning after %d retries\n",
  1389. 999-retries);
  1390. #endif
  1391. return r;
  1392. }
  1393. /* sleep if not ready to read */
  1394. schedule_timeout_interruptible(1);
  1395. }
  1396. pr_info("timeout in get_wait_data (status 0x%x)\n",
  1397. cosa_getstatus(cosa));
  1398. return -1;
  1399. }
  1400. /*
  1401. * This routine puts the data byte to the card waiting for the SR_TX_RDY
  1402. * bit to be set in a loop. It should be used in the exceptional cases
  1403. * only (for example when resetting the card or downloading the firmware).
  1404. */
  1405. static int put_wait_data(struct cosa_data *cosa, int data)
  1406. {
  1407. int retries = 1000;
  1408. while (--retries) {
  1409. /* read data and return them */
  1410. if (cosa_getstatus(cosa) & SR_TX_RDY) {
  1411. cosa_putdata8(cosa, data);
  1412. #if 0
  1413. pr_info("Putdata: %d retries\n", 999-retries);
  1414. #endif
  1415. return 0;
  1416. }
  1417. #if 0
  1418. /* sleep if not ready to read */
  1419. schedule_timeout_interruptible(1);
  1420. #endif
  1421. }
  1422. pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
  1423. cosa->num, cosa_getstatus(cosa));
  1424. return -1;
  1425. }
  1426. /*
  1427. * The following routine puts the hexadecimal number into the SRP monitor
  1428. * and verifies the proper echo of the sent bytes. Returns 0 on success,
  1429. * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
  1430. * (-2,-4,-6,-8) means that reading echo failed.
  1431. */
  1432. static int puthexnumber(struct cosa_data *cosa, int number)
  1433. {
  1434. char temp[5];
  1435. int i;
  1436. /* Well, I should probably replace this by something faster. */
  1437. sprintf(temp, "%04X", number);
  1438. for (i=0; i<4; i++) {
  1439. if (put_wait_data(cosa, temp[i]) == -1) {
  1440. pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
  1441. cosa->num, i);
  1442. return -1-2*i;
  1443. }
  1444. if (get_wait_data(cosa) != temp[i]) {
  1445. pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
  1446. cosa->num, i);
  1447. return -2-2*i;
  1448. }
  1449. }
  1450. return 0;
  1451. }
  1452. /* ---------- Interrupt routines ---------- */
  1453. /*
  1454. * There are three types of interrupt:
  1455. * At the beginning of transmit - this handled is in tx_interrupt(),
  1456. * at the beginning of receive - it is in rx_interrupt() and
  1457. * at the end of transmit/receive - it is the eot_interrupt() function.
  1458. * These functions are multiplexed by cosa_interrupt() according to the
  1459. * COSA status byte. I have moved the rx/tx/eot interrupt handling into
  1460. * separate functions to make it more readable. These functions are inline,
  1461. * so there should be no overhead of function call.
  1462. *
  1463. * In the COSA bus-master mode, we need to tell the card the address of a
  1464. * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
  1465. * It's time to use the bottom half :-(
  1466. */
  1467. /*
  1468. * Transmit interrupt routine - called when COSA is willing to obtain
  1469. * data from the OS. The most tricky part of the routine is selection
  1470. * of channel we (OS) want to send packet for. For SRP we should probably
  1471. * use the round-robin approach. The newer COSA firmwares have a simple
  1472. * flow-control - in the status word has bits 2 and 3 set to 1 means that the
  1473. * channel 0 or 1 doesn't want to receive data.
  1474. *
  1475. * It seems there is a bug in COSA firmware (need to trace it further):
  1476. * When the driver status says that the kernel has no more data for transmit
  1477. * (e.g. at the end of TX DMA) and then the kernel changes its mind
  1478. * (e.g. new packet is queued to hard_start_xmit()), the card issues
  1479. * the TX interrupt but does not mark the channel as ready-to-transmit.
  1480. * The fix seems to be to push the packet to COSA despite its request.
  1481. * We first try to obey the card's opinion, and then fall back to forced TX.
  1482. */
  1483. static inline void tx_interrupt(struct cosa_data *cosa, int status)
  1484. {
  1485. unsigned long flags, flags1;
  1486. #ifdef DEBUG_IRQS
  1487. pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
  1488. #endif
  1489. spin_lock_irqsave(&cosa->lock, flags);
  1490. set_bit(TXBIT, &cosa->rxtx);
  1491. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1492. /* flow control, see the comment above */
  1493. int i=0;
  1494. if (!cosa->txbitmap) {
  1495. pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
  1496. cosa->name);
  1497. put_driver_status_nolock(cosa);
  1498. clear_bit(TXBIT, &cosa->rxtx);
  1499. spin_unlock_irqrestore(&cosa->lock, flags);
  1500. return;
  1501. }
  1502. while (1) {
  1503. cosa->txchan++;
  1504. i++;
  1505. if (cosa->txchan >= cosa->nchannels)
  1506. cosa->txchan = 0;
  1507. if (!(cosa->txbitmap & (1<<cosa->txchan)))
  1508. continue;
  1509. if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
  1510. break;
  1511. /* in second pass, accept first ready-to-TX channel */
  1512. if (i > cosa->nchannels) {
  1513. /* Can be safely ignored */
  1514. #ifdef DEBUG_IRQS
  1515. printk(KERN_DEBUG "%s: Forcing TX "
  1516. "to not-ready channel %d\n",
  1517. cosa->name, cosa->txchan);
  1518. #endif
  1519. break;
  1520. }
  1521. }
  1522. cosa->txsize = cosa->chan[cosa->txchan].txsize;
  1523. if (cosa_dma_able(cosa->chan+cosa->txchan,
  1524. cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
  1525. cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
  1526. } else {
  1527. memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
  1528. cosa->txsize);
  1529. cosa->txbuf = cosa->bouncebuf;
  1530. }
  1531. }
  1532. if (is_8bit(cosa)) {
  1533. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1534. cosa_putstatus(cosa, SR_TX_INT_ENA);
  1535. cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
  1536. ((cosa->txsize >> 8) & 0x1f));
  1537. #ifdef DEBUG_IO
  1538. debug_status_out(cosa, SR_TX_INT_ENA);
  1539. debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
  1540. ((cosa->txsize >> 8) & 0x1f));
  1541. debug_data_in(cosa, cosa_getdata8(cosa));
  1542. #else
  1543. cosa_getdata8(cosa);
  1544. #endif
  1545. set_bit(IRQBIT, &cosa->rxtx);
  1546. spin_unlock_irqrestore(&cosa->lock, flags);
  1547. return;
  1548. } else {
  1549. clear_bit(IRQBIT, &cosa->rxtx);
  1550. cosa_putstatus(cosa, 0);
  1551. cosa_putdata8(cosa, cosa->txsize&0xff);
  1552. #ifdef DEBUG_IO
  1553. debug_status_out(cosa, 0);
  1554. debug_data_out(cosa, cosa->txsize&0xff);
  1555. #endif
  1556. }
  1557. } else {
  1558. cosa_putstatus(cosa, SR_TX_INT_ENA);
  1559. cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
  1560. | (cosa->txsize & 0x1fff));
  1561. #ifdef DEBUG_IO
  1562. debug_status_out(cosa, SR_TX_INT_ENA);
  1563. debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
  1564. | (cosa->txsize & 0x1fff));
  1565. debug_data_in(cosa, cosa_getdata8(cosa));
  1566. debug_status_out(cosa, 0);
  1567. #else
  1568. cosa_getdata8(cosa);
  1569. #endif
  1570. cosa_putstatus(cosa, 0);
  1571. }
  1572. if (cosa->busmaster) {
  1573. unsigned long addr = virt_to_bus(cosa->txbuf);
  1574. int count=0;
  1575. pr_info("busmaster IRQ\n");
  1576. while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
  1577. count++;
  1578. udelay(10);
  1579. if (count > 1000) break;
  1580. }
  1581. pr_info("status %x\n", cosa_getstatus(cosa));
  1582. pr_info("ready after %d loops\n", count);
  1583. cosa_putdata16(cosa, (addr >> 16)&0xffff);
  1584. count = 0;
  1585. while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
  1586. count++;
  1587. if (count > 1000) break;
  1588. udelay(10);
  1589. }
  1590. pr_info("ready after %d loops\n", count);
  1591. cosa_putdata16(cosa, addr &0xffff);
  1592. flags1 = claim_dma_lock();
  1593. set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
  1594. enable_dma(cosa->dma);
  1595. release_dma_lock(flags1);
  1596. } else {
  1597. /* start the DMA */
  1598. flags1 = claim_dma_lock();
  1599. disable_dma(cosa->dma);
  1600. clear_dma_ff(cosa->dma);
  1601. set_dma_mode(cosa->dma, DMA_MODE_WRITE);
  1602. set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
  1603. set_dma_count(cosa->dma, cosa->txsize);
  1604. enable_dma(cosa->dma);
  1605. release_dma_lock(flags1);
  1606. }
  1607. cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
  1608. #ifdef DEBUG_IO
  1609. debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
  1610. #endif
  1611. spin_unlock_irqrestore(&cosa->lock, flags);
  1612. }
  1613. static inline void rx_interrupt(struct cosa_data *cosa, int status)
  1614. {
  1615. unsigned long flags;
  1616. #ifdef DEBUG_IRQS
  1617. pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
  1618. #endif
  1619. spin_lock_irqsave(&cosa->lock, flags);
  1620. set_bit(RXBIT, &cosa->rxtx);
  1621. if (is_8bit(cosa)) {
  1622. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1623. set_bit(IRQBIT, &cosa->rxtx);
  1624. put_driver_status_nolock(cosa);
  1625. cosa->rxsize = cosa_getdata8(cosa) <<8;
  1626. #ifdef DEBUG_IO
  1627. debug_data_in(cosa, cosa->rxsize >> 8);
  1628. #endif
  1629. spin_unlock_irqrestore(&cosa->lock, flags);
  1630. return;
  1631. } else {
  1632. clear_bit(IRQBIT, &cosa->rxtx);
  1633. cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
  1634. #ifdef DEBUG_IO
  1635. debug_data_in(cosa, cosa->rxsize & 0xff);
  1636. #endif
  1637. #if 0
  1638. pr_info("cosa%d: receive rxsize = (0x%04x)\n",
  1639. cosa->num, cosa->rxsize);
  1640. #endif
  1641. }
  1642. } else {
  1643. cosa->rxsize = cosa_getdata16(cosa);
  1644. #ifdef DEBUG_IO
  1645. debug_data_in(cosa, cosa->rxsize);
  1646. #endif
  1647. #if 0
  1648. pr_info("cosa%d: receive rxsize = (0x%04x)\n",
  1649. cosa->num, cosa->rxsize);
  1650. #endif
  1651. }
  1652. if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
  1653. pr_warn("%s: rx for unknown channel (0x%04x)\n",
  1654. cosa->name, cosa->rxsize);
  1655. spin_unlock_irqrestore(&cosa->lock, flags);
  1656. goto reject;
  1657. }
  1658. cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
  1659. cosa->rxsize &= 0x1fff;
  1660. spin_unlock_irqrestore(&cosa->lock, flags);
  1661. cosa->rxbuf = NULL;
  1662. if (cosa->rxchan->setup_rx)
  1663. cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
  1664. if (!cosa->rxbuf) {
  1665. reject: /* Reject the packet */
  1666. pr_info("cosa%d: rejecting packet on channel %d\n",
  1667. cosa->num, cosa->rxchan->num);
  1668. cosa->rxbuf = cosa->bouncebuf;
  1669. }
  1670. /* start the DMA */
  1671. flags = claim_dma_lock();
  1672. disable_dma(cosa->dma);
  1673. clear_dma_ff(cosa->dma);
  1674. set_dma_mode(cosa->dma, DMA_MODE_READ);
  1675. if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
  1676. set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
  1677. } else {
  1678. set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
  1679. }
  1680. set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
  1681. enable_dma(cosa->dma);
  1682. release_dma_lock(flags);
  1683. spin_lock_irqsave(&cosa->lock, flags);
  1684. cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
  1685. if (!is_8bit(cosa) && (status & SR_TX_RDY))
  1686. cosa_putdata8(cosa, DRIVER_RX_READY);
  1687. #ifdef DEBUG_IO
  1688. debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
  1689. if (!is_8bit(cosa) && (status & SR_TX_RDY))
  1690. debug_data_cmd(cosa, DRIVER_RX_READY);
  1691. #endif
  1692. spin_unlock_irqrestore(&cosa->lock, flags);
  1693. }
  1694. static inline void eot_interrupt(struct cosa_data *cosa, int status)
  1695. {
  1696. unsigned long flags, flags1;
  1697. spin_lock_irqsave(&cosa->lock, flags);
  1698. flags1 = claim_dma_lock();
  1699. disable_dma(cosa->dma);
  1700. clear_dma_ff(cosa->dma);
  1701. release_dma_lock(flags1);
  1702. if (test_bit(TXBIT, &cosa->rxtx)) {
  1703. struct channel_data *chan = cosa->chan+cosa->txchan;
  1704. if (chan->tx_done)
  1705. if (chan->tx_done(chan, cosa->txsize))
  1706. clear_bit(chan->num, &cosa->txbitmap);
  1707. } else if (test_bit(RXBIT, &cosa->rxtx)) {
  1708. #ifdef DEBUG_DATA
  1709. {
  1710. int i;
  1711. pr_info("cosa%dc%d: done rx(0x%x)",
  1712. cosa->num, cosa->rxchan->num, cosa->rxsize);
  1713. for (i=0; i<cosa->rxsize; i++)
  1714. pr_cont(" %02x", cosa->rxbuf[i]&0xff);
  1715. pr_cont("\n");
  1716. }
  1717. #endif
  1718. /* Packet for unknown channel? */
  1719. if (cosa->rxbuf == cosa->bouncebuf)
  1720. goto out;
  1721. if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
  1722. memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
  1723. if (cosa->rxchan->rx_done)
  1724. if (cosa->rxchan->rx_done(cosa->rxchan))
  1725. clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
  1726. } else {
  1727. pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
  1728. }
  1729. /*
  1730. * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
  1731. * cleared anyway). We should do it as soon as possible
  1732. * so that we can tell the COSA we are done and to give it a time
  1733. * for recovery.
  1734. */
  1735. out:
  1736. cosa->rxtx = 0;
  1737. put_driver_status_nolock(cosa);
  1738. spin_unlock_irqrestore(&cosa->lock, flags);
  1739. }
  1740. static irqreturn_t cosa_interrupt(int irq, void *cosa_)
  1741. {
  1742. unsigned status;
  1743. int count = 0;
  1744. struct cosa_data *cosa = cosa_;
  1745. again:
  1746. status = cosa_getstatus(cosa);
  1747. #ifdef DEBUG_IRQS
  1748. pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
  1749. #endif
  1750. #ifdef DEBUG_IO
  1751. debug_status_in(cosa, status);
  1752. #endif
  1753. switch (status & SR_CMD_FROM_SRP_MASK) {
  1754. case SR_DOWN_REQUEST:
  1755. tx_interrupt(cosa, status);
  1756. break;
  1757. case SR_UP_REQUEST:
  1758. rx_interrupt(cosa, status);
  1759. break;
  1760. case SR_END_OF_TRANSFER:
  1761. eot_interrupt(cosa, status);
  1762. break;
  1763. default:
  1764. /* We may be too fast for SRP. Try to wait a bit more. */
  1765. if (count++ < 100) {
  1766. udelay(100);
  1767. goto again;
  1768. }
  1769. pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
  1770. cosa->num, status & 0xff, count);
  1771. }
  1772. #ifdef DEBUG_IRQS
  1773. if (count)
  1774. pr_info("%s: %d-times got unknown status in IRQ\n",
  1775. cosa->name, count);
  1776. else
  1777. pr_info("%s: returning from IRQ\n", cosa->name);
  1778. #endif
  1779. return IRQ_HANDLED;
  1780. }
  1781. /* ---------- I/O debugging routines ---------- */
  1782. /*
  1783. * These routines can be used to monitor COSA/SRP I/O and to printk()
  1784. * the data being transferred on the data and status I/O port in a
  1785. * readable way.
  1786. */
  1787. #ifdef DEBUG_IO
  1788. static void debug_status_in(struct cosa_data *cosa, int status)
  1789. {
  1790. char *s;
  1791. switch (status & SR_CMD_FROM_SRP_MASK) {
  1792. case SR_UP_REQUEST:
  1793. s = "RX_REQ";
  1794. break;
  1795. case SR_DOWN_REQUEST:
  1796. s = "TX_REQ";
  1797. break;
  1798. case SR_END_OF_TRANSFER:
  1799. s = "ET_REQ";
  1800. break;
  1801. default:
  1802. s = "NO_REQ";
  1803. break;
  1804. }
  1805. pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
  1806. cosa->name,
  1807. status,
  1808. status & SR_USR_RQ ? "USR_RQ|" : "",
  1809. status & SR_TX_RDY ? "TX_RDY|" : "",
  1810. status & SR_RX_RDY ? "RX_RDY|" : "",
  1811. s);
  1812. }
  1813. static void debug_status_out(struct cosa_data *cosa, int status)
  1814. {
  1815. pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
  1816. cosa->name,
  1817. status,
  1818. status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
  1819. status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
  1820. status & SR_RST ? "RESET|" : "",
  1821. status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
  1822. status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
  1823. status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
  1824. }
  1825. static void debug_data_in(struct cosa_data *cosa, int data)
  1826. {
  1827. pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
  1828. }
  1829. static void debug_data_out(struct cosa_data *cosa, int data)
  1830. {
  1831. pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
  1832. }
  1833. static void debug_data_cmd(struct cosa_data *cosa, int data)
  1834. {
  1835. pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
  1836. cosa->name, data,
  1837. data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
  1838. data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
  1839. }
  1840. #endif
  1841. /* EOF -- this file has not been truncated */