cosa.c 58 KB

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