cosa.c 57 KB

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