cosa.c 58 KB

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