cosa.c 57 KB

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