cosa.c 57 KB

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