smc-ircc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:30k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*********************************************************************
  2.  *                
  3.  * Filename:      smc-ircc.c
  4.  * Version:       0.4
  5.  * Description:   Driver for the SMC Infrared Communications Controller
  6.  * Status:        Experimental.
  7.  * Author:        Thomas Davis (tadavis@jps.net)
  8.  * Created at:    
  9.  * Modified at:   Tue Feb 22 10:05:06 2000
  10.  * Modified by:   Dag Brattli <dag@brattli.net>
  11.  * Modified at:   Tue Jun 26 2001
  12.  * Modified by:   Stefani Seibold <stefani@seibold.net>
  13.  * 
  14.  *     Copyright (c) 2001      Stefani Seibold
  15.  *     Copyright (c) 1999-2001 Dag Brattli
  16.  *     Copyright (c) 1998-1999 Thomas Davis, 
  17.  *     All Rights Reserved.
  18.  *      
  19.  *     This program is free software; you can redistribute it and/or 
  20.  *     modify it under the terms of the GNU General Public License as 
  21.  *     published by the Free Software Foundation; either version 2 of 
  22.  *     the License, or (at your option) any later version.
  23.  * 
  24.  *     This program is distributed in the hope that it will be useful,
  25.  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27.  *     GNU General Public License for more details.
  28.  * 
  29.  *     You should have received a copy of the GNU General Public License 
  30.  *     along with this program; if not, write to the Free Software 
  31.  *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
  32.  *     MA 02111-1307 USA
  33.  *
  34.  *     SIO's: all SIO documentet by SMC (June, 2001)
  35.  *     Applicable Models : Fujitsu Lifebook 635t, Sony PCG-505TX,
  36.  *      Dell Inspiron 8000
  37.  *
  38.  ********************************************************************/
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/types.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/ioport.h>
  45. #include <linux/delay.h>
  46. #include <linux/slab.h>
  47. #include <linux/init.h>
  48. #include <linux/rtnetlink.h>
  49. #include <linux/serial_reg.h>
  50. #include <asm/io.h>
  51. #include <asm/dma.h>
  52. #include <asm/byteorder.h>
  53. #include <linux/pm.h>
  54. #include <net/irda/wrapper.h>
  55. #include <net/irda/irda.h>
  56. #include <net/irda/irmod.h>
  57. #include <net/irda/irlap_frame.h>
  58. #include <net/irda/irda_device.h>
  59. #include <net/irda/smc-ircc.h>
  60. #include <net/irda/irport.h>
  61. struct smc_chip {
  62. char *name;
  63. u16 flags;
  64. u8 devid;
  65. u8 rev;
  66. };
  67. typedef struct smc_chip smc_chip_t;
  68. static const char *driver_name = "smc-ircc";
  69. #define DIM(x) (sizeof(x)/(sizeof(*(x))))
  70. #define CHIP_IO_EXTENT 8
  71. static struct ircc_cb *dev_self[] = { NULL, NULL};
  72. /* Some prototypes */
  73. static int  ircc_open(unsigned int iobase, unsigned int board_addr);
  74. static int  ircc_dma_receive(struct ircc_cb *self, int iobase); 
  75. static void ircc_dma_receive_complete(struct ircc_cb *self, int iobase);
  76. static int  ircc_hard_xmit(struct sk_buff *skb, struct net_device *dev);
  77. static void ircc_dma_xmit(struct ircc_cb *self, int iobase, int bofs);
  78. static void ircc_change_speed(void *priv, u32 speed);
  79. static void ircc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  80. static int  ircc_net_open(struct net_device *dev);
  81. static int  ircc_net_close(struct net_device *dev);
  82. static int  ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data);
  83. #define KEY55_1 0 /* SuperIO Configuration mode with Key <0x55> */
  84. #define KEY55_2 1 /* SuperIO Configuration mode with Key <0x55,0x55> */
  85. #define NoIRDA 2 /* SuperIO Chip has no IRDA Port */
  86. #define SIR 0 /* SuperIO Chip has only slow IRDA */
  87. #define FIR 4 /* SuperIO Chip has fast IRDA */
  88. #define SERx4 8 /* SuperIO Chip supports 115,2 KBaud * 4=460,8 KBaud */
  89. /* These are the currently known SMC SuperIO chipsets */
  90. static smc_chip_t __initdata fdc_chips_flat[]=
  91. {
  92. /* Base address 0x3f0 or 0x370 */
  93. { "37C44", KEY55_1|NoIRDA, 0x00, 0x00 }, /* This chip can not detected */
  94. { "37C665GT", KEY55_2|NoIRDA, 0x65, 0x01 },
  95. { "37C665GT", KEY55_2|NoIRDA, 0x66, 0x01 },
  96. { "37C669", KEY55_2|SIR|SERx4, 0x03, 0x02 },
  97. { "37C669", KEY55_2|SIR|SERx4, 0x04, 0x02 }, /* ID? */
  98. { "37C78", KEY55_2|NoIRDA, 0x78, 0x00 },
  99. { "37N769", KEY55_1|FIR|SERx4, 0x28, 0x00 },
  100. { "37N869", KEY55_1|FIR|SERx4, 0x29, 0x00 },
  101. { NULL }
  102. };
  103. static smc_chip_t __initdata fdc_chips_paged[]=
  104. {
  105. /* Base address 0x3f0 or 0x370 */
  106. { "37B72X", KEY55_1|SIR|SERx4, 0x4c, 0x00 },
  107. { "37B77X", KEY55_1|SIR|SERx4, 0x43, 0x00 },
  108. { "37B78X", KEY55_1|SIR|SERx4, 0x44, 0x00 },
  109. { "37B80X", KEY55_1|SIR|SERx4, 0x42, 0x00 },
  110. { "37C67X", KEY55_1|FIR|SERx4, 0x40, 0x00 },
  111. { "37C93X", KEY55_2|SIR|SERx4, 0x02, 0x01 },
  112. { "37C93XAPM", KEY55_1|SIR|SERx4, 0x30, 0x01 },
  113. { "37C93XFR", KEY55_2|FIR|SERx4, 0x03, 0x01 },
  114. { "37M707", KEY55_1|SIR|SERx4, 0x42, 0x00 },
  115. { "37M81X", KEY55_1|SIR|SERx4, 0x4d, 0x00 },
  116. { "37N958FR", KEY55_1|FIR|SERx4, 0x09, 0x04 },
  117. { "37N971", KEY55_1|FIR|SERx4, 0x0a, 0x00 },
  118. { "37N972", KEY55_1|FIR|SERx4, 0x0b, 0x00 },
  119. { NULL }
  120. };
  121. static smc_chip_t __initdata lpc_chips_flat[]=
  122. {
  123. /* Base address 0x2E or 0x4E */
  124. { "47N227", KEY55_1|FIR|SERx4, 0x5a, 0x00 },
  125. { "47N267", KEY55_1|FIR|SERx4, 0x5e, 0x00 },
  126. { NULL }
  127. };
  128. static smc_chip_t __initdata lpc_chips_paged[]=
  129. {
  130. /* Base address 0x2E or 0x4E */
  131. { "47B27X", KEY55_1|SIR|SERx4, 0x51, 0x00 },
  132. { "47B37X", KEY55_1|SIR|SERx4, 0x52, 0x00 },
  133. { "47M10X", KEY55_1|SIR|SERx4, 0x59, 0x00 },
  134. { "47M120", KEY55_1|NoIRDA|SERx4, 0x5c, 0x00 },
  135. { "47M13X", KEY55_1|SIR|SERx4, 0x59, 0x00 },
  136. { "47M14X", KEY55_1|SIR|SERx4, 0x5f, 0x00 },
  137. { "47N252", KEY55_1|FIR|SERx4, 0x0e, 0x00 },
  138. { "47S42X", KEY55_1|SIR|SERx4, 0x57, 0x00 },
  139. { NULL }
  140. };
  141. static int ircc_irq=255;
  142. static int ircc_dma=255;
  143. static int ircc_fir=0;
  144. static int ircc_sir=0;
  145. static int ircc_cfg=0;
  146. static unsigned short dev_count=0;
  147. static inline void register_bank(int iobase, int bank)
  148. {
  149.         outb(((inb(iobase+IRCC_MASTER) & 0xf0) | (bank & 0x07)),
  150.                iobase+IRCC_MASTER);
  151. }
  152. static int __init smc_access(unsigned short cfg_base,unsigned char reg)
  153. {
  154. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  155. outb(reg, cfg_base);
  156. if (inb(cfg_base)!=reg)
  157. return -1;
  158. return 0;
  159. }
  160. static const smc_chip_t * __init smc_probe(unsigned short cfg_base,u8 reg,const smc_chip_t *chip,char *type)
  161. {
  162. u8 devid,xdevid,rev; 
  163. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  164. /* Leave configuration */
  165. outb(0xaa, cfg_base);
  166. if (inb(cfg_base)==0xaa) /* not a smc superio chip */
  167. return NULL;
  168. outb(reg, cfg_base);
  169. xdevid=inb(cfg_base+1);
  170. /* Enter configuration */
  171. outb(0x55, cfg_base);
  172. if (smc_access(cfg_base,0x55)) /* send second key and check */
  173. return NULL;
  174. /* probe device ID */
  175. if (smc_access(cfg_base,reg))
  176. return NULL;
  177. devid=inb(cfg_base+1);
  178. if (devid==0) /* typical value for unused port */
  179. return NULL;
  180. if (devid==0xff) /* typical value for unused port */
  181. return NULL;
  182. /* probe revision ID */
  183. if (smc_access(cfg_base,reg+1))
  184. return NULL;
  185. rev=inb(cfg_base+1);
  186. if (rev>=128) /* i think this will make no sense */
  187. return NULL;
  188. if (devid==xdevid) /* protection against false positives */        
  189. return NULL;
  190. /* Check for expected device ID; are there others? */
  191. while(chip->devid!=devid) {
  192. chip++;
  193. if (chip->name==NULL)
  194. return NULL;
  195. }
  196. if (chip->rev>rev)
  197. return NULL;
  198. MESSAGE("found SMC SuperIO Chip (devid=0x%02x rev=%02X base=0x%04x): %s%sn",devid,rev,cfg_base,type,chip->name);
  199. if (chip->flags&NoIRDA)
  200. MESSAGE("chipset does not support IRDAn");
  201. return chip;
  202. }
  203. /*
  204.  * Function smc_superio_flat (chip, base, type)
  205.  *
  206.  *    Try get configuration of a smc SuperIO chip with flat register model
  207.  *
  208.  */
  209. static int __init smc_superio_flat(const smc_chip_t *chips, unsigned short cfg_base, char *type)
  210. {
  211. unsigned short fir_io;
  212. unsigned short sir_io;
  213. u8 mode;
  214. int ret = -ENODEV;
  215. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  216. if (smc_probe(cfg_base,0xD,chips,type)==NULL)
  217. return ret;
  218. outb(0x0c, cfg_base);
  219. mode = inb(cfg_base+1);
  220. mode = (mode & 0x38) >> 3;
  221. /* Value for IR port */
  222. if (mode && mode < 4) {
  223. /* SIR iobase */
  224. outb(0x25, cfg_base);
  225. sir_io = inb(cfg_base+1) << 2;
  226.         /* FIR iobase */
  227. outb(0x2b, cfg_base);
  228. fir_io = inb(cfg_base+1) << 3;
  229. if (fir_io) {
  230. if (ircc_open(fir_io, sir_io) == 0)
  231. ret=0; 
  232. }
  233. }
  234. /* Exit configuration */
  235. outb(0xaa, cfg_base);
  236. return ret;
  237. }
  238. /*
  239.  * Function smc_superio_paged (chip, base, type)
  240.  *
  241.  *    Try  get configuration of a smc SuperIO chip with paged register model
  242.  *
  243.  */
  244. static int __init smc_superio_paged(const smc_chip_t *chips, unsigned short cfg_base, char *type)
  245. {
  246. unsigned short fir_io;
  247. unsigned short sir_io;
  248. int ret = -ENODEV;
  249. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  250. if (smc_probe(cfg_base,0x20,chips,type)==NULL)
  251. return ret;
  252. /* Select logical device (UART2) */
  253. outb(0x07, cfg_base);
  254. outb(0x05, cfg_base + 1);
  255. /* SIR iobase */
  256. outb(0x60, cfg_base);
  257. sir_io  = inb(cfg_base + 1) << 8;
  258. outb(0x61, cfg_base);
  259. sir_io |= inb(cfg_base + 1);
  260. /* Read FIR base */
  261. outb(0x62, cfg_base);
  262. fir_io = inb(cfg_base + 1) << 8;
  263. outb(0x63, cfg_base);
  264. fir_io |= inb(cfg_base + 1);
  265. outb(0x2b, cfg_base); /* ??? */
  266. if (fir_io) {
  267. if (ircc_open(fir_io, sir_io) == 0)
  268. ret=0; 
  269. }
  270. /* Exit configuration */
  271. outb(0xaa, cfg_base);
  272. return ret;
  273. }
  274. static int __init smc_superio_fdc(unsigned short cfg_base)
  275. {
  276. if (check_region(cfg_base, 2) < 0) {
  277. IRDA_DEBUG(0, "%s: can't get cfg_base of 0x%03xn",
  278. __FUNCTION__, cfg_base);
  279. return -1;
  280. }
  281. if (!smc_superio_flat(fdc_chips_flat,cfg_base,"FDC")||!smc_superio_paged(fdc_chips_paged,cfg_base,"FDC"))
  282. return 0;
  283. return -1;
  284. }
  285. static int __init smc_superio_lpc(unsigned short cfg_base)
  286. {
  287. #if 0
  288. if (check_region(cfg_base, 2) < 0) {
  289. IRDA_DEBUG(0, "%s: can't get cfg_base of 0x%03xn",
  290. __FUNCTION__, cfg_base);
  291. return -1;
  292. }
  293. #endif
  294. if (!smc_superio_flat(lpc_chips_flat,cfg_base,"LPC")||!smc_superio_paged(lpc_chips_paged,cfg_base,"LPC"))
  295. return 0;
  296. return -1;
  297. }
  298. /*
  299.  * Function ircc_init ()
  300.  *
  301.  *    Initialize chip. Just try to find out how many chips we are dealing with
  302.  *    and where they are
  303.  */
  304. int __init ircc_init(void)
  305. {
  306. int ret=-ENODEV;
  307. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  308. dev_count=0;
  309. if ((ircc_fir>0)&&(ircc_sir>0)) {
  310.         MESSAGE(" Overriding FIR address 0x%04xn", ircc_fir);
  311. MESSAGE(" Overriding SIR address 0x%04xn", ircc_sir);
  312. if (ircc_open(ircc_fir, ircc_sir) == 0)
  313. return 0;
  314. return -ENODEV;
  315. }
  316. /* try user provided configuration register base address */
  317. if (ircc_cfg>0) {
  318.         MESSAGE(" Overriding configuration address 0x%04xn", ircc_cfg);
  319. if (!smc_superio_fdc(ircc_cfg))
  320. ret=0;
  321. }
  322. /* Trys to open for all the SMC chipsets we know about */
  323. IRDA_DEBUG(0, "%s Try to open all known SMC chipsetsn", __FUNCTION__);
  324. if (!smc_superio_fdc(0x3f0))
  325. ret=0;
  326. if (!smc_superio_fdc(0x370))
  327. ret=0;
  328. if (!smc_superio_fdc(0xe0))
  329. ret=0;
  330. if (!smc_superio_lpc(0x2e))
  331. ret=0;
  332. if (!smc_superio_lpc(0x4e))
  333. ret=0;
  334. return ret;
  335. }
  336. /*
  337.  * Function ircc_open (iobase, irq)
  338.  *
  339.  *    Try to open driver instance
  340.  *
  341.  */
  342. static int __init ircc_open(unsigned int fir_base, unsigned int sir_base)
  343. {
  344. struct ircc_cb *self;
  345.         struct irport_cb *irport;
  346. unsigned char low, high, chip, config, dma, irq, version;
  347. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  348. if (check_region(fir_base, CHIP_IO_EXTENT) < 0) {
  349. IRDA_DEBUG(0, "%s: can't get fir_base of 0x%03xn",
  350. __FUNCTION__, fir_base);
  351. return -ENODEV;
  352. }
  353. #if POSSIBLE_USED_BY_SERIAL_DRIVER
  354. if (check_region(sir_base, CHIP_IO_EXTENT) < 0) {
  355. IRDA_DEBUG(0, "%s: can't get sir_base of 0x%03xn",
  356. __FUNCTION__, sir_base);
  357. return -ENODEV;
  358. }
  359. #endif
  360. register_bank(fir_base, 3);
  361. high    = inb(fir_base+IRCC_ID_HIGH);
  362. low     = inb(fir_base+IRCC_ID_LOW);
  363. chip    = inb(fir_base+IRCC_CHIP_ID);
  364. version = inb(fir_base+IRCC_VERSION);
  365. config  = inb(fir_base+IRCC_INTERFACE);
  366. irq     = config >> 4 & 0x0f;
  367. dma     = config & 0x0f;
  368.         if (high != 0x10 || low != 0xb8 || (chip != 0xf1 && chip != 0xf2)) { 
  369.         IRDA_DEBUG(0, "%s(), addr 0x%04x - no device found!n", 
  370.          __FUNCTION__, fir_base);
  371. return -ENODEV;
  372. }
  373. MESSAGE("SMC IrDA Controller foundn IrCC version %d.%d, "
  374. "firport 0x%03x, sirport 0x%03x dma=%d, irq=%dn",
  375. chip & 0x0f, version, fir_base, sir_base, dma, irq);
  376. if (dev_count>DIM(dev_self)) {
  377.         IRDA_DEBUG(0, "%s(), to many devices!n", __FUNCTION__);
  378. return -ENOMEM;
  379. }
  380. /*
  381.  *  Allocate new instance of the driver
  382.  */
  383. self = kmalloc(sizeof(struct ircc_cb), GFP_KERNEL);
  384. if (self == NULL) {
  385. ERROR("%s, Can't allocate memory for control block!n",
  386.                       driver_name);
  387. return -ENOMEM;
  388. }
  389. memset(self, 0, sizeof(struct ircc_cb));
  390. spin_lock_init(&self->lock);
  391. /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
  392. self->rx_buff.truesize = 4000; 
  393. self->tx_buff.truesize = 4000;
  394. self->rx_buff.head = (u8 *) kmalloc(self->rx_buff.truesize,
  395.       GFP_KERNEL|GFP_DMA);
  396. if (self->rx_buff.head == NULL) {
  397. ERROR("%s, Can't allocate memory for receive buffer!n",
  398.                       driver_name);
  399. kfree(self);
  400. return -ENOMEM;
  401. }
  402. self->tx_buff.head = (u8 *) kmalloc(self->tx_buff.truesize, 
  403.       GFP_KERNEL|GFP_DMA);
  404. if (self->tx_buff.head == NULL) {
  405. ERROR("%s, Can't allocate memory for transmit buffer!n",
  406.                       driver_name);
  407. kfree(self->rx_buff.head);
  408. kfree(self);
  409. return -ENOMEM;
  410. }
  411. irport = irport_open(dev_count, sir_base, irq);
  412. if (!irport) {
  413. kfree(self->tx_buff.head);
  414. kfree(self->rx_buff.head);
  415. kfree(self);
  416. return -ENODEV;
  417. }
  418. memset(self->rx_buff.head, 0, self->rx_buff.truesize);
  419. memset(self->tx_buff.head, 0, self->tx_buff.truesize);
  420.    
  421. /* Need to store self somewhere */
  422. dev_self[dev_count++] = self;
  423. /* Steal the network device from irport */
  424. self->netdev = irport->netdev;
  425. self->irport = irport;
  426. irport->priv = self;
  427. /* Initialize IO */
  428. self->io           = &irport->io;
  429. self->io->fir_base  = fir_base;
  430.         self->io->sir_base  = sir_base; /* Used by irport */
  431.         self->io->fir_ext   = CHIP_IO_EXTENT;
  432.         self->io->sir_ext   = 8; /* Used by irport */
  433. if (ircc_irq < 255) {
  434. if (ircc_irq!=irq)
  435. MESSAGE("%s, Overriding IRQ - chip says %d, using %dn",
  436. driver_name, self->io->irq, ircc_irq);
  437. self->io->irq = ircc_irq;
  438. }
  439. else
  440. self->io->irq = irq;
  441. if (ircc_dma < 255) {
  442. if (ircc_dma!=dma)
  443. MESSAGE("%s, Overriding DMA - chip says %d, using %dn",
  444. driver_name, self->io->dma, ircc_dma);
  445. self->io->dma = ircc_dma;
  446. }
  447. else
  448. self->io->dma = dma;
  449. request_region(fir_base, CHIP_IO_EXTENT, driver_name);
  450. /* Initialize QoS for this device */
  451. irda_init_max_qos_capabilies(&irport->qos);
  452. /* The only value we must override it the baudrate */
  453. irport->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  454. IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8);
  455. irport->qos.min_turn_time.bits = 0x07;
  456. irport->qos.window_size.bits = 0x01;
  457. irda_qos_bits_to_value(&irport->qos);
  458. irport->flags = IFF_FIR|IFF_MIR|IFF_SIR|IFF_DMA|IFF_PIO;
  459. self->rx_buff.in_frame = FALSE;
  460. self->rx_buff.state = OUTSIDE_FRAME;
  461. self->tx_buff.data = self->tx_buff.head;
  462. self->rx_buff.data = self->rx_buff.head;
  463. /* Override the speed change function, since we must control it now */
  464. irport->change_speed = &ircc_change_speed;
  465. irport->interrupt    = &ircc_interrupt;
  466. self->netdev->open   = &ircc_net_open;
  467. self->netdev->stop   = &ircc_net_close;
  468. irport_start(self->irport);
  469.         self->pmdev = pm_register(PM_SYS_DEV, PM_SYS_IRDA, ircc_pmproc);
  470.         if (self->pmdev)
  471.                 self->pmdev->data = self;
  472. /* Power on device */
  473. outb(0x00, fir_base+IRCC_MASTER);
  474. return 0;
  475. }
  476. /*
  477.  * Function ircc_change_speed (self, baud)
  478.  *
  479.  *    Change the speed of the device
  480.  *
  481.  */
  482. static void ircc_change_speed(void *priv, u32 speed)
  483. {
  484. int iobase, ir_mode, ctrl, fast; 
  485. struct ircc_cb *self = (struct ircc_cb *) priv;
  486. struct net_device *dev;
  487. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  488. ASSERT(self != NULL, return;);
  489. dev = self->netdev;
  490. iobase = self->io->fir_base;
  491. /* Update accounting for new speed */
  492. self->io->speed = speed;
  493. outb(IRCC_MASTER_RESET, iobase+IRCC_MASTER);
  494. outb(0x00, iobase+IRCC_MASTER);
  495. switch (speed) {
  496. default:
  497. IRDA_DEBUG(0, "%s(), unknown baud rate of %dn", 
  498. __FUNCTION__, speed);
  499. /* FALLTHROUGH */
  500. case 9600:
  501. case 19200:
  502. case 38400:
  503. case 57600:
  504. case 115200:
  505. ir_mode = IRCC_CFGA_IRDA_SIR_A;
  506. ctrl = 0;
  507. fast = 0;
  508. break;
  509. case 576000:
  510. ir_mode = IRCC_CFGA_IRDA_HDLC;
  511. ctrl = IRCC_CRC;
  512. fast = 0;
  513. IRDA_DEBUG(0, "%s(), handling baud of 576000n", __FUNCTION__);
  514. break;
  515. case 1152000:
  516. ir_mode = IRCC_CFGA_IRDA_HDLC;
  517. ctrl = IRCC_1152 | IRCC_CRC;
  518. fast = 0;
  519. IRDA_DEBUG(0, "%s(), handling baud of 1152000n", __FUNCTION__);
  520. break;
  521. case 4000000:
  522. ir_mode = IRCC_CFGA_IRDA_4PPM;
  523. ctrl = IRCC_CRC;
  524. fast = IRCC_LCR_A_FAST;
  525. IRDA_DEBUG(0, "%s(), handling baud of 4000000n", __FUNCTION__);
  526. break;
  527. }
  528. register_bank(iobase, 0);
  529. outb(0, iobase+IRCC_IER);
  530. outb(IRCC_MASTER_INT_EN, iobase+IRCC_MASTER);
  531. /* Make special FIR init if necessary */
  532. if (speed > 115200) {
  533. irport_stop(self->irport);
  534. /* Install FIR transmit handler */
  535. dev->hard_start_xmit = &ircc_hard_xmit;
  536. /* 
  537.  * Don't know why we have to do this, but FIR interrupts 
  538.  * stops working if we remove it.
  539.  */
  540. /* outb(UART_MCR_OUT2, self->io->sir_base + UART_MCR); */
  541. /* Be ready for incoming frames */
  542. ircc_dma_receive(self, iobase);
  543. } else {
  544. /* Install SIR transmit handler */
  545. dev->hard_start_xmit = &irport_hard_xmit;
  546. irport_start(self->irport);
  547.         IRDA_DEBUG(0, "%s(), using irport to change speed to %dn", __FUNCTION__, speed);
  548. irport_change_speed(self->irport, speed);
  549. }
  550. register_bank(iobase, 1);
  551. outb(((inb(iobase+IRCC_SCE_CFGA) & 0x87) | ir_mode), 
  552.      iobase+IRCC_SCE_CFGA);
  553. #ifdef SMC_669 /* Uses pin 88/89 for Rx/Tx */
  554. outb(((inb(iobase+IRCC_SCE_CFGB) & 0x3f) | IRCC_CFGB_MUX_COM), 
  555.      iobase+IRCC_SCE_CFGB);
  556. #else
  557. outb(((inb(iobase+IRCC_SCE_CFGB) & 0x3f) | IRCC_CFGB_MUX_IR),
  558.      iobase+IRCC_SCE_CFGB);
  559. #endif
  560. (void) inb(iobase+IRCC_FIFO_THRESHOLD);
  561. outb(64, iobase+IRCC_FIFO_THRESHOLD);
  562. register_bank(iobase, 4);
  563. outb((inb(iobase+IRCC_CONTROL) & 0x30) | ctrl, iobase+IRCC_CONTROL);
  564. register_bank(iobase, 0);
  565. outb(fast, iobase+IRCC_LCR_A);
  566. netif_start_queue(dev);
  567. }
  568. /*
  569.  * Function ircc_hard_xmit (skb, dev)
  570.  *
  571.  *    Transmit the frame!
  572.  *
  573.  */
  574. static int ircc_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  575. {
  576. struct irport_cb *irport;
  577. struct ircc_cb *self;
  578. unsigned long flags;
  579. s32 speed;
  580. int iobase;
  581. int mtt;
  582. irport = (struct irport_cb *) dev->priv;
  583. self = (struct ircc_cb *) irport->priv;
  584. ASSERT(self != NULL, return 0;);
  585. iobase = self->io->fir_base;
  586. netif_stop_queue(dev);
  587. /* Check if we need to change the speed after this frame */
  588. speed = irda_get_next_speed(skb);
  589. if ((speed != self->io->speed) && (speed != -1)) {
  590. /* Check for empty frame */
  591. if (!skb->len) {
  592. ircc_change_speed(self, speed); 
  593. dev_kfree_skb(skb);
  594. return 0;
  595. } else
  596. self->new_speed = speed;
  597. }
  598. spin_lock_irqsave(&self->lock, flags);
  599. memcpy(self->tx_buff.head, skb->data, skb->len);
  600. self->tx_buff.len = skb->len;
  601. self->tx_buff.data = self->tx_buff.head;
  602. mtt = irda_get_mtt(skb);
  603. if (mtt) {
  604. int bofs;
  605. /* 
  606.  * Compute how many BOFs (STA or PA's) we need to waste the
  607.  * min turn time given the speed of the link.
  608.  */
  609. bofs = mtt * (self->io->speed / 1000) / 8000;
  610. if (bofs > 4095)
  611. bofs = 4095;
  612. ircc_dma_xmit(self, iobase, bofs);
  613. } else {
  614. /* Transmit frame */
  615. ircc_dma_xmit(self, iobase, 0);
  616. }
  617. spin_unlock_irqrestore(&self->lock, flags);
  618. dev_kfree_skb(skb);
  619. return 0;
  620. }
  621. /*
  622.  * Function ircc_dma_xmit (self, iobase)
  623.  *
  624.  *    Transmit data using DMA
  625.  *
  626.  */
  627. static void ircc_dma_xmit(struct ircc_cb *self, int iobase, int bofs)
  628. {
  629. u8 ctrl;
  630. IRDA_DEBUG(2, "%s()n", __FUNCTION__);
  631. #if 0
  632. /* Disable Rx */
  633. register_bank(iobase, 0);
  634. outb(0x00, iobase+IRCC_LCR_B);
  635. #endif
  636. register_bank(iobase, 1);
  637. outb(inb(iobase+IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE, 
  638.      iobase+IRCC_SCE_CFGB);
  639. self->io->direction = IO_XMIT;
  640. /* Set BOF additional count for generating the min turn time */
  641. register_bank(iobase, 4);
  642. outb(bofs & 0xff, iobase+IRCC_BOF_COUNT_LO);
  643. ctrl = inb(iobase+IRCC_CONTROL) & 0xf0;
  644. outb(ctrl | ((bofs >> 8) & 0x0f), iobase+IRCC_BOF_COUNT_HI);
  645. /* Set max Tx frame size */
  646. outb(self->tx_buff.len >> 8, iobase+IRCC_TX_SIZE_HI);
  647. outb(self->tx_buff.len & 0xff, iobase+IRCC_TX_SIZE_LO);
  648. /* Setup DMA controller (must be done after enabling chip DMA) */
  649. setup_dma(self->io->dma, self->tx_buff.data, self->tx_buff.len, 
  650.   DMA_TX_MODE);
  651. outb(UART_MCR_OUT2, self->io->sir_base + UART_MCR);
  652. /* Enable burst mode chip Tx DMA */
  653. register_bank(iobase, 1);
  654. outb(inb(iobase+IRCC_SCE_CFGB) | IRCC_CFGB_DMA_ENABLE |
  655.      IRCC_CFGB_DMA_BURST, iobase+IRCC_SCE_CFGB);
  656. /* Enable interrupt */
  657. outb(IRCC_MASTER_INT_EN, iobase+IRCC_MASTER);
  658. register_bank(iobase, 0);
  659. outb(IRCC_IER_ACTIVE_FRAME | IRCC_IER_EOM, iobase+IRCC_IER);
  660. /* Enable transmit */
  661. outb(IRCC_LCR_B_SCE_TRANSMIT|IRCC_LCR_B_SIP_ENABLE, iobase+IRCC_LCR_B);
  662. }
  663. /*
  664.  * Function ircc_dma_xmit_complete (self)
  665.  *
  666.  *    The transfer of a frame in finished. This function will only be called 
  667.  *    by the interrupt handler
  668.  *
  669.  */
  670. static void ircc_dma_xmit_complete(struct ircc_cb *self, int iobase)
  671. {
  672. IRDA_DEBUG(2, "%s()n", __FUNCTION__);
  673. #if 0
  674. /* Disable Tx */
  675. register_bank(iobase, 0);
  676. outb(0x00, iobase+IRCC_LCR_B);
  677. #endif
  678. register_bank(self->io->fir_base, 1);
  679. outb(inb(self->io->fir_base+IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE,
  680.      self->io->fir_base+IRCC_SCE_CFGB);
  681. /* Check for underrrun! */
  682. register_bank(iobase, 0);
  683. if (inb(iobase+IRCC_LSR) & IRCC_LSR_UNDERRUN) {
  684. self->irport->stats.tx_errors++;
  685. self->irport->stats.tx_fifo_errors++;
  686. /* Reset error condition */
  687. register_bank(iobase, 0);
  688. outb(IRCC_MASTER_ERROR_RESET, iobase+IRCC_MASTER);
  689. outb(0x00, iobase+IRCC_MASTER);
  690. } else {
  691. self->irport->stats.tx_packets++;
  692. self->irport->stats.tx_bytes +=  self->tx_buff.len;
  693. }
  694. /* Check if it's time to change the speed */
  695. if (self->new_speed) {
  696. ircc_change_speed(self, self->new_speed);
  697. self->new_speed = 0;
  698. }
  699. netif_wake_queue(self->netdev);
  700. }
  701. /*
  702.  * Function ircc_dma_receive (self)
  703.  *
  704.  *    Get ready for receiving a frame. The device will initiate a DMA
  705.  *    if it starts to receive a frame.
  706.  *
  707.  */
  708. static int ircc_dma_receive(struct ircc_cb *self, int iobase) 
  709. {
  710. #if 0
  711. /* Turn off chip DMA */
  712. register_bank(iobase, 1);
  713. outb(inb(iobase+IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE, 
  714.      iobase+IRCC_SCE_CFGB);
  715. #endif
  716. setup_dma(self->io->dma, self->rx_buff.data, self->rx_buff.truesize, 
  717.   DMA_RX_MODE);
  718. /* Set max Rx frame size */
  719. register_bank(iobase, 4);
  720. outb((2050 >> 8) & 0x0f, iobase+IRCC_RX_SIZE_HI);
  721. outb(2050 & 0xff, iobase+IRCC_RX_SIZE_LO);
  722. self->io->direction = IO_RECV;
  723. self->rx_buff.data = self->rx_buff.head;
  724. /* Setup DMA controller */
  725. /* Enable receiver */
  726. register_bank(iobase, 0);
  727. outb(IRCC_LCR_B_SCE_RECEIVE | IRCC_LCR_B_SIP_ENABLE, 
  728.      iobase+IRCC_LCR_B);
  729. /* Enable burst mode chip Rx DMA */
  730. register_bank(iobase, 1);
  731. outb(inb(iobase+IRCC_SCE_CFGB) | IRCC_CFGB_DMA_ENABLE | 
  732.      IRCC_CFGB_DMA_BURST, iobase+IRCC_SCE_CFGB);
  733. return 0;
  734. }
  735. /*
  736.  * Function ircc_dma_receive_complete (self)
  737.  *
  738.  *    Finished with receiving frames
  739.  *
  740.  */
  741. static void ircc_dma_receive_complete(struct ircc_cb *self, int iobase)
  742. {
  743. struct sk_buff *skb;
  744. int len, msgcnt;
  745. IRDA_DEBUG(2, "%s()n", __FUNCTION__);
  746. #if 0
  747. /* Disable Rx */
  748. register_bank(iobase, 0);
  749. outb(0x00, iobase+IRCC_LCR_B);
  750. #endif
  751. register_bank(iobase, 0);
  752. msgcnt = inb(iobase+IRCC_LCR_B) & 0x08;
  753. IRDA_DEBUG(2, "%s: dma count = %dn",
  754. __FUNCTION__, get_dma_residue(self->io->dma));
  755. len = self->rx_buff.truesize - get_dma_residue(self->io->dma);
  756. /* Remove CRC */
  757. if (self->io->speed < 4000000)
  758. len -= 2;
  759. else
  760. len -= 4;
  761. if ((len < 2) || (len > 2050)) {
  762. WARNING("%s(), bogus len=%dn", __FUNCTION__, len);
  763. return;
  764. }
  765. IRDA_DEBUG(2, "%s: msgcnt = %d, len=%dn", __FUNCTION__, msgcnt, len);
  766. skb = dev_alloc_skb(len+1);
  767. if (!skb)  {
  768. WARNING("%s(), memory squeeze, dropping frame.n", __FUNCTION__);
  769. return;
  770. }
  771. /* Make sure IP header gets aligned */
  772. skb_reserve(skb, 1); 
  773. memcpy(skb_put(skb, len), self->rx_buff.data, len);
  774. self->irport->stats.rx_packets++;
  775. self->irport->stats.rx_bytes += len;
  776. skb->dev = self->netdev;
  777. skb->mac.raw  = skb->data;
  778. skb->protocol = htons(ETH_P_IRDA);
  779. netif_rx(skb);
  780. }
  781. /*
  782.  * Function ircc_interrupt (irq, dev_id, regs)
  783.  *
  784.  *    An interrupt from the chip has arrived. Time to do some work
  785.  *
  786.  */
  787. static void ircc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  788. {
  789. struct net_device *dev = (struct net_device *) dev_id;
  790. struct irport_cb *irport;
  791. struct ircc_cb *self;
  792. int iobase, iir;
  793. if (dev == NULL) {
  794. printk(KERN_WARNING "%s: irq %d for unknown device.n", 
  795.        driver_name, irq);
  796. return;
  797. }
  798. irport = (struct irport_cb *) dev->priv;
  799. ASSERT(irport != NULL, return;);
  800. self = (struct ircc_cb *) irport->priv;
  801. ASSERT(self != NULL, return;);
  802. /* Check if we should use the SIR interrupt handler */
  803. if (self->io->speed < 576000) {
  804. irport_interrupt(irq, dev_id, regs);
  805. return;
  806. }
  807. iobase = self->io->fir_base;
  808. spin_lock(&self->lock);
  809. register_bank(iobase, 0);
  810. iir = inb(iobase+IRCC_IIR);
  811. /* Disable interrupts */
  812. outb(0, iobase+IRCC_IER);
  813. IRDA_DEBUG(2, "%s(), iir = 0x%02xn", __FUNCTION__, iir);
  814. if (iir & IRCC_IIR_EOM) {
  815. if (self->io->direction == IO_RECV)
  816. ircc_dma_receive_complete(self, iobase);
  817. else
  818. ircc_dma_xmit_complete(self, iobase);
  819. ircc_dma_receive(self, iobase);
  820. }
  821. /* Enable interrupts again */
  822. register_bank(iobase, 0);
  823. outb(IRCC_IER_ACTIVE_FRAME|IRCC_IER_EOM, iobase+IRCC_IER);
  824. spin_unlock(&self->lock);
  825. }
  826. #if 0 /* unused */
  827. /*
  828.  * Function ircc_is_receiving (self)
  829.  *
  830.  *    Return TRUE is we are currently receiving a frame
  831.  *
  832.  */
  833. static int ircc_is_receiving(struct ircc_cb *self)
  834. {
  835. int status = FALSE;
  836. /* int iobase; */
  837. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  838. ASSERT(self != NULL, return FALSE;);
  839. IRDA_DEBUG(0, "%s: dma count = %dn",
  840. __FUNCTION__, get_dma_residue(self->io->dma));
  841. status = (self->rx_buff.state != OUTSIDE_FRAME);
  842. return status;
  843. }
  844. #endif /* unused */
  845. /*
  846.  * Function ircc_net_open (dev)
  847.  *
  848.  *    Start the device
  849.  *
  850.  */
  851. static int ircc_net_open(struct net_device *dev)
  852. {
  853. struct irport_cb *irport;
  854. struct ircc_cb *self;
  855. int iobase;
  856. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  857. ASSERT(dev != NULL, return -1;);
  858. irport = (struct irport_cb *) dev->priv;
  859. self = (struct ircc_cb *) irport->priv;
  860. ASSERT(self != NULL, return 0;);
  861. iobase = self->io->fir_base;
  862. irport_net_open(dev); /* irport allocates the irq */
  863. /*
  864.  * Always allocate the DMA channel after the IRQ,
  865.  * and clean up on failure.
  866.  */
  867. if (request_dma(self->io->dma, dev->name)) {
  868. irport_net_close(dev);
  869. WARNING("%s(), unable to allocate DMA=%dn", __FUNCTION__, self->io->dma);
  870. return -EAGAIN;
  871. }
  872. MOD_INC_USE_COUNT;
  873. return 0;
  874. }
  875. /*
  876.  * Function ircc_net_close (dev)
  877.  *
  878.  *    Stop the device
  879.  *
  880.  */
  881. static int ircc_net_close(struct net_device *dev)
  882. {
  883. struct irport_cb *irport;
  884. struct ircc_cb *self;
  885. int iobase;
  886. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  887. ASSERT(dev != NULL, return -1;);
  888. irport = (struct irport_cb *) dev->priv;
  889. self = (struct ircc_cb *) irport->priv;
  890. ASSERT(self != NULL, return 0;);
  891. iobase = self->io->fir_base;
  892. irport_net_close(dev);
  893. disable_dma(self->io->dma);
  894. free_dma(self->io->dma);
  895. MOD_DEC_USE_COUNT;
  896. return 0;
  897. }
  898. static void ircc_suspend(struct ircc_cb *self)
  899. {
  900. MESSAGE("%s, Suspendingn", driver_name);
  901. if (self->io->suspended)
  902. return;
  903. ircc_net_close(self->netdev);
  904. self->io->suspended = 1;
  905. }
  906. static void ircc_wakeup(struct ircc_cb *self)
  907. {
  908. unsigned long flags;
  909. if (!self->io->suspended)
  910. return;
  911. save_flags(flags);
  912. cli();
  913. ircc_net_open(self->netdev);
  914. restore_flags(flags);
  915. MESSAGE("%s, Waking upn", driver_name);
  916. }
  917. static int ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data)
  918. {
  919.         struct ircc_cb *self = (struct ircc_cb*) dev->data;
  920.         if (self) {
  921.                 switch (rqst) {
  922.                 case PM_SUSPEND:
  923.                         ircc_suspend(self);
  924.                         break;
  925.                 case PM_RESUME:
  926.                         ircc_wakeup(self);
  927.                         break;
  928.                 }
  929.         }
  930. return 0;
  931. }
  932. #ifdef MODULE
  933. /*
  934.  * Function ircc_close (self)
  935.  *
  936.  *    Close driver instance
  937.  *
  938.  */
  939. #ifdef MODULE
  940. static int __exit ircc_close(struct ircc_cb *self)
  941. {
  942. int iobase;
  943. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  944. ASSERT(self != NULL, return -1;);
  945.         iobase = self->irport->io.fir_base;
  946. irport_close(self->irport);
  947. /* Stop interrupts */
  948. register_bank(iobase, 0);
  949. outb(0, iobase+IRCC_IER);
  950. outb(IRCC_MASTER_RESET, iobase+IRCC_MASTER);
  951. outb(0x00, iobase+IRCC_MASTER);
  952. #if 0
  953. /* Reset to SIR mode */
  954. register_bank(iobase, 1);
  955.         outb(IRCC_CFGA_IRDA_SIR_A|IRCC_CFGA_TX_POLARITY, iobase+IRCC_SCE_CFGA);
  956.         outb(IRCC_CFGB_IR, iobase+IRCC_SCE_CFGB);
  957. #endif
  958. /* Release the PORT that this driver is using */
  959. IRDA_DEBUG(0, "%s(), releasing 0x%03xn", 
  960. __FUNCTION__, self->io->fir_base);
  961. release_region(self->io->fir_base, self->io->fir_ext);
  962. if (self->tx_buff.head)
  963. kfree(self->tx_buff.head);
  964. if (self->rx_buff.head)
  965. kfree(self->rx_buff.head);
  966. kfree(self);
  967. return 0;
  968. }
  969. #endif /* MODULE */
  970. static int __init smc_init(void)
  971. {
  972. return ircc_init();
  973. }
  974. void __exit smc_cleanup(void)
  975. {
  976. int i;
  977. IRDA_DEBUG(0, "%s()n", __FUNCTION__);
  978. for (i=0; i < 2; i++) {
  979. if (dev_self[i])
  980. ircc_close(dev_self[i]);
  981. }
  982. }
  983. module_init(smc_init);
  984. module_exit(smc_cleanup);
  985.  
  986. MODULE_AUTHOR("Thomas Davis <tadavis@jps.net>");
  987. MODULE_DESCRIPTION("SMC IrCC controller driver");
  988. MODULE_LICENSE("GPL");
  989. MODULE_PARM(ircc_dma, "1i");
  990. MODULE_PARM_DESC(ircc_dma, "DMA channel");
  991. MODULE_PARM(ircc_irq, "1i");
  992. MODULE_PARM_DESC(ircc_irq, "IRQ line");
  993. MODULE_PARM(ircc_fir, "1-4i");
  994. MODULE_PARM_DESC(ircc_fir, "FIR Base Address");
  995. MODULE_PARM(ircc_sir, "1-4i");
  996. MODULE_PARM_DESC(ircc_sir, "SIR Base Address");
  997. MODULE_PARM(ircc_cfg, "1-4i");
  998. MODULE_PARM_DESC(ircc_cfg, "Configuration register base address");
  999. #endif /* MODULE */