cosa.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  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/mutex.h>
  90. #include <linux/device.h>
  91. #include <linux/smp_lock.h>
  92. #undef COSA_SLOW_IO /* for testing purposes only */
  93. #include <asm/io.h>
  94. #include <asm/dma.h>
  95. #include <asm/byteorder.h>
  96. #include <net/syncppp.h>
  97. #include "cosa.h"
  98. /* Maximum length of the identification string. */
  99. #define COSA_MAX_ID_STRING 128
  100. /* Maximum length of the channel name */
  101. #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
  102. /* Per-channel data structure */
  103. struct channel_data {
  104. void *if_ptr; /* General purpose pointer (used by SPPP) */
  105. int usage; /* Usage count; >0 for chrdev, -1 for netdev */
  106. int num; /* Number of the channel */
  107. struct cosa_data *cosa; /* Pointer to the per-card structure */
  108. int txsize; /* Size of transmitted data */
  109. char *txbuf; /* Transmit buffer */
  110. char name[COSA_MAX_NAME]; /* channel name */
  111. /* The HW layer interface */
  112. /* routine called from the RX interrupt */
  113. char *(*setup_rx)(struct channel_data *channel, int size);
  114. /* routine called when the RX is done (from the EOT interrupt) */
  115. int (*rx_done)(struct channel_data *channel);
  116. /* routine called when the TX is done (from the EOT interrupt) */
  117. int (*tx_done)(struct channel_data *channel, int size);
  118. /* Character device parts */
  119. struct mutex rlock;
  120. struct semaphore wsem;
  121. char *rxdata;
  122. int rxsize;
  123. wait_queue_head_t txwaitq, rxwaitq;
  124. int tx_status, rx_status;
  125. /* SPPP/HDLC device parts */
  126. struct ppp_device pppdev;
  127. struct sk_buff *rx_skb, *tx_skb;
  128. struct net_device_stats stats;
  129. };
  130. /* cosa->firmware_status bits */
  131. #define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
  132. #define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
  133. #define COSA_FW_START (1<<2) /* Is the microcode running? */
  134. struct cosa_data {
  135. int num; /* Card number */
  136. char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
  137. unsigned int datareg, statusreg; /* I/O ports */
  138. unsigned short irq, dma; /* IRQ and DMA number */
  139. unsigned short startaddr; /* Firmware start address */
  140. unsigned short busmaster; /* Use busmastering? */
  141. int nchannels; /* # of channels on this card */
  142. int driver_status; /* For communicating with firmware */
  143. int firmware_status; /* Downloaded, reseted, etc. */
  144. unsigned long rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
  145. unsigned long rxtx; /* RX or TX in progress? */
  146. int enabled;
  147. int usage; /* usage count */
  148. int txchan, txsize, rxsize;
  149. struct channel_data *rxchan;
  150. char *bouncebuf;
  151. char *txbuf, *rxbuf;
  152. struct channel_data *chan;
  153. spinlock_t lock; /* For exclusive operations on this structure */
  154. char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
  155. char *type; /* card type */
  156. };
  157. /*
  158. * Define this if you want all the possible ports to be autoprobed.
  159. * It is here but it probably is not a good idea to use this.
  160. */
  161. /* #define COSA_ISA_AUTOPROBE 1 */
  162. /*
  163. * Character device major number. 117 was allocated for us.
  164. * The value of 0 means to allocate a first free one.
  165. */
  166. static int cosa_major = 117;
  167. /*
  168. * Encoding of the minor numbers:
  169. * The lowest CARD_MINOR_BITS bits means the channel on the single card,
  170. * the highest bits means the card number.
  171. */
  172. #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
  173. * for the single card */
  174. /*
  175. * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
  176. * macro doesn't like anything other than the raw number as an argument :-(
  177. */
  178. #define MAX_CARDS 16
  179. /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
  180. #define DRIVER_RX_READY 0x0001
  181. #define DRIVER_TX_READY 0x0002
  182. #define DRIVER_TXMAP_SHIFT 2
  183. #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
  184. /*
  185. * for cosa->rxtx - indicates whether either transmit or receive is
  186. * in progress. These values are mean number of the bit.
  187. */
  188. #define TXBIT 0
  189. #define RXBIT 1
  190. #define IRQBIT 2
  191. #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
  192. #undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
  193. #undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
  194. #undef DEBUG_IO //1 /* Dump the I/O traffic */
  195. #define TX_TIMEOUT (5*HZ)
  196. /* Maybe the following should be allocated dynamically */
  197. static struct cosa_data cosa_cards[MAX_CARDS];
  198. static int nr_cards;
  199. #ifdef COSA_ISA_AUTOPROBE
  200. static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
  201. /* NOTE: DMA is not autoprobed!!! */
  202. static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
  203. #else
  204. static int io[MAX_CARDS+1];
  205. static int dma[MAX_CARDS+1];
  206. #endif
  207. /* IRQ can be safely autoprobed */
  208. static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
  209. /* for class stuff*/
  210. static struct class *cosa_class;
  211. #ifdef MODULE
  212. module_param_array(io, int, NULL, 0);
  213. MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
  214. module_param_array(irq, int, NULL, 0);
  215. MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
  216. module_param_array(dma, int, NULL, 0);
  217. MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
  218. MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
  219. MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
  220. MODULE_LICENSE("GPL");
  221. #endif
  222. /* I use this mainly for testing purposes */
  223. #ifdef COSA_SLOW_IO
  224. #define cosa_outb outb_p
  225. #define cosa_outw outw_p
  226. #define cosa_inb inb_p
  227. #define cosa_inw inw_p
  228. #else
  229. #define cosa_outb outb
  230. #define cosa_outw outw
  231. #define cosa_inb inb
  232. #define cosa_inw inw
  233. #endif
  234. #define is_8bit(cosa) (!(cosa->datareg & 0x08))
  235. #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
  236. #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
  237. #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
  238. #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
  239. #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
  240. #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
  241. /* Initialization stuff */
  242. static int cosa_probe(int ioaddr, int irq, int dma);
  243. /* HW interface */
  244. static void cosa_enable_rx(struct channel_data *chan);
  245. static void cosa_disable_rx(struct channel_data *chan);
  246. static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
  247. static void cosa_kick(struct cosa_data *cosa);
  248. static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
  249. /* SPPP/HDLC stuff */
  250. static void sppp_channel_init(struct channel_data *chan);
  251. static void sppp_channel_delete(struct channel_data *chan);
  252. static int cosa_sppp_open(struct net_device *d);
  253. static int cosa_sppp_close(struct net_device *d);
  254. static void cosa_sppp_timeout(struct net_device *d);
  255. static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *d);
  256. static char *sppp_setup_rx(struct channel_data *channel, int size);
  257. static int sppp_rx_done(struct channel_data *channel);
  258. static int sppp_tx_done(struct channel_data *channel, int size);
  259. static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  260. static struct net_device_stats *cosa_net_stats(struct net_device *dev);
  261. /* Character device */
  262. static void chardev_channel_init(struct channel_data *chan);
  263. static char *chrdev_setup_rx(struct channel_data *channel, int size);
  264. static int chrdev_rx_done(struct channel_data *channel);
  265. static int chrdev_tx_done(struct channel_data *channel, int size);
  266. static ssize_t cosa_read(struct file *file,
  267. char __user *buf, size_t count, loff_t *ppos);
  268. static ssize_t cosa_write(struct file *file,
  269. const char __user *buf, size_t count, loff_t *ppos);
  270. static unsigned int cosa_poll(struct file *file, poll_table *poll);
  271. static int cosa_open(struct inode *inode, struct file *file);
  272. static int cosa_release(struct inode *inode, struct file *file);
  273. static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
  274. unsigned int cmd, unsigned long arg);
  275. #ifdef COSA_FASYNC_WORKING
  276. static int cosa_fasync(struct inode *inode, struct file *file, int on);
  277. #endif
  278. static const struct file_operations cosa_fops = {
  279. .owner = THIS_MODULE,
  280. .llseek = no_llseek,
  281. .read = cosa_read,
  282. .write = cosa_write,
  283. .poll = cosa_poll,
  284. .ioctl = cosa_chardev_ioctl,
  285. .open = cosa_open,
  286. .release = cosa_release,
  287. #ifdef COSA_FASYNC_WORKING
  288. .fasync = cosa_fasync,
  289. #endif
  290. };
  291. /* Ioctls */
  292. static int cosa_start(struct cosa_data *cosa, int address);
  293. static int cosa_reset(struct cosa_data *cosa);
  294. static int cosa_download(struct cosa_data *cosa, void __user *a);
  295. static int cosa_readmem(struct cosa_data *cosa, void __user *a);
  296. /* COSA/SRP ROM monitor */
  297. static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
  298. static int startmicrocode(struct cosa_data *cosa, int address);
  299. static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
  300. static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
  301. /* Auxilliary functions */
  302. static int get_wait_data(struct cosa_data *cosa);
  303. static int put_wait_data(struct cosa_data *cosa, int data);
  304. static int puthexnumber(struct cosa_data *cosa, int number);
  305. static void put_driver_status(struct cosa_data *cosa);
  306. static void put_driver_status_nolock(struct cosa_data *cosa);
  307. /* Interrupt handling */
  308. static irqreturn_t cosa_interrupt(int irq, void *cosa);
  309. /* I/O ops debugging */
  310. #ifdef DEBUG_IO
  311. static void debug_data_in(struct cosa_data *cosa, int data);
  312. static void debug_data_out(struct cosa_data *cosa, int data);
  313. static void debug_data_cmd(struct cosa_data *cosa, int data);
  314. static void debug_status_in(struct cosa_data *cosa, int status);
  315. static void debug_status_out(struct cosa_data *cosa, int status);
  316. #endif
  317. /* ---------- Initialization stuff ---------- */
  318. static int __init cosa_init(void)
  319. {
  320. int i, err = 0;
  321. printk(KERN_INFO "cosa v1.08 (c) 1997-2000 Jan Kasprzak <kas@fi.muni.cz>\n");
  322. #ifdef CONFIG_SMP
  323. printk(KERN_INFO "cosa: SMP found. Please mail any success/failure reports to the author.\n");
  324. #endif
  325. if (cosa_major > 0) {
  326. if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
  327. printk(KERN_WARNING "cosa: unable to get major %d\n",
  328. cosa_major);
  329. err = -EIO;
  330. goto out;
  331. }
  332. } else {
  333. if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
  334. printk(KERN_WARNING "cosa: unable to register chardev\n");
  335. err = -EIO;
  336. goto out;
  337. }
  338. }
  339. for (i=0; i<MAX_CARDS; i++)
  340. cosa_cards[i].num = -1;
  341. for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
  342. cosa_probe(io[i], irq[i], dma[i]);
  343. if (!nr_cards) {
  344. printk(KERN_WARNING "cosa: no devices found.\n");
  345. unregister_chrdev(cosa_major, "cosa");
  346. err = -ENODEV;
  347. goto out;
  348. }
  349. cosa_class = class_create(THIS_MODULE, "cosa");
  350. if (IS_ERR(cosa_class)) {
  351. err = PTR_ERR(cosa_class);
  352. goto out_chrdev;
  353. }
  354. for (i=0; i<nr_cards; i++) {
  355. device_create(cosa_class, NULL, MKDEV(cosa_major, i), "cosa%d", i);
  356. }
  357. err = 0;
  358. goto out;
  359. out_chrdev:
  360. unregister_chrdev(cosa_major, "cosa");
  361. out:
  362. return err;
  363. }
  364. module_init(cosa_init);
  365. static void __exit cosa_exit(void)
  366. {
  367. struct cosa_data *cosa;
  368. int i;
  369. printk(KERN_INFO "Unloading the cosa module\n");
  370. for (i=0; i<nr_cards; i++)
  371. device_destroy(cosa_class, MKDEV(cosa_major, i));
  372. class_destroy(cosa_class);
  373. for (cosa=cosa_cards; nr_cards--; cosa++) {
  374. /* Clean up the per-channel data */
  375. for (i=0; i<cosa->nchannels; i++) {
  376. /* Chardev driver has no alloc'd per-channel data */
  377. sppp_channel_delete(cosa->chan+i);
  378. }
  379. /* Clean up the per-card data */
  380. kfree(cosa->chan);
  381. kfree(cosa->bouncebuf);
  382. free_irq(cosa->irq, cosa);
  383. free_dma(cosa->dma);
  384. release_region(cosa->datareg,is_8bit(cosa)?2:4);
  385. }
  386. unregister_chrdev(cosa_major, "cosa");
  387. }
  388. module_exit(cosa_exit);
  389. /*
  390. * This function should register all the net devices needed for the
  391. * single channel.
  392. */
  393. static __inline__ void channel_init(struct channel_data *chan)
  394. {
  395. sprintf(chan->name, "cosa%dc%d", chan->cosa->num, chan->num);
  396. /* Initialize the chardev data structures */
  397. chardev_channel_init(chan);
  398. /* Register the sppp interface */
  399. sppp_channel_init(chan);
  400. }
  401. static int cosa_probe(int base, int irq, int dma)
  402. {
  403. struct cosa_data *cosa = cosa_cards+nr_cards;
  404. int i, err = 0;
  405. memset(cosa, 0, sizeof(struct cosa_data));
  406. /* Checking validity of parameters: */
  407. /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
  408. if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
  409. printk (KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
  410. return -1;
  411. }
  412. /* I/O address should be between 0x100 and 0x3ff and should be
  413. * multiple of 8. */
  414. if (base < 0x100 || base > 0x3ff || base & 0x7) {
  415. printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
  416. base);
  417. return -1;
  418. }
  419. /* DMA should be 0,1 or 3-7 */
  420. if (dma < 0 || dma == 4 || dma > 7) {
  421. printk (KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
  422. return -1;
  423. }
  424. /* and finally, on 16-bit COSA DMA should be 4-7 and
  425. * I/O base should not be multiple of 0x10 */
  426. if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
  427. printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
  428. " (base=0x%x, dma=%d)\n", base, dma);
  429. return -1;
  430. }
  431. cosa->dma = dma;
  432. cosa->datareg = base;
  433. cosa->statusreg = is_8bit(cosa)?base+1:base+2;
  434. spin_lock_init(&cosa->lock);
  435. if (!request_region(base, is_8bit(cosa)?2:4,"cosa"))
  436. return -1;
  437. if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
  438. printk(KERN_DEBUG "cosa: probe at 0x%x failed.\n", base);
  439. err = -1;
  440. goto err_out;
  441. }
  442. /* Test the validity of identification string */
  443. if (!strncmp(cosa->id_string, "SRP", 3))
  444. cosa->type = "srp";
  445. else if (!strncmp(cosa->id_string, "COSA", 4))
  446. cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
  447. else {
  448. /* Print a warning only if we are not autoprobing */
  449. #ifndef COSA_ISA_AUTOPROBE
  450. printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
  451. base);
  452. #endif
  453. err = -1;
  454. goto err_out;
  455. }
  456. /* Update the name of the region now we know the type of card */
  457. release_region(base, is_8bit(cosa)?2:4);
  458. if (!request_region(base, is_8bit(cosa)?2:4, cosa->type)) {
  459. printk(KERN_DEBUG "cosa: changing name at 0x%x failed.\n", base);
  460. return -1;
  461. }
  462. /* Now do IRQ autoprobe */
  463. if (irq < 0) {
  464. unsigned long irqs;
  465. /* printk(KERN_INFO "IRQ autoprobe\n"); */
  466. irqs = probe_irq_on();
  467. /*
  468. * Enable interrupt on tx buffer empty (it sure is)
  469. * really sure ?
  470. * FIXME: When this code is not used as module, we should
  471. * probably call udelay() instead of the interruptible sleep.
  472. */
  473. set_current_state(TASK_INTERRUPTIBLE);
  474. cosa_putstatus(cosa, SR_TX_INT_ENA);
  475. schedule_timeout(30);
  476. irq = probe_irq_off(irqs);
  477. /* Disable all IRQs from the card */
  478. cosa_putstatus(cosa, 0);
  479. /* Empty the received data register */
  480. cosa_getdata8(cosa);
  481. if (irq < 0) {
  482. printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
  483. irq, cosa->datareg);
  484. err = -1;
  485. goto err_out;
  486. }
  487. if (irq == 0) {
  488. printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
  489. cosa->datareg);
  490. /* return -1; */
  491. }
  492. }
  493. cosa->irq = irq;
  494. cosa->num = nr_cards;
  495. cosa->usage = 0;
  496. cosa->nchannels = 2; /* FIXME: how to determine this? */
  497. if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
  498. err = -1;
  499. goto err_out;
  500. }
  501. if (request_dma(cosa->dma, cosa->type)) {
  502. err = -1;
  503. goto err_out1;
  504. }
  505. cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
  506. if (!cosa->bouncebuf) {
  507. err = -ENOMEM;
  508. goto err_out2;
  509. }
  510. sprintf(cosa->name, "cosa%d", cosa->num);
  511. /* Initialize the per-channel data */
  512. cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
  513. if (!cosa->chan) {
  514. err = -ENOMEM;
  515. goto err_out3;
  516. }
  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->ml_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->ml_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->ml_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->ml_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->ml_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->ml_priv;
  717. return &chan->stats;
  718. }
  719. /*---------- Character device ---------- */
  720. static void chardev_channel_init(struct channel_data *chan)
  721. {
  722. mutex_init(&chan->rlock);
  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 (mutex_lock_interruptible(&chan->rlock))
  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. mutex_unlock(&chan->rlock);
  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. mutex_unlock(&chan->rlock);
  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. mutex_unlock(&chan->rlock);
  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. int ret = 0;
  868. lock_kernel();
  869. if ((n=iminor(file->f_path.dentry->d_inode)>>CARD_MINOR_BITS)
  870. >= nr_cards) {
  871. ret = -ENODEV;
  872. goto out;
  873. }
  874. cosa = cosa_cards+n;
  875. if ((n=iminor(file->f_path.dentry->d_inode)
  876. & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
  877. ret = -ENODEV;
  878. goto out;
  879. }
  880. chan = cosa->chan + n;
  881. file->private_data = chan;
  882. spin_lock_irqsave(&cosa->lock, flags);
  883. if (chan->usage < 0) { /* in netdev mode */
  884. spin_unlock_irqrestore(&cosa->lock, flags);
  885. ret = -EBUSY;
  886. goto out;
  887. }
  888. cosa->usage++;
  889. chan->usage++;
  890. chan->tx_done = chrdev_tx_done;
  891. chan->setup_rx = chrdev_setup_rx;
  892. chan->rx_done = chrdev_rx_done;
  893. spin_unlock_irqrestore(&cosa->lock, flags);
  894. out:
  895. unlock_kernel();
  896. return ret;
  897. }
  898. static int cosa_release(struct inode *inode, struct file *file)
  899. {
  900. struct channel_data *channel = file->private_data;
  901. struct cosa_data *cosa;
  902. unsigned long flags;
  903. cosa = channel->cosa;
  904. spin_lock_irqsave(&cosa->lock, flags);
  905. cosa->usage--;
  906. channel->usage--;
  907. spin_unlock_irqrestore(&cosa->lock, flags);
  908. return 0;
  909. }
  910. #ifdef COSA_FASYNC_WORKING
  911. static struct fasync_struct *fasync[256] = { NULL, };
  912. /* To be done ... */
  913. static int cosa_fasync(struct inode *inode, struct file *file, int on)
  914. {
  915. int port = iminor(inode);
  916. int rv = fasync_helper(inode, file, on, &fasync[port]);
  917. return rv < 0 ? rv : 0;
  918. }
  919. #endif
  920. /* ---------- Ioctls ---------- */
  921. /*
  922. * Ioctl subroutines can safely be made inline, because they are called
  923. * only from cosa_ioctl().
  924. */
  925. static inline int cosa_reset(struct cosa_data *cosa)
  926. {
  927. char idstring[COSA_MAX_ID_STRING];
  928. if (cosa->usage > 1)
  929. printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  930. cosa->num, cosa->usage);
  931. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
  932. if (cosa_reset_and_read_id(cosa, idstring) < 0) {
  933. printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
  934. return -EIO;
  935. }
  936. printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
  937. idstring);
  938. cosa->firmware_status |= COSA_FW_RESET;
  939. return 0;
  940. }
  941. /* High-level function to download data into COSA memory. Calls download() */
  942. static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
  943. {
  944. struct cosa_download d;
  945. int i;
  946. if (cosa->usage > 1)
  947. printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  948. cosa->name, cosa->usage);
  949. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  950. printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
  951. cosa->name, cosa->firmware_status);
  952. return -EPERM;
  953. }
  954. if (copy_from_user(&d, arg, sizeof(d)))
  955. return -EFAULT;
  956. if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
  957. return -EINVAL;
  958. if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
  959. return -EINVAL;
  960. /* If something fails, force the user to reset the card */
  961. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
  962. i = download(cosa, d.code, d.len, d.addr);
  963. if (i < 0) {
  964. printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
  965. cosa->num, i);
  966. return -EIO;
  967. }
  968. printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
  969. cosa->num, d.len, d.addr);
  970. cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
  971. return 0;
  972. }
  973. /* High-level function to read COSA memory. Calls readmem() */
  974. static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
  975. {
  976. struct cosa_download d;
  977. int i;
  978. if (cosa->usage > 1)
  979. printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
  980. "cosa->usage > 1 (%d). Odd things may happen.\n",
  981. cosa->num, cosa->usage);
  982. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  983. printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
  984. cosa->name, cosa->firmware_status);
  985. return -EPERM;
  986. }
  987. if (copy_from_user(&d, arg, sizeof(d)))
  988. return -EFAULT;
  989. /* If something fails, force the user to reset the card */
  990. cosa->firmware_status &= ~COSA_FW_RESET;
  991. i = readmem(cosa, d.code, d.len, d.addr);
  992. if (i < 0) {
  993. printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
  994. cosa->num, i);
  995. return -EIO;
  996. }
  997. printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
  998. cosa->num, d.len, d.addr);
  999. cosa->firmware_status |= COSA_FW_RESET;
  1000. return 0;
  1001. }
  1002. /* High-level function to start microcode. Calls startmicrocode(). */
  1003. static inline int cosa_start(struct cosa_data *cosa, int address)
  1004. {
  1005. int i;
  1006. if (cosa->usage > 1)
  1007. printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  1008. cosa->num, cosa->usage);
  1009. if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
  1010. != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
  1011. printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
  1012. cosa->name, cosa->firmware_status);
  1013. return -EPERM;
  1014. }
  1015. cosa->firmware_status &= ~COSA_FW_RESET;
  1016. if ((i=startmicrocode(cosa, address)) < 0) {
  1017. printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
  1018. cosa->num, address, i);
  1019. return -EIO;
  1020. }
  1021. printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
  1022. cosa->num, address);
  1023. cosa->startaddr = address;
  1024. cosa->firmware_status |= COSA_FW_START;
  1025. return 0;
  1026. }
  1027. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  1028. static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
  1029. {
  1030. int l = strlen(cosa->id_string)+1;
  1031. if (copy_to_user(string, cosa->id_string, l))
  1032. return -EFAULT;
  1033. return l;
  1034. }
  1035. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  1036. static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
  1037. {
  1038. int l = strlen(cosa->type)+1;
  1039. if (copy_to_user(string, cosa->type, l))
  1040. return -EFAULT;
  1041. return l;
  1042. }
  1043. static int cosa_ioctl_common(struct cosa_data *cosa,
  1044. struct channel_data *channel, unsigned int cmd, unsigned long arg)
  1045. {
  1046. void __user *argp = (void __user *)arg;
  1047. switch(cmd) {
  1048. case COSAIORSET: /* Reset the device */
  1049. if (!capable(CAP_NET_ADMIN))
  1050. return -EACCES;
  1051. return cosa_reset(cosa);
  1052. case COSAIOSTRT: /* Start the firmware */
  1053. if (!capable(CAP_SYS_RAWIO))
  1054. return -EACCES;
  1055. return cosa_start(cosa, arg);
  1056. case COSAIODOWNLD: /* Download the firmware */
  1057. if (!capable(CAP_SYS_RAWIO))
  1058. return -EACCES;
  1059. return cosa_download(cosa, argp);
  1060. case COSAIORMEM:
  1061. if (!capable(CAP_SYS_RAWIO))
  1062. return -EACCES;
  1063. return cosa_readmem(cosa, argp);
  1064. case COSAIORTYPE:
  1065. return cosa_gettype(cosa, argp);
  1066. case COSAIORIDSTR:
  1067. return cosa_getidstr(cosa, argp);
  1068. case COSAIONRCARDS:
  1069. return nr_cards;
  1070. case COSAIONRCHANS:
  1071. return cosa->nchannels;
  1072. case COSAIOBMSET:
  1073. if (!capable(CAP_SYS_RAWIO))
  1074. return -EACCES;
  1075. if (is_8bit(cosa))
  1076. return -EINVAL;
  1077. if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
  1078. return -EINVAL;
  1079. cosa->busmaster = arg;
  1080. return 0;
  1081. case COSAIOBMGET:
  1082. return cosa->busmaster;
  1083. }
  1084. return -ENOIOCTLCMD;
  1085. }
  1086. static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr,
  1087. int cmd)
  1088. {
  1089. int rv;
  1090. struct channel_data *chan = dev->ml_priv;
  1091. rv = cosa_ioctl_common(chan->cosa, chan, cmd, (unsigned long)ifr->ifr_data);
  1092. if (rv == -ENOIOCTLCMD) {
  1093. return sppp_do_ioctl(dev, ifr, cmd);
  1094. }
  1095. return rv;
  1096. }
  1097. static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
  1098. unsigned int cmd, unsigned long arg)
  1099. {
  1100. struct channel_data *channel = file->private_data;
  1101. struct cosa_data *cosa = channel->cosa;
  1102. return cosa_ioctl_common(cosa, channel, cmd, arg);
  1103. }
  1104. /*---------- HW layer interface ---------- */
  1105. /*
  1106. * The higher layer can bind itself to the HW layer by setting the callbacks
  1107. * in the channel_data structure and by using these routines.
  1108. */
  1109. static void cosa_enable_rx(struct channel_data *chan)
  1110. {
  1111. struct cosa_data *cosa = chan->cosa;
  1112. if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
  1113. put_driver_status(cosa);
  1114. }
  1115. static void cosa_disable_rx(struct channel_data *chan)
  1116. {
  1117. struct cosa_data *cosa = chan->cosa;
  1118. if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
  1119. put_driver_status(cosa);
  1120. }
  1121. /*
  1122. * FIXME: This routine probably should check for cosa_start_tx() called when
  1123. * the previous transmit is still unfinished. In this case the non-zero
  1124. * return value should indicate to the caller that the queuing(sp?) up
  1125. * the transmit has failed.
  1126. */
  1127. static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
  1128. {
  1129. struct cosa_data *cosa = chan->cosa;
  1130. unsigned long flags;
  1131. #ifdef DEBUG_DATA
  1132. int i;
  1133. printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
  1134. chan->num, len);
  1135. for (i=0; i<len; i++)
  1136. printk(" %02x", buf[i]&0xff);
  1137. printk("\n");
  1138. #endif
  1139. spin_lock_irqsave(&cosa->lock, flags);
  1140. chan->txbuf = buf;
  1141. chan->txsize = len;
  1142. if (len > COSA_MTU)
  1143. chan->txsize = COSA_MTU;
  1144. spin_unlock_irqrestore(&cosa->lock, flags);
  1145. /* Tell the firmware we are ready */
  1146. set_bit(chan->num, &cosa->txbitmap);
  1147. put_driver_status(cosa);
  1148. return 0;
  1149. }
  1150. static void put_driver_status(struct cosa_data *cosa)
  1151. {
  1152. unsigned long flags;
  1153. int status;
  1154. spin_lock_irqsave(&cosa->lock, flags);
  1155. status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
  1156. | (cosa->txbitmap ? DRIVER_TX_READY : 0)
  1157. | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
  1158. &DRIVER_TXMAP_MASK : 0);
  1159. if (!cosa->rxtx) {
  1160. if (cosa->rxbitmap|cosa->txbitmap) {
  1161. if (!cosa->enabled) {
  1162. cosa_putstatus(cosa, SR_RX_INT_ENA);
  1163. #ifdef DEBUG_IO
  1164. debug_status_out(cosa, SR_RX_INT_ENA);
  1165. #endif
  1166. cosa->enabled = 1;
  1167. }
  1168. } else if (cosa->enabled) {
  1169. cosa->enabled = 0;
  1170. cosa_putstatus(cosa, 0);
  1171. #ifdef DEBUG_IO
  1172. debug_status_out(cosa, 0);
  1173. #endif
  1174. }
  1175. cosa_putdata8(cosa, status);
  1176. #ifdef DEBUG_IO
  1177. debug_data_cmd(cosa, status);
  1178. #endif
  1179. }
  1180. spin_unlock_irqrestore(&cosa->lock, flags);
  1181. }
  1182. static void put_driver_status_nolock(struct cosa_data *cosa)
  1183. {
  1184. int status;
  1185. status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
  1186. | (cosa->txbitmap ? DRIVER_TX_READY : 0)
  1187. | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
  1188. &DRIVER_TXMAP_MASK : 0);
  1189. if (cosa->rxbitmap|cosa->txbitmap) {
  1190. cosa_putstatus(cosa, SR_RX_INT_ENA);
  1191. #ifdef DEBUG_IO
  1192. debug_status_out(cosa, SR_RX_INT_ENA);
  1193. #endif
  1194. cosa->enabled = 1;
  1195. } else {
  1196. cosa_putstatus(cosa, 0);
  1197. #ifdef DEBUG_IO
  1198. debug_status_out(cosa, 0);
  1199. #endif
  1200. cosa->enabled = 0;
  1201. }
  1202. cosa_putdata8(cosa, status);
  1203. #ifdef DEBUG_IO
  1204. debug_data_cmd(cosa, status);
  1205. #endif
  1206. }
  1207. /*
  1208. * The "kickme" function: When the DMA times out, this is called to
  1209. * clean up the driver status.
  1210. * FIXME: Preliminary support, the interface is probably wrong.
  1211. */
  1212. static void cosa_kick(struct cosa_data *cosa)
  1213. {
  1214. unsigned long flags, flags1;
  1215. char *s = "(probably) IRQ";
  1216. if (test_bit(RXBIT, &cosa->rxtx))
  1217. s = "RX DMA";
  1218. if (test_bit(TXBIT, &cosa->rxtx))
  1219. s = "TX DMA";
  1220. printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
  1221. spin_lock_irqsave(&cosa->lock, flags);
  1222. cosa->rxtx = 0;
  1223. flags1 = claim_dma_lock();
  1224. disable_dma(cosa->dma);
  1225. clear_dma_ff(cosa->dma);
  1226. release_dma_lock(flags1);
  1227. /* FIXME: Anything else? */
  1228. udelay(100);
  1229. cosa_putstatus(cosa, 0);
  1230. udelay(100);
  1231. (void) cosa_getdata8(cosa);
  1232. udelay(100);
  1233. cosa_putdata8(cosa, 0);
  1234. udelay(100);
  1235. put_driver_status_nolock(cosa);
  1236. spin_unlock_irqrestore(&cosa->lock, flags);
  1237. }
  1238. /*
  1239. * Check if the whole buffer is DMA-able. It means it is below the 16M of
  1240. * physical memory and doesn't span the 64k boundary. For now it seems
  1241. * SKB's never do this, but we'll check this anyway.
  1242. */
  1243. static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
  1244. {
  1245. static int count;
  1246. unsigned long b = (unsigned long)buf;
  1247. if (b+len >= MAX_DMA_ADDRESS)
  1248. return 0;
  1249. if ((b^ (b+len)) & 0x10000) {
  1250. if (count++ < 5)
  1251. printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
  1252. chan->name);
  1253. return 0;
  1254. }
  1255. return 1;
  1256. }
  1257. /* ---------- The SRP/COSA ROM monitor functions ---------- */
  1258. /*
  1259. * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
  1260. * drivers need to say 4-digit hex number meaning start address of the microcode
  1261. * separated by a single space. Monitor replies by saying " =". Now driver
  1262. * has to write 4-digit hex number meaning the last byte address ended
  1263. * by a single space. Monitor has to reply with a space. Now the download
  1264. * begins. After the download monitor replies with "\r\n." (CR LF dot).
  1265. */
  1266. static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
  1267. {
  1268. int i;
  1269. if (put_wait_data(cosa, 'w') == -1) return -1;
  1270. if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
  1271. if (get_wait_data(cosa) != '=') return -3;
  1272. if (puthexnumber(cosa, address) < 0) return -4;
  1273. if (put_wait_data(cosa, ' ') == -1) return -10;
  1274. if (get_wait_data(cosa) != ' ') return -11;
  1275. if (get_wait_data(cosa) != '=') return -12;
  1276. if (puthexnumber(cosa, address+length-1) < 0) return -13;
  1277. if (put_wait_data(cosa, ' ') == -1) return -18;
  1278. if (get_wait_data(cosa) != ' ') return -19;
  1279. while (length--) {
  1280. char c;
  1281. #ifndef SRP_DOWNLOAD_AT_BOOT
  1282. if (get_user(c, microcode))
  1283. return -23; /* ??? */
  1284. #else
  1285. c = *microcode;
  1286. #endif
  1287. if (put_wait_data(cosa, c) == -1)
  1288. return -20;
  1289. microcode++;
  1290. }
  1291. if (get_wait_data(cosa) != '\r') return -21;
  1292. if (get_wait_data(cosa) != '\n') return -22;
  1293. if (get_wait_data(cosa) != '.') return -23;
  1294. #if 0
  1295. printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
  1296. #endif
  1297. return 0;
  1298. }
  1299. /*
  1300. * Starting microcode is done via the "g" command of the SRP monitor.
  1301. * The chat should be the following: "g" "g=" "<addr><CR>"
  1302. * "<CR><CR><LF><CR><LF>".
  1303. */
  1304. static int startmicrocode(struct cosa_data *cosa, int address)
  1305. {
  1306. if (put_wait_data(cosa, 'g') == -1) return -1;
  1307. if (get_wait_data(cosa) != 'g') return -2;
  1308. if (get_wait_data(cosa) != '=') return -3;
  1309. if (puthexnumber(cosa, address) < 0) return -4;
  1310. if (put_wait_data(cosa, '\r') == -1) return -5;
  1311. if (get_wait_data(cosa) != '\r') return -6;
  1312. if (get_wait_data(cosa) != '\r') return -7;
  1313. if (get_wait_data(cosa) != '\n') return -8;
  1314. if (get_wait_data(cosa) != '\r') return -9;
  1315. if (get_wait_data(cosa) != '\n') return -10;
  1316. #if 0
  1317. printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
  1318. #endif
  1319. return 0;
  1320. }
  1321. /*
  1322. * Reading memory is done via the "r" command of the SRP monitor.
  1323. * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
  1324. * Then driver can read the data and the conversation is finished
  1325. * by SRP monitor sending "<CR><LF>." (dot at the end).
  1326. *
  1327. * This routine is not needed during the normal operation and serves
  1328. * for debugging purposes only.
  1329. */
  1330. static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
  1331. {
  1332. if (put_wait_data(cosa, 'r') == -1) return -1;
  1333. if ((get_wait_data(cosa)) != 'r') return -2;
  1334. if ((get_wait_data(cosa)) != '=') return -3;
  1335. if (puthexnumber(cosa, address) < 0) return -4;
  1336. if (put_wait_data(cosa, ' ') == -1) return -5;
  1337. if (get_wait_data(cosa) != ' ') return -6;
  1338. if (get_wait_data(cosa) != '=') return -7;
  1339. if (puthexnumber(cosa, address+length-1) < 0) return -8;
  1340. if (put_wait_data(cosa, ' ') == -1) return -9;
  1341. if (get_wait_data(cosa) != ' ') return -10;
  1342. while (length--) {
  1343. char c;
  1344. int i;
  1345. if ((i=get_wait_data(cosa)) == -1) {
  1346. printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
  1347. length);
  1348. return -11;
  1349. }
  1350. c=i;
  1351. #if 1
  1352. if (put_user(c, microcode))
  1353. return -23; /* ??? */
  1354. #else
  1355. *microcode = c;
  1356. #endif
  1357. microcode++;
  1358. }
  1359. if (get_wait_data(cosa) != '\r') return -21;
  1360. if (get_wait_data(cosa) != '\n') return -22;
  1361. if (get_wait_data(cosa) != '.') return -23;
  1362. #if 0
  1363. printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
  1364. #endif
  1365. return 0;
  1366. }
  1367. /*
  1368. * This function resets the device and reads the initial prompt
  1369. * of the device's ROM monitor.
  1370. */
  1371. static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
  1372. {
  1373. int i=0, id=0, prev=0, curr=0;
  1374. /* Reset the card ... */
  1375. cosa_putstatus(cosa, 0);
  1376. cosa_getdata8(cosa);
  1377. cosa_putstatus(cosa, SR_RST);
  1378. #ifdef MODULE
  1379. msleep(500);
  1380. #else
  1381. udelay(5*100000);
  1382. #endif
  1383. /* Disable all IRQs from the card */
  1384. cosa_putstatus(cosa, 0);
  1385. /*
  1386. * Try to read the ID string. The card then prints out the
  1387. * identification string ended by the "\n\x2e".
  1388. *
  1389. * The following loop is indexed through i (instead of id)
  1390. * to avoid looping forever when for any reason
  1391. * the port returns '\r', '\n' or '\x2e' permanently.
  1392. */
  1393. for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
  1394. if ((curr = get_wait_data(cosa)) == -1) {
  1395. return -1;
  1396. }
  1397. curr &= 0xff;
  1398. if (curr != '\r' && curr != '\n' && curr != 0x2e)
  1399. idstring[id++] = curr;
  1400. if (curr == 0x2e && prev == '\n')
  1401. break;
  1402. }
  1403. /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
  1404. idstring[id] = '\0';
  1405. return id;
  1406. }
  1407. /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
  1408. /*
  1409. * This routine gets the data byte from the card waiting for the SR_RX_RDY
  1410. * bit to be set in a loop. It should be used in the exceptional cases
  1411. * only (for example when resetting the card or downloading the firmware.
  1412. */
  1413. static int get_wait_data(struct cosa_data *cosa)
  1414. {
  1415. int retries = 1000;
  1416. while (--retries) {
  1417. /* read data and return them */
  1418. if (cosa_getstatus(cosa) & SR_RX_RDY) {
  1419. short r;
  1420. r = cosa_getdata8(cosa);
  1421. #if 0
  1422. printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
  1423. #endif
  1424. return r;
  1425. }
  1426. /* sleep if not ready to read */
  1427. schedule_timeout_interruptible(1);
  1428. }
  1429. printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
  1430. cosa_getstatus(cosa));
  1431. return -1;
  1432. }
  1433. /*
  1434. * This routine puts the data byte to the card waiting for the SR_TX_RDY
  1435. * bit to be set in a loop. It should be used in the exceptional cases
  1436. * only (for example when resetting the card or downloading the firmware).
  1437. */
  1438. static int put_wait_data(struct cosa_data *cosa, int data)
  1439. {
  1440. int retries = 1000;
  1441. while (--retries) {
  1442. /* read data and return them */
  1443. if (cosa_getstatus(cosa) & SR_TX_RDY) {
  1444. cosa_putdata8(cosa, data);
  1445. #if 0
  1446. printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
  1447. #endif
  1448. return 0;
  1449. }
  1450. #if 0
  1451. /* sleep if not ready to read */
  1452. schedule_timeout_interruptible(1);
  1453. #endif
  1454. }
  1455. printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
  1456. cosa->num, cosa_getstatus(cosa));
  1457. return -1;
  1458. }
  1459. /*
  1460. * The following routine puts the hexadecimal number into the SRP monitor
  1461. * and verifies the proper echo of the sent bytes. Returns 0 on success,
  1462. * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
  1463. * (-2,-4,-6,-8) means that reading echo failed.
  1464. */
  1465. static int puthexnumber(struct cosa_data *cosa, int number)
  1466. {
  1467. char temp[5];
  1468. int i;
  1469. /* Well, I should probably replace this by something faster. */
  1470. sprintf(temp, "%04X", number);
  1471. for (i=0; i<4; i++) {
  1472. if (put_wait_data(cosa, temp[i]) == -1) {
  1473. printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
  1474. cosa->num, i);
  1475. return -1-2*i;
  1476. }
  1477. if (get_wait_data(cosa) != temp[i]) {
  1478. printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
  1479. cosa->num, i);
  1480. return -2-2*i;
  1481. }
  1482. }
  1483. return 0;
  1484. }
  1485. /* ---------- Interrupt routines ---------- */
  1486. /*
  1487. * There are three types of interrupt:
  1488. * At the beginning of transmit - this handled is in tx_interrupt(),
  1489. * at the beginning of receive - it is in rx_interrupt() and
  1490. * at the end of transmit/receive - it is the eot_interrupt() function.
  1491. * These functions are multiplexed by cosa_interrupt() according to the
  1492. * COSA status byte. I have moved the rx/tx/eot interrupt handling into
  1493. * separate functions to make it more readable. These functions are inline,
  1494. * so there should be no overhead of function call.
  1495. *
  1496. * In the COSA bus-master mode, we need to tell the card the address of a
  1497. * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
  1498. * It's time to use the bottom half :-(
  1499. */
  1500. /*
  1501. * Transmit interrupt routine - called when COSA is willing to obtain
  1502. * data from the OS. The most tricky part of the routine is selection
  1503. * of channel we (OS) want to send packet for. For SRP we should probably
  1504. * use the round-robin approach. The newer COSA firmwares have a simple
  1505. * flow-control - in the status word has bits 2 and 3 set to 1 means that the
  1506. * channel 0 or 1 doesn't want to receive data.
  1507. *
  1508. * It seems there is a bug in COSA firmware (need to trace it further):
  1509. * When the driver status says that the kernel has no more data for transmit
  1510. * (e.g. at the end of TX DMA) and then the kernel changes its mind
  1511. * (e.g. new packet is queued to hard_start_xmit()), the card issues
  1512. * the TX interrupt but does not mark the channel as ready-to-transmit.
  1513. * The fix seems to be to push the packet to COSA despite its request.
  1514. * We first try to obey the card's opinion, and then fall back to forced TX.
  1515. */
  1516. static inline void tx_interrupt(struct cosa_data *cosa, int status)
  1517. {
  1518. unsigned long flags, flags1;
  1519. #ifdef DEBUG_IRQS
  1520. printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
  1521. cosa->num, status);
  1522. #endif
  1523. spin_lock_irqsave(&cosa->lock, flags);
  1524. set_bit(TXBIT, &cosa->rxtx);
  1525. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1526. /* flow control, see the comment above */
  1527. int i=0;
  1528. if (!cosa->txbitmap) {
  1529. printk(KERN_WARNING "%s: No channel wants data "
  1530. "in TX IRQ. Expect DMA timeout.",
  1531. cosa->name);
  1532. put_driver_status_nolock(cosa);
  1533. clear_bit(TXBIT, &cosa->rxtx);
  1534. spin_unlock_irqrestore(&cosa->lock, flags);
  1535. return;
  1536. }
  1537. while(1) {
  1538. cosa->txchan++;
  1539. i++;
  1540. if (cosa->txchan >= cosa->nchannels)
  1541. cosa->txchan = 0;
  1542. if (!(cosa->txbitmap & (1<<cosa->txchan)))
  1543. continue;
  1544. if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
  1545. break;
  1546. /* in second pass, accept first ready-to-TX channel */
  1547. if (i > cosa->nchannels) {
  1548. /* Can be safely ignored */
  1549. #ifdef DEBUG_IRQS
  1550. printk(KERN_DEBUG "%s: Forcing TX "
  1551. "to not-ready channel %d\n",
  1552. cosa->name, cosa->txchan);
  1553. #endif
  1554. break;
  1555. }
  1556. }
  1557. cosa->txsize = cosa->chan[cosa->txchan].txsize;
  1558. if (cosa_dma_able(cosa->chan+cosa->txchan,
  1559. cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
  1560. cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
  1561. } else {
  1562. memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
  1563. cosa->txsize);
  1564. cosa->txbuf = cosa->bouncebuf;
  1565. }
  1566. }
  1567. if (is_8bit(cosa)) {
  1568. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1569. cosa_putstatus(cosa, SR_TX_INT_ENA);
  1570. cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
  1571. ((cosa->txsize >> 8) & 0x1f));
  1572. #ifdef DEBUG_IO
  1573. debug_status_out(cosa, SR_TX_INT_ENA);
  1574. debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
  1575. ((cosa->txsize >> 8) & 0x1f));
  1576. debug_data_in(cosa, cosa_getdata8(cosa));
  1577. #else
  1578. cosa_getdata8(cosa);
  1579. #endif
  1580. set_bit(IRQBIT, &cosa->rxtx);
  1581. spin_unlock_irqrestore(&cosa->lock, flags);
  1582. return;
  1583. } else {
  1584. clear_bit(IRQBIT, &cosa->rxtx);
  1585. cosa_putstatus(cosa, 0);
  1586. cosa_putdata8(cosa, cosa->txsize&0xff);
  1587. #ifdef DEBUG_IO
  1588. debug_status_out(cosa, 0);
  1589. debug_data_out(cosa, cosa->txsize&0xff);
  1590. #endif
  1591. }
  1592. } else {
  1593. cosa_putstatus(cosa, SR_TX_INT_ENA);
  1594. cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
  1595. | (cosa->txsize & 0x1fff));
  1596. #ifdef DEBUG_IO
  1597. debug_status_out(cosa, SR_TX_INT_ENA);
  1598. debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
  1599. | (cosa->txsize & 0x1fff));
  1600. debug_data_in(cosa, cosa_getdata8(cosa));
  1601. debug_status_out(cosa, 0);
  1602. #else
  1603. cosa_getdata8(cosa);
  1604. #endif
  1605. cosa_putstatus(cosa, 0);
  1606. }
  1607. if (cosa->busmaster) {
  1608. unsigned long addr = virt_to_bus(cosa->txbuf);
  1609. int count=0;
  1610. printk(KERN_INFO "busmaster IRQ\n");
  1611. while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
  1612. count++;
  1613. udelay(10);
  1614. if (count > 1000) break;
  1615. }
  1616. printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
  1617. printk(KERN_INFO "ready after %d loops\n", count);
  1618. cosa_putdata16(cosa, (addr >> 16)&0xffff);
  1619. count = 0;
  1620. while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
  1621. count++;
  1622. if (count > 1000) break;
  1623. udelay(10);
  1624. }
  1625. printk(KERN_INFO "ready after %d loops\n", count);
  1626. cosa_putdata16(cosa, addr &0xffff);
  1627. flags1 = claim_dma_lock();
  1628. set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
  1629. enable_dma(cosa->dma);
  1630. release_dma_lock(flags1);
  1631. } else {
  1632. /* start the DMA */
  1633. flags1 = claim_dma_lock();
  1634. disable_dma(cosa->dma);
  1635. clear_dma_ff(cosa->dma);
  1636. set_dma_mode(cosa->dma, DMA_MODE_WRITE);
  1637. set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
  1638. set_dma_count(cosa->dma, cosa->txsize);
  1639. enable_dma(cosa->dma);
  1640. release_dma_lock(flags1);
  1641. }
  1642. cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
  1643. #ifdef DEBUG_IO
  1644. debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
  1645. #endif
  1646. spin_unlock_irqrestore(&cosa->lock, flags);
  1647. }
  1648. static inline void rx_interrupt(struct cosa_data *cosa, int status)
  1649. {
  1650. unsigned long flags;
  1651. #ifdef DEBUG_IRQS
  1652. printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
  1653. #endif
  1654. spin_lock_irqsave(&cosa->lock, flags);
  1655. set_bit(RXBIT, &cosa->rxtx);
  1656. if (is_8bit(cosa)) {
  1657. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1658. set_bit(IRQBIT, &cosa->rxtx);
  1659. put_driver_status_nolock(cosa);
  1660. cosa->rxsize = cosa_getdata8(cosa) <<8;
  1661. #ifdef DEBUG_IO
  1662. debug_data_in(cosa, cosa->rxsize >> 8);
  1663. #endif
  1664. spin_unlock_irqrestore(&cosa->lock, flags);
  1665. return;
  1666. } else {
  1667. clear_bit(IRQBIT, &cosa->rxtx);
  1668. cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
  1669. #ifdef DEBUG_IO
  1670. debug_data_in(cosa, cosa->rxsize & 0xff);
  1671. #endif
  1672. #if 0
  1673. printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
  1674. cosa->num, cosa->rxsize);
  1675. #endif
  1676. }
  1677. } else {
  1678. cosa->rxsize = cosa_getdata16(cosa);
  1679. #ifdef DEBUG_IO
  1680. debug_data_in(cosa, cosa->rxsize);
  1681. #endif
  1682. #if 0
  1683. printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
  1684. cosa->num, cosa->rxsize);
  1685. #endif
  1686. }
  1687. if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
  1688. printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
  1689. cosa->name, cosa->rxsize);
  1690. spin_unlock_irqrestore(&cosa->lock, flags);
  1691. goto reject;
  1692. }
  1693. cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
  1694. cosa->rxsize &= 0x1fff;
  1695. spin_unlock_irqrestore(&cosa->lock, flags);
  1696. cosa->rxbuf = NULL;
  1697. if (cosa->rxchan->setup_rx)
  1698. cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
  1699. if (!cosa->rxbuf) {
  1700. reject: /* Reject the packet */
  1701. printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
  1702. cosa->num, cosa->rxchan->num);
  1703. cosa->rxbuf = cosa->bouncebuf;
  1704. }
  1705. /* start the DMA */
  1706. flags = claim_dma_lock();
  1707. disable_dma(cosa->dma);
  1708. clear_dma_ff(cosa->dma);
  1709. set_dma_mode(cosa->dma, DMA_MODE_READ);
  1710. if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
  1711. set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
  1712. } else {
  1713. set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
  1714. }
  1715. set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
  1716. enable_dma(cosa->dma);
  1717. release_dma_lock(flags);
  1718. spin_lock_irqsave(&cosa->lock, flags);
  1719. cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
  1720. if (!is_8bit(cosa) && (status & SR_TX_RDY))
  1721. cosa_putdata8(cosa, DRIVER_RX_READY);
  1722. #ifdef DEBUG_IO
  1723. debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
  1724. if (!is_8bit(cosa) && (status & SR_TX_RDY))
  1725. debug_data_cmd(cosa, DRIVER_RX_READY);
  1726. #endif
  1727. spin_unlock_irqrestore(&cosa->lock, flags);
  1728. }
  1729. static inline void eot_interrupt(struct cosa_data *cosa, int status)
  1730. {
  1731. unsigned long flags, flags1;
  1732. spin_lock_irqsave(&cosa->lock, flags);
  1733. flags1 = claim_dma_lock();
  1734. disable_dma(cosa->dma);
  1735. clear_dma_ff(cosa->dma);
  1736. release_dma_lock(flags1);
  1737. if (test_bit(TXBIT, &cosa->rxtx)) {
  1738. struct channel_data *chan = cosa->chan+cosa->txchan;
  1739. if (chan->tx_done)
  1740. if (chan->tx_done(chan, cosa->txsize))
  1741. clear_bit(chan->num, &cosa->txbitmap);
  1742. } else if (test_bit(RXBIT, &cosa->rxtx)) {
  1743. #ifdef DEBUG_DATA
  1744. {
  1745. int i;
  1746. printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
  1747. cosa->rxchan->num, cosa->rxsize);
  1748. for (i=0; i<cosa->rxsize; i++)
  1749. printk (" %02x", cosa->rxbuf[i]&0xff);
  1750. printk("\n");
  1751. }
  1752. #endif
  1753. /* Packet for unknown channel? */
  1754. if (cosa->rxbuf == cosa->bouncebuf)
  1755. goto out;
  1756. if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
  1757. memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
  1758. if (cosa->rxchan->rx_done)
  1759. if (cosa->rxchan->rx_done(cosa->rxchan))
  1760. clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
  1761. } else {
  1762. printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
  1763. cosa->num);
  1764. }
  1765. /*
  1766. * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
  1767. * cleared anyway). We should do it as soon as possible
  1768. * so that we can tell the COSA we are done and to give it a time
  1769. * for recovery.
  1770. */
  1771. out:
  1772. cosa->rxtx = 0;
  1773. put_driver_status_nolock(cosa);
  1774. spin_unlock_irqrestore(&cosa->lock, flags);
  1775. }
  1776. static irqreturn_t cosa_interrupt(int irq, void *cosa_)
  1777. {
  1778. unsigned status;
  1779. int count = 0;
  1780. struct cosa_data *cosa = cosa_;
  1781. again:
  1782. status = cosa_getstatus(cosa);
  1783. #ifdef DEBUG_IRQS
  1784. printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
  1785. status & 0xff);
  1786. #endif
  1787. #ifdef DEBUG_IO
  1788. debug_status_in(cosa, status);
  1789. #endif
  1790. switch (status & SR_CMD_FROM_SRP_MASK) {
  1791. case SR_DOWN_REQUEST:
  1792. tx_interrupt(cosa, status);
  1793. break;
  1794. case SR_UP_REQUEST:
  1795. rx_interrupt(cosa, status);
  1796. break;
  1797. case SR_END_OF_TRANSFER:
  1798. eot_interrupt(cosa, status);
  1799. break;
  1800. default:
  1801. /* We may be too fast for SRP. Try to wait a bit more. */
  1802. if (count++ < 100) {
  1803. udelay(100);
  1804. goto again;
  1805. }
  1806. printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
  1807. cosa->num, status & 0xff, count);
  1808. }
  1809. #ifdef DEBUG_IRQS
  1810. if (count)
  1811. printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
  1812. cosa->name, count);
  1813. else
  1814. printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
  1815. #endif
  1816. return IRQ_HANDLED;
  1817. }
  1818. /* ---------- I/O debugging routines ---------- */
  1819. /*
  1820. * These routines can be used to monitor COSA/SRP I/O and to printk()
  1821. * the data being transferred on the data and status I/O port in a
  1822. * readable way.
  1823. */
  1824. #ifdef DEBUG_IO
  1825. static void debug_status_in(struct cosa_data *cosa, int status)
  1826. {
  1827. char *s;
  1828. switch(status & SR_CMD_FROM_SRP_MASK) {
  1829. case SR_UP_REQUEST:
  1830. s = "RX_REQ";
  1831. break;
  1832. case SR_DOWN_REQUEST:
  1833. s = "TX_REQ";
  1834. break;
  1835. case SR_END_OF_TRANSFER:
  1836. s = "ET_REQ";
  1837. break;
  1838. default:
  1839. s = "NO_REQ";
  1840. break;
  1841. }
  1842. printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
  1843. cosa->name,
  1844. status,
  1845. status & SR_USR_RQ ? "USR_RQ|":"",
  1846. status & SR_TX_RDY ? "TX_RDY|":"",
  1847. status & SR_RX_RDY ? "RX_RDY|":"",
  1848. s);
  1849. }
  1850. static void debug_status_out(struct cosa_data *cosa, int status)
  1851. {
  1852. printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
  1853. cosa->name,
  1854. status,
  1855. status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
  1856. status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
  1857. status & SR_RST ? "RESET|":"",
  1858. status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
  1859. status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
  1860. status & SR_RX_INT_ENA ? "RXINT":"!rxint");
  1861. }
  1862. static void debug_data_in(struct cosa_data *cosa, int data)
  1863. {
  1864. printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
  1865. }
  1866. static void debug_data_out(struct cosa_data *cosa, int data)
  1867. {
  1868. printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
  1869. }
  1870. static void debug_data_cmd(struct cosa_data *cosa, int data)
  1871. {
  1872. printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
  1873. cosa->name, data,
  1874. data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
  1875. data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
  1876. }
  1877. #endif
  1878. /* EOF -- this file has not been truncated */