cosa.c 59 KB

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