cosa.c 59 KB

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