cosa.c 58 KB

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