cosa.c 58 KB

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