cosa.c 58 KB

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