cosa.c 58 KB

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