cosa.c 58 KB

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