/kern_oII/drivers/net/wan/cosa.c

http://omnia2droid.googlecode.com/
C | 2059 lines | 1550 code | 204 blank | 305 comment | 284 complexity | 9a9f17898b070cbff7c9f9cd1b6d2d94 MD5 | raw file

✨ Summary

Large files are truncated, but you can click here to view the full file

  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. #include <linux/module.h>
  75. #include <linux/kernel.h>
  76. #include <linux/slab.h>
  77. #include <linux/poll.h>
  78. #include <linux/fs.h>
  79. #include <linux/interrupt.h>
  80. #include <linux/delay.h>
  81. #include <linux/hdlc.h>
  82. #include <linux/errno.h>
  83. #include <linux/ioport.h>
  84. #include <linux/netdevice.h>
  85. #include <linux/spinlock.h>
  86. #include <linux/mutex.h>
  87. #include <linux/device.h>
  88. #include <linux/smp_lock.h>
  89. #include <asm/io.h>
  90. #include <asm/dma.h>
  91. #include <asm/byteorder.h>
  92. #undef COSA_SLOW_IO /* for testing purposes only */
  93. #include "cosa.h"
  94. /* Maximum length of the identification string. */
  95. #define COSA_MAX_ID_STRING 128
  96. /* Maximum length of the channel name */
  97. #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
  98. /* Per-channel data structure */
  99. struct channel_data {
  100. int usage; /* Usage count; >0 for chrdev, -1 for netdev */
  101. int num; /* Number of the channel */
  102. struct cosa_data *cosa; /* Pointer to the per-card structure */
  103. int txsize; /* Size of transmitted data */
  104. char *txbuf; /* Transmit buffer */
  105. char name[COSA_MAX_NAME]; /* channel name */
  106. /* The HW layer interface */
  107. /* routine called from the RX interrupt */
  108. char *(*setup_rx)(struct channel_data *channel, int size);
  109. /* routine called when the RX is done (from the EOT interrupt) */
  110. int (*rx_done)(struct channel_data *channel);
  111. /* routine called when the TX is done (from the EOT interrupt) */
  112. int (*tx_done)(struct channel_data *channel, int size);
  113. /* Character device parts */
  114. struct mutex rlock;
  115. struct semaphore wsem;
  116. char *rxdata;
  117. int rxsize;
  118. wait_queue_head_t txwaitq, rxwaitq;
  119. int tx_status, rx_status;
  120. /* generic HDLC device parts */
  121. struct net_device *netdev;
  122. struct sk_buff *rx_skb, *tx_skb;
  123. };
  124. /* cosa->firmware_status bits */
  125. #define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
  126. #define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
  127. #define COSA_FW_START (1<<2) /* Is the microcode running? */
  128. struct cosa_data {
  129. int num; /* Card number */
  130. char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
  131. unsigned int datareg, statusreg; /* I/O ports */
  132. unsigned short irq, dma; /* IRQ and DMA number */
  133. unsigned short startaddr; /* Firmware start address */
  134. unsigned short busmaster; /* Use busmastering? */
  135. int nchannels; /* # of channels on this card */
  136. int driver_status; /* For communicating with firmware */
  137. int firmware_status; /* Downloaded, reseted, etc. */
  138. unsigned long rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
  139. unsigned long rxtx; /* RX or TX in progress? */
  140. int enabled;
  141. int usage; /* usage count */
  142. int txchan, txsize, rxsize;
  143. struct channel_data *rxchan;
  144. char *bouncebuf;
  145. char *txbuf, *rxbuf;
  146. struct channel_data *chan;
  147. spinlock_t lock; /* For exclusive operations on this structure */
  148. char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
  149. char *type; /* card type */
  150. };
  151. /*
  152. * Define this if you want all the possible ports to be autoprobed.
  153. * It is here but it probably is not a good idea to use this.
  154. */
  155. /* #define COSA_ISA_AUTOPROBE 1 */
  156. /*
  157. * Character device major number. 117 was allocated for us.
  158. * The value of 0 means to allocate a first free one.
  159. */
  160. static int cosa_major = 117;
  161. /*
  162. * Encoding of the minor numbers:
  163. * The lowest CARD_MINOR_BITS bits means the channel on the single card,
  164. * the highest bits means the card number.
  165. */
  166. #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
  167. * for the single card */
  168. /*
  169. * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
  170. * macro doesn't like anything other than the raw number as an argument :-(
  171. */
  172. #define MAX_CARDS 16
  173. /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
  174. #define DRIVER_RX_READY 0x0001
  175. #define DRIVER_TX_READY 0x0002
  176. #define DRIVER_TXMAP_SHIFT 2
  177. #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
  178. /*
  179. * for cosa->rxtx - indicates whether either transmit or receive is
  180. * in progress. These values are mean number of the bit.
  181. */
  182. #define TXBIT 0
  183. #define RXBIT 1
  184. #define IRQBIT 2
  185. #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
  186. #undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
  187. #undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
  188. #undef DEBUG_IO //1 /* Dump the I/O traffic */
  189. #define TX_TIMEOUT (5*HZ)
  190. /* Maybe the following should be allocated dynamically */
  191. static struct cosa_data cosa_cards[MAX_CARDS];
  192. static int nr_cards;
  193. #ifdef COSA_ISA_AUTOPROBE
  194. static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
  195. /* NOTE: DMA is not autoprobed!!! */
  196. static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
  197. #else
  198. static int io[MAX_CARDS+1];
  199. static int dma[MAX_CARDS+1];
  200. #endif
  201. /* IRQ can be safely autoprobed */
  202. static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
  203. /* for class stuff*/
  204. static struct class *cosa_class;
  205. #ifdef MODULE
  206. module_param_array(io, int, NULL, 0);
  207. MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
  208. module_param_array(irq, int, NULL, 0);
  209. MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
  210. module_param_array(dma, int, NULL, 0);
  211. MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
  212. MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
  213. MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
  214. MODULE_LICENSE("GPL");
  215. #endif
  216. /* I use this mainly for testing purposes */
  217. #ifdef COSA_SLOW_IO
  218. #define cosa_outb outb_p
  219. #define cosa_outw outw_p
  220. #define cosa_inb inb_p
  221. #define cosa_inw inw_p
  222. #else
  223. #define cosa_outb outb
  224. #define cosa_outw outw
  225. #define cosa_inb inb
  226. #define cosa_inw inw
  227. #endif
  228. #define is_8bit(cosa) (!(cosa->datareg & 0x08))
  229. #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
  230. #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
  231. #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
  232. #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
  233. #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
  234. #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
  235. /* Initialization stuff */
  236. static int cosa_probe(int ioaddr, int irq, int dma);
  237. /* HW interface */
  238. static void cosa_enable_rx(struct channel_data *chan);
  239. static void cosa_disable_rx(struct channel_data *chan);
  240. static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
  241. static void cosa_kick(struct cosa_data *cosa);
  242. static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
  243. /* Network device stuff */
  244. static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
  245. unsigned short parity);
  246. static int cosa_net_open(struct net_device *d);
  247. static int cosa_net_close(struct net_device *d);
  248. static void cosa_net_timeout(struct net_device *d);
  249. static int cosa_net_tx(struct sk_buff *skb, struct net_device *d);
  250. static char *cosa_net_setup_rx(struct channel_data *channel, int size);
  251. static int cosa_net_rx_done(struct channel_data *channel);
  252. static int cosa_net_tx_done(struct channel_data *channel, int size);
  253. static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  254. /* Character device */
  255. static char *chrdev_setup_rx(struct channel_data *channel, int size);
  256. static int chrdev_rx_done(struct channel_data *channel);
  257. static int chrdev_tx_done(struct channel_data *channel, int size);
  258. static ssize_t cosa_read(struct file *file,
  259. char __user *buf, size_t count, loff_t *ppos);
  260. static ssize_t cosa_write(struct file *file,
  261. const char __user *buf, size_t count, loff_t *ppos);
  262. static unsigned int cosa_poll(struct file *file, poll_table *poll);
  263. static int cosa_open(struct inode *inode, struct file *file);
  264. static int cosa_release(struct inode *inode, struct file *file);
  265. static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
  266. unsigned int cmd, unsigned long arg);
  267. #ifdef COSA_FASYNC_WORKING
  268. static int cosa_fasync(struct inode *inode, struct file *file, int on);
  269. #endif
  270. static const struct file_operations cosa_fops = {
  271. .owner = THIS_MODULE,
  272. .llseek = no_llseek,
  273. .read = cosa_read,
  274. .write = cosa_write,
  275. .poll = cosa_poll,
  276. .ioctl = cosa_chardev_ioctl,
  277. .open = cosa_open,
  278. .release = cosa_release,
  279. #ifdef COSA_FASYNC_WORKING
  280. .fasync = cosa_fasync,
  281. #endif
  282. };
  283. /* Ioctls */
  284. static int cosa_start(struct cosa_data *cosa, int address);
  285. static int cosa_reset(struct cosa_data *cosa);
  286. static int cosa_download(struct cosa_data *cosa, void __user *a);
  287. static int cosa_readmem(struct cosa_data *cosa, void __user *a);
  288. /* COSA/SRP ROM monitor */
  289. static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
  290. static int startmicrocode(struct cosa_data *cosa, int address);
  291. static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
  292. static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
  293. /* Auxilliary functions */
  294. static int get_wait_data(struct cosa_data *cosa);
  295. static int put_wait_data(struct cosa_data *cosa, int data);
  296. static int puthexnumber(struct cosa_data *cosa, int number);
  297. static void put_driver_status(struct cosa_data *cosa);
  298. static void put_driver_status_nolock(struct cosa_data *cosa);
  299. /* Interrupt handling */
  300. static irqreturn_t cosa_interrupt(int irq, void *cosa);
  301. /* I/O ops debugging */
  302. #ifdef DEBUG_IO
  303. static void debug_data_in(struct cosa_data *cosa, int data);
  304. static void debug_data_out(struct cosa_data *cosa, int data);
  305. static void debug_data_cmd(struct cosa_data *cosa, int data);
  306. static void debug_status_in(struct cosa_data *cosa, int status);
  307. static void debug_status_out(struct cosa_data *cosa, int status);
  308. #endif
  309. static inline struct channel_data* dev_to_chan(struct net_device *dev)
  310. {
  311. return (struct channel_data *)dev_to_hdlc(dev)->priv;
  312. }
  313. /* ---------- Initialization stuff ---------- */
  314. static int __init cosa_init(void)
  315. {
  316. int i, err = 0;
  317. if (cosa_major > 0) {
  318. if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
  319. printk(KERN_WARNING "cosa: unable to get major %d\n",
  320. cosa_major);
  321. err = -EIO;
  322. goto out;
  323. }
  324. } else {
  325. if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
  326. printk(KERN_WARNING "cosa: unable to register chardev\n");
  327. err = -EIO;
  328. goto out;
  329. }
  330. }
  331. for (i=0; i<MAX_CARDS; i++)
  332. cosa_cards[i].num = -1;
  333. for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
  334. cosa_probe(io[i], irq[i], dma[i]);
  335. if (!nr_cards) {
  336. printk(KERN_WARNING "cosa: no devices found.\n");
  337. unregister_chrdev(cosa_major, "cosa");
  338. err = -ENODEV;
  339. goto out;
  340. }
  341. cosa_class = class_create(THIS_MODULE, "cosa");
  342. if (IS_ERR(cosa_class)) {
  343. err = PTR_ERR(cosa_class);
  344. goto out_chrdev;
  345. }
  346. for (i = 0; i < nr_cards; i++)
  347. device_create(cosa_class, NULL, MKDEV(cosa_major, i), NULL,
  348. "cosa%d", i);
  349. err = 0;
  350. goto out;
  351. out_chrdev:
  352. unregister_chrdev(cosa_major, "cosa");
  353. out:
  354. return err;
  355. }
  356. module_init(cosa_init);
  357. static void __exit cosa_exit(void)
  358. {
  359. struct cosa_data *cosa;
  360. int i;
  361. for (i = 0; i < nr_cards; i++)
  362. device_destroy(cosa_class, MKDEV(cosa_major, i));
  363. class_destroy(cosa_class);
  364. for (cosa = cosa_cards; nr_cards--; cosa++) {
  365. /* Clean up the per-channel data */
  366. for (i = 0; i < cosa->nchannels; i++) {
  367. /* Chardev driver has no alloc'd per-channel data */
  368. unregister_hdlc_device(cosa->chan[i].netdev);
  369. free_netdev(cosa->chan[i].netdev);
  370. }
  371. /* Clean up the per-card data */
  372. kfree(cosa->chan);
  373. kfree(cosa->bouncebuf);
  374. free_irq(cosa->irq, cosa);
  375. free_dma(cosa->dma);
  376. release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
  377. }
  378. unregister_chrdev(cosa_major, "cosa");
  379. }
  380. module_exit(cosa_exit);
  381. static const struct net_device_ops cosa_ops = {
  382. .ndo_open = cosa_net_open,
  383. .ndo_stop = cosa_net_close,
  384. .ndo_change_mtu = hdlc_change_mtu,
  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. printk (KERN_INFO "cosa_probe: 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. printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
  404. base);
  405. return -1;
  406. }
  407. /* DMA should be 0,1 or 3-7 */
  408. if (dma < 0 || dma == 4 || dma > 7) {
  409. printk (KERN_INFO "cosa_probe: 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. printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
  416. " (base=0x%x, dma=%d)\n", 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 "cosa: 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. printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
  439. base);
  440. #endif
  441. err = -1;
  442. goto err_out;
  443. }
  444. /* Update the name of the region now we know the type of card */
  445. release_region(base, is_8bit(cosa)?2:4);
  446. if (!request_region(base, is_8bit(cosa)?2:4, cosa->type)) {
  447. printk(KERN_DEBUG "cosa: changing name at 0x%x failed.\n", base);
  448. return -1;
  449. }
  450. /* Now do IRQ autoprobe */
  451. if (irq < 0) {
  452. unsigned long irqs;
  453. /* printk(KERN_INFO "IRQ autoprobe\n"); */
  454. irqs = probe_irq_on();
  455. /*
  456. * Enable interrupt on tx buffer empty (it sure is)
  457. * really sure ?
  458. * FIXME: When this code is not used as module, we should
  459. * probably call udelay() instead of the interruptible sleep.
  460. */
  461. set_current_state(TASK_INTERRUPTIBLE);
  462. cosa_putstatus(cosa, SR_TX_INT_ENA);
  463. schedule_timeout(30);
  464. irq = probe_irq_off(irqs);
  465. /* Disable all IRQs from the card */
  466. cosa_putstatus(cosa, 0);
  467. /* Empty the received data register */
  468. cosa_getdata8(cosa);
  469. if (irq < 0) {
  470. printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
  471. irq, cosa->datareg);
  472. err = -1;
  473. goto err_out;
  474. }
  475. if (irq == 0) {
  476. printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
  477. cosa->datareg);
  478. /* return -1; */
  479. }
  480. }
  481. cosa->irq = irq;
  482. cosa->num = nr_cards;
  483. cosa->usage = 0;
  484. cosa->nchannels = 2; /* FIXME: how to determine this? */
  485. if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
  486. err = -1;
  487. goto err_out;
  488. }
  489. if (request_dma(cosa->dma, cosa->type)) {
  490. err = -1;
  491. goto err_out1;
  492. }
  493. cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
  494. if (!cosa->bouncebuf) {
  495. err = -ENOMEM;
  496. goto err_out2;
  497. }
  498. sprintf(cosa->name, "cosa%d", cosa->num);
  499. /* Initialize the per-channel data */
  500. cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
  501. if (!cosa->chan) {
  502. err = -ENOMEM;
  503. goto err_out3;
  504. }
  505. for (i = 0; i < cosa->nchannels; i++) {
  506. struct channel_data *chan = &cosa->chan[i];
  507. chan->cosa = cosa;
  508. chan->num = i;
  509. sprintf(chan->name, "cosa%dc%d", chan->cosa->num, i);
  510. /* Initialize the chardev data structures */
  511. mutex_init(&chan->rlock);
  512. init_MUTEX(&chan->wsem);
  513. /* Register the network interface */
  514. if (!(chan->netdev = alloc_hdlcdev(chan))) {
  515. printk(KERN_WARNING "%s: alloc_hdlcdev failed.\n",
  516. chan->name);
  517. goto err_hdlcdev;
  518. }
  519. dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
  520. dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
  521. chan->netdev->netdev_ops = &cosa_ops;
  522. chan->netdev->watchdog_timeo = TX_TIMEOUT;
  523. chan->netdev->base_addr = chan->cosa->datareg;
  524. chan->netdev->irq = chan->cosa->irq;
  525. chan->netdev->dma = chan->cosa->dma;
  526. if (register_hdlc_device(chan->netdev)) {
  527. printk(KERN_WARNING "%s: register_hdlc_device()"
  528. " failed.\n", chan->netdev->name);
  529. free_netdev(chan->netdev);
  530. goto err_hdlcdev;
  531. }
  532. }
  533. printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
  534. cosa->num, cosa->id_string, cosa->type,
  535. cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
  536. return nr_cards++;
  537. err_hdlcdev:
  538. while (i-- > 0) {
  539. unregister_hdlc_device(cosa->chan[i].netdev);
  540. free_netdev(cosa->chan[i].netdev);
  541. }
  542. kfree(cosa->chan);
  543. err_out3:
  544. kfree(cosa->bouncebuf);
  545. err_out2:
  546. free_dma(cosa->dma);
  547. err_out1:
  548. free_irq(cosa->irq, cosa);
  549. err_out:
  550. release_region(cosa->datareg,is_8bit(cosa)?2:4);
  551. printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
  552. cosa->num);
  553. return err;
  554. }
  555. /*---------- network device ---------- */
  556. static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
  557. unsigned short parity)
  558. {
  559. if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
  560. return 0;
  561. return -EINVAL;
  562. }
  563. static int cosa_net_open(struct net_device *dev)
  564. {
  565. struct channel_data *chan = dev_to_chan(dev);
  566. int err;
  567. unsigned long flags;
  568. if (!(chan->cosa->firmware_status & COSA_FW_START)) {
  569. printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
  570. chan->cosa->name, chan->cosa->firmware_status);
  571. return -EPERM;
  572. }
  573. spin_lock_irqsave(&chan->cosa->lock, flags);
  574. if (chan->usage != 0) {
  575. printk(KERN_WARNING "%s: cosa_net_open called with usage count"
  576. " %d\n", chan->name, chan->usage);
  577. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  578. return -EBUSY;
  579. }
  580. chan->setup_rx = cosa_net_setup_rx;
  581. chan->tx_done = cosa_net_tx_done;
  582. chan->rx_done = cosa_net_rx_done;
  583. chan->usage = -1;
  584. chan->cosa->usage++;
  585. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  586. err = hdlc_open(dev);
  587. if (err) {
  588. spin_lock_irqsave(&chan->cosa->lock, flags);
  589. chan->usage = 0;
  590. chan->cosa->usage--;
  591. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  592. return err;
  593. }
  594. netif_start_queue(dev);
  595. cosa_enable_rx(chan);
  596. return 0;
  597. }
  598. static int cosa_net_tx(struct sk_buff *skb, struct net_device *dev)
  599. {
  600. struct channel_data *chan = dev_to_chan(dev);
  601. netif_stop_queue(dev);
  602. chan->tx_skb = skb;
  603. cosa_start_tx(chan, skb->data, skb->len);
  604. return 0;
  605. }
  606. static void cosa_net_timeout(struct net_device *dev)
  607. {
  608. struct channel_data *chan = dev_to_chan(dev);
  609. if (test_bit(RXBIT, &chan->cosa->rxtx)) {
  610. chan->netdev->stats.rx_errors++;
  611. chan->netdev->stats.rx_missed_errors++;
  612. } else {
  613. chan->netdev->stats.tx_errors++;
  614. chan->netdev->stats.tx_aborted_errors++;
  615. }
  616. cosa_kick(chan->cosa);
  617. if (chan->tx_skb) {
  618. dev_kfree_skb(chan->tx_skb);
  619. chan->tx_skb = NULL;
  620. }
  621. netif_wake_queue(dev);
  622. }
  623. static int cosa_net_close(struct net_device *dev)
  624. {
  625. struct channel_data *chan = dev_to_chan(dev);
  626. unsigned long flags;
  627. netif_stop_queue(dev);
  628. hdlc_close(dev);
  629. cosa_disable_rx(chan);
  630. spin_lock_irqsave(&chan->cosa->lock, flags);
  631. if (chan->rx_skb) {
  632. kfree_skb(chan->rx_skb);
  633. chan->rx_skb = NULL;
  634. }
  635. if (chan->tx_skb) {
  636. kfree_skb(chan->tx_skb);
  637. chan->tx_skb = NULL;
  638. }
  639. chan->usage = 0;
  640. chan->cosa->usage--;
  641. spin_unlock_irqrestore(&chan->cosa->lock, flags);
  642. return 0;
  643. }
  644. static char *cosa_net_setup_rx(struct channel_data *chan, int size)
  645. {
  646. /*
  647. * We can safely fall back to non-dma-able memory, because we have
  648. * the cosa->bouncebuf pre-allocated.
  649. */
  650. kfree_skb(chan->rx_skb);
  651. chan->rx_skb = dev_alloc_skb(size);
  652. if (chan->rx_skb == NULL) {
  653. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
  654. chan->name);
  655. chan->netdev->stats.rx_dropped++;
  656. return NULL;
  657. }
  658. chan->netdev->trans_start = jiffies;
  659. return skb_put(chan->rx_skb, size);
  660. }
  661. static int cosa_net_rx_done(struct channel_data *chan)
  662. {
  663. if (!chan->rx_skb) {
  664. printk(KERN_WARNING "%s: rx_done with empty skb!\n",
  665. chan->name);
  666. chan->netdev->stats.rx_errors++;
  667. chan->netdev->stats.rx_frame_errors++;
  668. return 0;
  669. }
  670. chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
  671. chan->rx_skb->dev = chan->netdev;
  672. skb_reset_mac_header(chan->rx_skb);
  673. chan->netdev->stats.rx_packets++;
  674. chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
  675. netif_rx(chan->rx_skb);
  676. chan->rx_skb = NULL;
  677. return 0;
  678. }
  679. /* ARGSUSED */
  680. static int cosa_net_tx_done(struct channel_data *chan, int size)
  681. {
  682. if (!chan->tx_skb) {
  683. printk(KERN_WARNING "%s: tx_done with empty skb!\n",
  684. chan->name);
  685. chan->netdev->stats.tx_errors++;
  686. chan->netdev->stats.tx_aborted_errors++;
  687. return 1;
  688. }
  689. dev_kfree_skb_irq(chan->tx_skb);
  690. chan->tx_skb = NULL;
  691. chan->netdev->stats.tx_packets++;
  692. chan->netdev->stats.tx_bytes += size;
  693. netif_wake_queue(chan->netdev);
  694. return 1;
  695. }
  696. /*---------- Character device ---------- */
  697. static ssize_t cosa_read(struct file *file,
  698. char __user *buf, size_t count, loff_t *ppos)
  699. {
  700. DECLARE_WAITQUEUE(wait, current);
  701. unsigned long flags;
  702. struct channel_data *chan = file->private_data;
  703. struct cosa_data *cosa = chan->cosa;
  704. char *kbuf;
  705. if (!(cosa->firmware_status & COSA_FW_START)) {
  706. printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
  707. cosa->name, cosa->firmware_status);
  708. return -EPERM;
  709. }
  710. if (mutex_lock_interruptible(&chan->rlock))
  711. return -ERESTARTSYS;
  712. if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
  713. printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
  714. mutex_unlock(&chan->rlock);
  715. return -ENOMEM;
  716. }
  717. chan->rx_status = 0;
  718. cosa_enable_rx(chan);
  719. spin_lock_irqsave(&cosa->lock, flags);
  720. add_wait_queue(&chan->rxwaitq, &wait);
  721. while(!chan->rx_status) {
  722. current->state = TASK_INTERRUPTIBLE;
  723. spin_unlock_irqrestore(&cosa->lock, flags);
  724. schedule();
  725. spin_lock_irqsave(&cosa->lock, flags);
  726. if (signal_pending(current) && chan->rx_status == 0) {
  727. chan->rx_status = 1;
  728. remove_wait_queue(&chan->rxwaitq, &wait);
  729. current->state = TASK_RUNNING;
  730. spin_unlock_irqrestore(&cosa->lock, flags);
  731. mutex_unlock(&chan->rlock);
  732. return -ERESTARTSYS;
  733. }
  734. }
  735. remove_wait_queue(&chan->rxwaitq, &wait);
  736. current->state = TASK_RUNNING;
  737. kbuf = chan->rxdata;
  738. count = chan->rxsize;
  739. spin_unlock_irqrestore(&cosa->lock, flags);
  740. mutex_unlock(&chan->rlock);
  741. if (copy_to_user(buf, kbuf, count)) {
  742. kfree(kbuf);
  743. return -EFAULT;
  744. }
  745. kfree(kbuf);
  746. return count;
  747. }
  748. static char *chrdev_setup_rx(struct channel_data *chan, int size)
  749. {
  750. /* Expect size <= COSA_MTU */
  751. chan->rxsize = size;
  752. return chan->rxdata;
  753. }
  754. static int chrdev_rx_done(struct channel_data *chan)
  755. {
  756. if (chan->rx_status) { /* Reader has died */
  757. kfree(chan->rxdata);
  758. up(&chan->wsem);
  759. }
  760. chan->rx_status = 1;
  761. wake_up_interruptible(&chan->rxwaitq);
  762. return 1;
  763. }
  764. static ssize_t cosa_write(struct file *file,
  765. const char __user *buf, size_t count, loff_t *ppos)
  766. {
  767. DECLARE_WAITQUEUE(wait, current);
  768. struct channel_data *chan = file->private_data;
  769. struct cosa_data *cosa = chan->cosa;
  770. unsigned long flags;
  771. char *kbuf;
  772. if (!(cosa->firmware_status & COSA_FW_START)) {
  773. printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
  774. cosa->name, cosa->firmware_status);
  775. return -EPERM;
  776. }
  777. if (down_interruptible(&chan->wsem))
  778. return -ERESTARTSYS;
  779. if (count > COSA_MTU)
  780. count = COSA_MTU;
  781. /* Allocate the buffer */
  782. if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
  783. printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
  784. cosa->name);
  785. up(&chan->wsem);
  786. return -ENOMEM;
  787. }
  788. if (copy_from_user(kbuf, buf, count)) {
  789. up(&chan->wsem);
  790. kfree(kbuf);
  791. return -EFAULT;
  792. }
  793. chan->tx_status=0;
  794. cosa_start_tx(chan, kbuf, count);
  795. spin_lock_irqsave(&cosa->lock, flags);
  796. add_wait_queue(&chan->txwaitq, &wait);
  797. while(!chan->tx_status) {
  798. current->state = TASK_INTERRUPTIBLE;
  799. spin_unlock_irqrestore(&cosa->lock, flags);
  800. schedule();
  801. spin_lock_irqsave(&cosa->lock, flags);
  802. if (signal_pending(current) && chan->tx_status == 0) {
  803. chan->tx_status = 1;
  804. remove_wait_queue(&chan->txwaitq, &wait);
  805. current->state = TASK_RUNNING;
  806. chan->tx_status = 1;
  807. spin_unlock_irqrestore(&cosa->lock, flags);
  808. return -ERESTARTSYS;
  809. }
  810. }
  811. remove_wait_queue(&chan->txwaitq, &wait);
  812. current->state = TASK_RUNNING;
  813. up(&chan->wsem);
  814. spin_unlock_irqrestore(&cosa->lock, flags);
  815. kfree(kbuf);
  816. return count;
  817. }
  818. static int chrdev_tx_done(struct channel_data *chan, int size)
  819. {
  820. if (chan->tx_status) { /* Writer was interrupted */
  821. kfree(chan->txbuf);
  822. up(&chan->wsem);
  823. }
  824. chan->tx_status = 1;
  825. wake_up_interruptible(&chan->txwaitq);
  826. return 1;
  827. }
  828. static unsigned int cosa_poll(struct file *file, poll_table *poll)
  829. {
  830. printk(KERN_INFO "cosa_poll is here\n");
  831. return 0;
  832. }
  833. static int cosa_open(struct inode *inode, struct file *file)
  834. {
  835. struct cosa_data *cosa;
  836. struct channel_data *chan;
  837. unsigned long flags;
  838. int n;
  839. int ret = 0;
  840. lock_kernel();
  841. if ((n=iminor(file->f_path.dentry->d_inode)>>CARD_MINOR_BITS)
  842. >= nr_cards) {
  843. ret = -ENODEV;
  844. goto out;
  845. }
  846. cosa = cosa_cards+n;
  847. if ((n=iminor(file->f_path.dentry->d_inode)
  848. & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
  849. ret = -ENODEV;
  850. goto out;
  851. }
  852. chan = cosa->chan + n;
  853. file->private_data = chan;
  854. spin_lock_irqsave(&cosa->lock, flags);
  855. if (chan->usage < 0) { /* in netdev mode */
  856. spin_unlock_irqrestore(&cosa->lock, flags);
  857. ret = -EBUSY;
  858. goto out;
  859. }
  860. cosa->usage++;
  861. chan->usage++;
  862. chan->tx_done = chrdev_tx_done;
  863. chan->setup_rx = chrdev_setup_rx;
  864. chan->rx_done = chrdev_rx_done;
  865. spin_unlock_irqrestore(&cosa->lock, flags);
  866. out:
  867. unlock_kernel();
  868. return ret;
  869. }
  870. static int cosa_release(struct inode *inode, struct file *file)
  871. {
  872. struct channel_data *channel = file->private_data;
  873. struct cosa_data *cosa;
  874. unsigned long flags;
  875. cosa = channel->cosa;
  876. spin_lock_irqsave(&cosa->lock, flags);
  877. cosa->usage--;
  878. channel->usage--;
  879. spin_unlock_irqrestore(&cosa->lock, flags);
  880. return 0;
  881. }
  882. #ifdef COSA_FASYNC_WORKING
  883. static struct fasync_struct *fasync[256] = { NULL, };
  884. /* To be done ... */
  885. static int cosa_fasync(struct inode *inode, struct file *file, int on)
  886. {
  887. int port = iminor(inode);
  888. return fasync_helper(inode, file, on, &fasync[port]);
  889. }
  890. #endif
  891. /* ---------- Ioctls ---------- */
  892. /*
  893. * Ioctl subroutines can safely be made inline, because they are called
  894. * only from cosa_ioctl().
  895. */
  896. static inline int cosa_reset(struct cosa_data *cosa)
  897. {
  898. char idstring[COSA_MAX_ID_STRING];
  899. if (cosa->usage > 1)
  900. printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  901. cosa->num, cosa->usage);
  902. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
  903. if (cosa_reset_and_read_id(cosa, idstring) < 0) {
  904. printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
  905. return -EIO;
  906. }
  907. printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
  908. idstring);
  909. cosa->firmware_status |= COSA_FW_RESET;
  910. return 0;
  911. }
  912. /* High-level function to download data into COSA memory. Calls download() */
  913. static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
  914. {
  915. struct cosa_download d;
  916. int i;
  917. if (cosa->usage > 1)
  918. printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  919. cosa->name, cosa->usage);
  920. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  921. printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
  922. cosa->name, cosa->firmware_status);
  923. return -EPERM;
  924. }
  925. if (copy_from_user(&d, arg, sizeof(d)))
  926. return -EFAULT;
  927. if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
  928. return -EINVAL;
  929. if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
  930. return -EINVAL;
  931. /* If something fails, force the user to reset the card */
  932. cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
  933. i = download(cosa, d.code, d.len, d.addr);
  934. if (i < 0) {
  935. printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
  936. cosa->num, i);
  937. return -EIO;
  938. }
  939. printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
  940. cosa->num, d.len, d.addr);
  941. cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
  942. return 0;
  943. }
  944. /* High-level function to read COSA memory. Calls readmem() */
  945. static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
  946. {
  947. struct cosa_download d;
  948. int i;
  949. if (cosa->usage > 1)
  950. printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
  951. "cosa->usage > 1 (%d). Odd things may happen.\n",
  952. cosa->num, cosa->usage);
  953. if (!(cosa->firmware_status & COSA_FW_RESET)) {
  954. printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
  955. cosa->name, cosa->firmware_status);
  956. return -EPERM;
  957. }
  958. if (copy_from_user(&d, arg, sizeof(d)))
  959. return -EFAULT;
  960. /* If something fails, force the user to reset the card */
  961. cosa->firmware_status &= ~COSA_FW_RESET;
  962. i = readmem(cosa, d.code, d.len, d.addr);
  963. if (i < 0) {
  964. printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
  965. cosa->num, i);
  966. return -EIO;
  967. }
  968. printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
  969. cosa->num, d.len, d.addr);
  970. cosa->firmware_status |= COSA_FW_RESET;
  971. return 0;
  972. }
  973. /* High-level function to start microcode. Calls startmicrocode(). */
  974. static inline int cosa_start(struct cosa_data *cosa, int address)
  975. {
  976. int i;
  977. if (cosa->usage > 1)
  978. printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
  979. cosa->num, cosa->usage);
  980. if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
  981. != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
  982. printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
  983. cosa->name, cosa->firmware_status);
  984. return -EPERM;
  985. }
  986. cosa->firmware_status &= ~COSA_FW_RESET;
  987. if ((i=startmicrocode(cosa, address)) < 0) {
  988. printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
  989. cosa->num, address, i);
  990. return -EIO;
  991. }
  992. printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
  993. cosa->num, address);
  994. cosa->startaddr = address;
  995. cosa->firmware_status |= COSA_FW_START;
  996. return 0;
  997. }
  998. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  999. static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
  1000. {
  1001. int l = strlen(cosa->id_string)+1;
  1002. if (copy_to_user(string, cosa->id_string, l))
  1003. return -EFAULT;
  1004. return l;
  1005. }
  1006. /* Buffer of size at least COSA_MAX_ID_STRING is expected */
  1007. static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
  1008. {
  1009. int l = strlen(cosa->type)+1;
  1010. if (copy_to_user(string, cosa->type, l))
  1011. return -EFAULT;
  1012. return l;
  1013. }
  1014. static int cosa_ioctl_common(struct cosa_data *cosa,
  1015. struct channel_data *channel, unsigned int cmd, unsigned long arg)
  1016. {
  1017. void __user *argp = (void __user *)arg;
  1018. switch(cmd) {
  1019. case COSAIORSET: /* Reset the device */
  1020. if (!capable(CAP_NET_ADMIN))
  1021. return -EACCES;
  1022. return cosa_reset(cosa);
  1023. case COSAIOSTRT: /* Start the firmware */
  1024. if (!capable(CAP_SYS_RAWIO))
  1025. return -EACCES;
  1026. return cosa_start(cosa, arg);
  1027. case COSAIODOWNLD: /* Download the firmware */
  1028. if (!capable(CAP_SYS_RAWIO))
  1029. return -EACCES;
  1030. return cosa_download(cosa, argp);
  1031. case COSAIORMEM:
  1032. if (!capable(CAP_SYS_RAWIO))
  1033. return -EACCES;
  1034. return cosa_readmem(cosa, argp);
  1035. case COSAIORTYPE:
  1036. return cosa_gettype(cosa, argp);
  1037. case COSAIORIDSTR:
  1038. return cosa_getidstr(cosa, argp);
  1039. case COSAIONRCARDS:
  1040. return nr_cards;
  1041. case COSAIONRCHANS:
  1042. return cosa->nchannels;
  1043. case COSAIOBMSET:
  1044. if (!capable(CAP_SYS_RAWIO))
  1045. return -EACCES;
  1046. if (is_8bit(cosa))
  1047. return -EINVAL;
  1048. if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
  1049. return -EINVAL;
  1050. cosa->busmaster = arg;
  1051. return 0;
  1052. case COSAIOBMGET:
  1053. return cosa->busmaster;
  1054. }
  1055. return -ENOIOCTLCMD;
  1056. }
  1057. static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1058. {
  1059. int rv;
  1060. struct channel_data *chan = dev_to_chan(dev);
  1061. rv = cosa_ioctl_common(chan->cosa, chan, cmd,
  1062. (unsigned long)ifr->ifr_data);
  1063. if (rv != -ENOIOCTLCMD)
  1064. return rv;
  1065. return hdlc_ioctl(dev, ifr, cmd);
  1066. }
  1067. static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
  1068. unsigned int cmd, unsigned long arg)
  1069. {
  1070. struct channel_data *channel = file->private_data;
  1071. struct cosa_data *cosa = channel->cosa;
  1072. return cosa_ioctl_common(cosa, channel, cmd, arg);
  1073. }
  1074. /*---------- HW layer interface ---------- */
  1075. /*
  1076. * The higher layer can bind itself to the HW layer by setting the callbacks
  1077. * in the channel_data structure and by using these routines.
  1078. */
  1079. static void cosa_enable_rx(struct channel_data *chan)
  1080. {
  1081. struct cosa_data *cosa = chan->cosa;
  1082. if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
  1083. put_driver_status(cosa);
  1084. }
  1085. static void cosa_disable_rx(struct channel_data *chan)
  1086. {
  1087. struct cosa_data *cosa = chan->cosa;
  1088. if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
  1089. put_driver_status(cosa);
  1090. }
  1091. /*
  1092. * FIXME: This routine probably should check for cosa_start_tx() called when
  1093. * the previous transmit is still unfinished. In this case the non-zero
  1094. * return value should indicate to the caller that the queuing(sp?) up
  1095. * the transmit has failed.
  1096. */
  1097. static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
  1098. {
  1099. struct cosa_data *cosa = chan->cosa;
  1100. unsigned long flags;
  1101. #ifdef DEBUG_DATA
  1102. int i;
  1103. printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
  1104. chan->num, len);
  1105. for (i=0; i<len; i++)
  1106. printk(" %02x", buf[i]&0xff);
  1107. printk("\n");
  1108. #endif
  1109. spin_lock_irqsave(&cosa->lock, flags);
  1110. chan->txbuf = buf;
  1111. chan->txsize = len;
  1112. if (len > COSA_MTU)
  1113. chan->txsize = COSA_MTU;
  1114. spin_unlock_irqrestore(&cosa->lock, flags);
  1115. /* Tell the firmware we are ready */
  1116. set_bit(chan->num, &cosa->txbitmap);
  1117. put_driver_status(cosa);
  1118. return 0;
  1119. }
  1120. static void put_driver_status(struct cosa_data *cosa)
  1121. {
  1122. unsigned long flags;
  1123. int status;
  1124. spin_lock_irqsave(&cosa->lock, flags);
  1125. status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
  1126. | (cosa->txbitmap ? DRIVER_TX_READY : 0)
  1127. | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
  1128. &DRIVER_TXMAP_MASK : 0);
  1129. if (!cosa->rxtx) {
  1130. if (cosa->rxbitmap|cosa->txbitmap) {
  1131. if (!cosa->enabled) {
  1132. cosa_putstatus(cosa, SR_RX_INT_ENA);
  1133. #ifdef DEBUG_IO
  1134. debug_status_out(cosa, SR_RX_INT_ENA);
  1135. #endif
  1136. cosa->enabled = 1;
  1137. }
  1138. } else if (cosa->enabled) {
  1139. cosa->enabled = 0;
  1140. cosa_putstatus(cosa, 0);
  1141. #ifdef DEBUG_IO
  1142. debug_status_out(cosa, 0);
  1143. #endif
  1144. }
  1145. cosa_putdata8(cosa, status);
  1146. #ifdef DEBUG_IO
  1147. debug_data_cmd(cosa, status);
  1148. #endif
  1149. }
  1150. spin_unlock_irqrestore(&cosa->lock, flags);
  1151. }
  1152. static void put_driver_status_nolock(struct cosa_data *cosa)
  1153. {
  1154. int status;
  1155. status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
  1156. | (cosa->txbitmap ? DRIVER_TX_READY : 0)
  1157. | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
  1158. &DRIVER_TXMAP_MASK : 0);
  1159. if (cosa->rxbitmap|cosa->txbitmap) {
  1160. cosa_putstatus(cosa, SR_RX_INT_ENA);
  1161. #ifdef DEBUG_IO
  1162. debug_status_out(cosa, SR_RX_INT_ENA);
  1163. #endif
  1164. cosa->enabled = 1;
  1165. } else {
  1166. cosa_putstatus(cosa, 0);
  1167. #ifdef DEBUG_IO
  1168. debug_status_out(cosa, 0);
  1169. #endif
  1170. cosa->enabled = 0;
  1171. }
  1172. cosa_putdata8(cosa, status);
  1173. #ifdef DEBUG_IO
  1174. debug_data_cmd(cosa, status);
  1175. #endif
  1176. }
  1177. /*
  1178. * The "kickme" function: When the DMA times out, this is called to
  1179. * clean up the driver status.
  1180. * FIXME: Preliminary support, the interface is probably wrong.
  1181. */
  1182. static void cosa_kick(struct cosa_data *cosa)
  1183. {
  1184. unsigned long flags, flags1;
  1185. char *s = "(probably) IRQ";
  1186. if (test_bit(RXBIT, &cosa->rxtx))
  1187. s = "RX DMA";
  1188. if (test_bit(TXBIT, &cosa->rxtx))
  1189. s = "TX DMA";
  1190. printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
  1191. spin_lock_irqsave(&cosa->lock, flags);
  1192. cosa->rxtx = 0;
  1193. flags1 = claim_dma_lock();
  1194. disable_dma(cosa->dma);
  1195. clear_dma_ff(cosa->dma);
  1196. release_dma_lock(flags1);
  1197. /* FIXME: Anything else? */
  1198. udelay(100);
  1199. cosa_putstatus(cosa, 0);
  1200. udelay(100);
  1201. (void) cosa_getdata8(cosa);
  1202. udelay(100);
  1203. cosa_putdata8(cosa, 0);
  1204. udelay(100);
  1205. put_driver_status_nolock(cosa);
  1206. spin_unlock_irqrestore(&cosa->lock, flags);
  1207. }
  1208. /*
  1209. * Check if the whole buffer is DMA-able. It means it is below the 16M of
  1210. * physical memory and doesn't span the 64k boundary. For now it seems
  1211. * SKB's never do this, but we'll check this anyway.
  1212. */
  1213. static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
  1214. {
  1215. static int count;
  1216. unsigned long b = (unsigned long)buf;
  1217. if (b+len >= MAX_DMA_ADDRESS)
  1218. return 0;
  1219. if ((b^ (b+len)) & 0x10000) {
  1220. if (count++ < 5)
  1221. printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
  1222. chan->name);
  1223. return 0;
  1224. }
  1225. return 1;
  1226. }
  1227. /* ---------- The SRP/COSA ROM monitor functions ---------- */
  1228. /*
  1229. * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
  1230. * drivers need to say 4-digit hex number meaning start address of the microcode
  1231. * separated by a single space. Monitor replies by saying " =". Now driver
  1232. * has to write 4-digit hex number meaning the last byte address ended
  1233. * by a single space. Monitor has to reply with a space. Now the download
  1234. * begins. After the download monitor replies with "\r\n." (CR LF dot).
  1235. */
  1236. static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
  1237. {
  1238. int i;
  1239. if (put_wait_data(cosa, 'w') == -1) return -1;
  1240. if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
  1241. if (get_wait_data(cosa) != '=') return -3;
  1242. if (puthexnumber(cosa, address) < 0) return -4;
  1243. if (put_wait_data(cosa, ' ') == -1) return -10;
  1244. if (get_wait_data(cosa) != ' ') return -11;
  1245. if (get_wait_data(cosa) != '=') return -12;
  1246. if (puthexnumber(cosa, address+length-1) < 0) return -13;
  1247. if (put_wait_data(cosa, ' ') == -1) return -18;
  1248. if (get_wait_data(cosa) != ' ') return -19;
  1249. while (length--) {
  1250. char c;
  1251. #ifndef SRP_DOWNLOAD_AT_BOOT
  1252. if (get_user(c, microcode))
  1253. return -23; /* ??? */
  1254. #else
  1255. c = *microcode;
  1256. #endif
  1257. if (put_wait_data(cosa, c) == -1)
  1258. return -20;
  1259. microcode++;
  1260. }
  1261. if (get_wait_data(cosa) != '\r') return -21;
  1262. if (get_wait_data(cosa) != '\n') return -22;
  1263. if (get_wait_data(cosa) != '.') return -23;
  1264. #if 0
  1265. printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
  1266. #endif
  1267. return 0;
  1268. }
  1269. /*
  1270. * Starting microcode is done via the "g" command of the SRP monitor.
  1271. * The chat should be the following: "g" "g=" "<addr><CR>"
  1272. * "<CR><CR><LF><CR><LF>".
  1273. */
  1274. static int startmicrocode(struct cosa_data *cosa, int address)
  1275. {
  1276. if (put_wait_data(cosa, 'g') == -1) return -1;
  1277. if (get_wait_data(cosa) != 'g') return -2;
  1278. if (get_wait_data(cosa) != '=') return -3;
  1279. if (puthexnumber(cosa, address) < 0) return -4;
  1280. if (put_wait_data(cosa, '\r') == -1) return -5;
  1281. if (get_wait_data(cosa) != '\r') return -6;
  1282. if (get_wait_data(cosa) != '\r') return -7;
  1283. if (get_wait_data(cosa) != '\n') return -8;
  1284. if (get_wait_data(cosa) != '\r') return -9;
  1285. if (get_wait_data(cosa) != '\n') return -10;
  1286. #if 0
  1287. printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
  1288. #endif
  1289. return 0;
  1290. }
  1291. /*
  1292. * Reading memory is done via the "r" command of the SRP monitor.
  1293. * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
  1294. * Then driver can read the data and the conversation is finished
  1295. * by SRP monitor sending "<CR><LF>." (dot at the end).
  1296. *
  1297. * This routine is not needed during the normal operation and serves
  1298. * for debugging purposes only.
  1299. */
  1300. static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
  1301. {
  1302. if (put_wait_data(cosa, 'r') == -1) return -1;
  1303. if ((get_wait_data(cosa)) != 'r') return -2;
  1304. if ((get_wait_data(cosa)) != '=') return -3;
  1305. if (puthexnumber(cosa, address) < 0) return -4;
  1306. if (put_wait_data(cosa, ' ') == -1) return -5;
  1307. if (get_wait_data(cosa) != ' ') return -6;
  1308. if (get_wait_data(cosa) != '=') return -7;
  1309. if (puthexnumber(cosa, address+length-1) < 0) return -8;
  1310. if (put_wait_data(cosa, ' ') == -1) return -9;
  1311. if (get_wait_data(cosa) != ' ') return -10;
  1312. while (length--) {
  1313. char c;
  1314. int i;
  1315. if ((i=get_wait_data(cosa)) == -1) {
  1316. printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
  1317. length);
  1318. return -11;
  1319. }
  1320. c=i;
  1321. #if 1
  1322. if (put_user(c, microcode))
  1323. return -23; /* ??? */
  1324. #else
  1325. *microcode = c;
  1326. #endif
  1327. microcode++;
  1328. }
  1329. if (get_wait_data(cosa) != '\r') return -21;
  1330. if (get_wait_data(cosa) != '\n') return -22;
  1331. if (get_wait_data(cosa) != '.') return -23;
  1332. #if 0
  1333. printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
  1334. #endif
  1335. return 0;
  1336. }
  1337. /*
  1338. * This function resets the device and reads the initial prompt
  1339. * of the device's ROM monitor.
  1340. */
  1341. static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
  1342. {
  1343. int i=0, id=0, prev=0, curr=0;
  1344. /* Reset the card ... */
  1345. cosa_putstatus(cosa, 0);
  1346. cosa_getdata8(cosa);
  1347. cosa_putstatus(cosa, SR_RST);
  1348. #ifdef MODULE
  1349. msleep(500);
  1350. #else
  1351. udelay(5*100000);
  1352. #endif
  1353. /* Disable all IRQs from the card */
  1354. cosa_putstatus(cosa, 0);
  1355. /*
  1356. * Try to read the ID string. The card then prints out the
  1357. * identification string ended by the "\n\x2e".
  1358. *
  1359. * The following loop is indexed through i (instead of id)
  1360. * to avoid looping forever when for any reason
  1361. * the port returns '\r', '\n' or '\x2e' permanently.
  1362. */
  1363. for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
  1364. if ((curr = get_wait_data(cosa)) == -1) {
  1365. return -1;
  1366. }
  1367. curr &= 0xff;
  1368. if (curr != '\r' && curr != '\n' && curr != 0x2e)
  1369. idstring[id++] = curr;
  1370. if (curr == 0x2e && prev == '\n')
  1371. break;
  1372. }
  1373. /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
  1374. idstring[id] = '\0';
  1375. return id;
  1376. }
  1377. /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
  1378. /*
  1379. * This routine gets the data byte from the card waiting for the SR_RX_RDY
  1380. * bit to be set in a loop. It should be used in the exceptional cases
  1381. * only (for example when resetting the card or downloading the firmware.
  1382. */
  1383. static int get_wait_data(struct cosa_data *cosa)
  1384. {
  1385. int retries = 1000;
  1386. while (--retries) {
  1387. /* read data and return them */
  1388. if (cosa_getstatus(cosa) & SR_RX_RDY) {
  1389. short r;
  1390. r = cosa_getdata8(cosa);
  1391. #if 0
  1392. printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
  1393. #endif
  1394. return r;
  1395. }
  1396. /* sleep if not ready to read */
  1397. schedule_timeout_interruptible(1);
  1398. }
  1399. printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
  1400. cosa_getstatus(cosa));
  1401. return -1;
  1402. }
  1403. /*
  1404. * This routine puts the data byte to the card waiting for the SR_TX_RDY
  1405. * bit to be set in a loop. It should be used in the exceptional cases
  1406. * only (for example when resetting the card or downloading the firmware).
  1407. */
  1408. static int put_wait_data(struct cosa_data *cosa, int data)
  1409. {
  1410. int retries = 1000;
  1411. while (--retries) {
  1412. /* read data and return them */
  1413. if (cosa_getstatus(cosa) & SR_TX_RDY) {
  1414. cosa_putdata8(cosa, data);
  1415. #if 0
  1416. printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
  1417. #endif
  1418. return 0;
  1419. }
  1420. #if 0
  1421. /* sleep if not ready to read */
  1422. schedule_timeout_interruptible(1);
  1423. #endif
  1424. }
  1425. printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
  1426. cosa->num, cosa_getstatus(cosa));
  1427. return -1;
  1428. }
  1429. /*
  1430. * The following routine puts the hexadecimal number into the SRP monitor
  1431. * and verifies the proper echo of the sent bytes. Returns 0 on success,
  1432. * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
  1433. * (-2,-4,-6,-8) means that reading echo failed.
  1434. */
  1435. static int puthexnumber(struct cosa_data *cosa, int number)
  1436. {
  1437. char temp[5];
  1438. int i;
  1439. /* Well, I should probably replace this by something faster. */
  1440. sprintf(temp, "%04X", number);
  1441. for (i=0; i<4; i++) {
  1442. if (put_wait_data(cosa, temp[i]) == -1) {
  1443. printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
  1444. cosa->num, i);
  1445. return -1-2*i;
  1446. }
  1447. if (get_wait_data(cosa) != temp[i]) {
  1448. printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
  1449. cosa->num, i);
  1450. return -2-2*i;
  1451. }
  1452. }
  1453. return 0;
  1454. }
  1455. /* ---------- Interrupt routines ---------- */
  1456. /*
  1457. * There are three types of interrupt:
  1458. * At the beginning of transmit - this handled is in tx_interrupt(),
  1459. * at the beginning of receive - it is in rx_interrupt() and
  1460. * at the end of transmit/receive - it is the eot_interrupt() function.
  1461. * These functions are multiplexed by cosa_interrupt() according to the
  1462. * COSA status byte. I have moved the rx/tx/eot interrupt handling into
  1463. * separate functions to make it more readable. These functions are inline,
  1464. * so there should be no overhead of function call.
  1465. *
  1466. * In the COSA bus-master mode, we need to tell the card the address of a
  1467. * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
  1468. * It's time to use the bottom half :-(
  1469. */
  1470. /*
  1471. * Transmit interrupt routine - called when COSA is willing to obtain
  1472. * data from the OS. The most tricky part of the routine is selection
  1473. * of channel we (OS) want to send packet for. For SRP we should probably
  1474. * use the round-robin approach. The newer COSA firmwares have a simple
  1475. * flow-control - in the status word has bits 2 and 3 set to 1 means that the
  1476. * channel 0 or 1 doesn't want to receive data.
  1477. *
  1478. * It seems there is a bug in COSA firmware (need to trace it further):
  1479. * When the driver status says that the kernel has no more data for transmit
  1480. * (e.g. at the end of TX DMA) and then the kernel changes its mind
  1481. * (e.g. new packet is queued to hard_start_xmit()), the card issues
  1482. * the TX interrupt but does not mark the channel as ready-to-transmit.
  1483. * The fix seems to be to push the packet to COSA despite its request.
  1484. * We first try to obey the card's opinion, and then fall back to forced TX.
  1485. */
  1486. static inline void tx_interrupt(struct cosa_data *cosa, int status)
  1487. {
  1488. unsigned long flags, flags1;
  1489. #ifdef DEBUG_IRQS
  1490. printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
  1491. cosa->num, status);
  1492. #endif
  1493. spin_lock_irqsave(&cosa->lock, flags);
  1494. set_bit(TXBIT, &cosa->rxtx);
  1495. if (!test_bit(IRQBIT, &cosa->rxtx)) {
  1496. /* flow control, see the comment above */
  1497. int i=0;
  1498. if (!cosa->txbitmap) {
  1499. printk(KERN_WARNING "%s: No channel wants data "
  1500. "in TX IRQ. Expect DMA timeout.",
  1501. cosa->name);
  1502. put_driver_status_nolock(cosa);
  1503. clear_bit(TXBIT, &cosa->rxtx);
  1504. spin_unlock_irqrestore(&cosa->lock, flags);
  1505. return;
  1506. }
  1507. while(1) {
  1508. cosa->txchan++;
  1509. i++;
  1510. if (cosa->txchan >= cosa->nchannels)
  1511. cosa->txchan = 0;
  1512. if (!(cosa->txbitmap & (1<<cosa->txchan)))
  1513. continue;
  1514. if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
  1515. break;
  1516. /* in second pass, accept first ready-to-TX channel */
  1517. if (i > cosa->nchannels) {
  1518. /* Can be safely ignored */
  1519. #ifdef DEBUG_IRQS
  1520. printk(KERN_DEBUG "%s: Forcing TX "
  1521. "to not-ready channel %d\n",
  1522. cosa->name, cosa->txchan);
  1523. #endif
  1524. break;
  1525. }
  1526. }
  1527. cosa->txsize = cosa->chan[cosa->txchan].txsize;
  1528. if (cosa_dma_able(cosa->ch

Large files are truncated, but you can click here to view the full file