smc-ircc.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:29k
源码类别:

嵌入式Linux

开发平台:

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, __FUNCTION__ "()n");
  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, __FUNCTION__ "()n");
  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, __FUNCTION__ "()n");
  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, __FUNCTION__ "()n");
  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, __FUNCTION__ ": can't get cfg_base of 0x%03xn",
  278.    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, __FUNCTION__ ": can't get cfg_base of 0x%03xn",
  290.    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, __FUNCTION__ "n");
  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, __FUNCTION__ 
  324. " Try to open all known SMC chipsetsn");
  325. if (!smc_superio_fdc(0x3f0))
  326. ret=0;
  327. if (!smc_superio_fdc(0x370))
  328. ret=0;
  329. if (!smc_superio_fdc(0xe0))
  330. ret=0;
  331. if (!smc_superio_lpc(0x2e))
  332. ret=0;
  333. if (!smc_superio_lpc(0x4e))
  334. ret=0;
  335. return ret;
  336. }
  337. /*
  338.  * Function ircc_open (iobase, irq)
  339.  *
  340.  *    Try to open driver instance
  341.  *
  342.  */
  343. static int __init ircc_open(unsigned int fir_base, unsigned int sir_base)
  344. {
  345. struct ircc_cb *self;
  346.         struct irport_cb *irport;
  347. unsigned char low, high, chip, config, dma, irq, version;
  348. IRDA_DEBUG(0, __FUNCTION__ "n");
  349. if (check_region(fir_base, CHIP_IO_EXTENT) < 0) {
  350. IRDA_DEBUG(0, __FUNCTION__ ": can't get fir_base of 0x%03xn",
  351.    fir_base);
  352. return -ENODEV;
  353. }
  354. #if POSSIBLE_USED_BY_SERIAL_DRIVER
  355. if (check_region(sir_base, CHIP_IO_EXTENT) < 0) {
  356. IRDA_DEBUG(0, __FUNCTION__ ": can't get sir_base of 0x%03xn",
  357.    sir_base);
  358. return -ENODEV;
  359. }
  360. #endif
  361. register_bank(fir_base, 3);
  362. high    = inb(fir_base+IRCC_ID_HIGH);
  363. low     = inb(fir_base+IRCC_ID_LOW);
  364. chip    = inb(fir_base+IRCC_CHIP_ID);
  365. version = inb(fir_base+IRCC_VERSION);
  366. config  = inb(fir_base+IRCC_INTERFACE);
  367. irq     = config >> 4 & 0x0f;
  368. dma     = config & 0x0f;
  369.         if (high != 0x10 || low != 0xb8 || (chip != 0xf1 && chip != 0xf2)) { 
  370.         IRDA_DEBUG(0, __FUNCTION__ 
  371.    "(), addr 0x%04x - no device found!n", fir_base);
  372. return -ENODEV;
  373. }
  374. MESSAGE("SMC IrDA Controller foundn IrCC version %d.%d, "
  375. "firport 0x%03x, sirport 0x%03x dma=%d, irq=%dn",
  376. chip & 0x0f, version, fir_base, sir_base, dma, irq);
  377. if (dev_count>DIM(dev_self)) {
  378.         IRDA_DEBUG(0, __FUNCTION__ 
  379.    "(), to many devices!n");
  380. return -ENOMEM;
  381. }
  382. /*
  383.  *  Allocate new instance of the driver
  384.  */
  385. self = kmalloc(sizeof(struct ircc_cb), GFP_KERNEL);
  386. if (self == NULL) {
  387. ERROR("%s, Can't allocate memory for control block!n",
  388.                       driver_name);
  389. return -ENOMEM;
  390. }
  391. memset(self, 0, sizeof(struct ircc_cb));
  392. spin_lock_init(&self->lock);
  393. /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
  394. self->rx_buff.truesize = 4000; 
  395. self->tx_buff.truesize = 4000;
  396. self->rx_buff.head = (u8 *) kmalloc(self->rx_buff.truesize,
  397.       GFP_KERNEL|GFP_DMA);
  398. if (self->rx_buff.head == NULL) {
  399. ERROR("%s, Can't allocate memory for receive buffer!n",
  400.                       driver_name);
  401. kfree(self);
  402. return -ENOMEM;
  403. }
  404. self->tx_buff.head = (u8 *) kmalloc(self->tx_buff.truesize, 
  405.       GFP_KERNEL|GFP_DMA);
  406. if (self->tx_buff.head == NULL) {
  407. ERROR("%s, Can't allocate memory for transmit buffer!n",
  408.                       driver_name);
  409. kfree(self->rx_buff.head);
  410. kfree(self);
  411. return -ENOMEM;
  412. }
  413. irport = irport_open(dev_count, sir_base, irq);
  414. if (!irport) {
  415. kfree(self->tx_buff.head);
  416. kfree(self->rx_buff.head);
  417. kfree(self);
  418. return -ENODEV;
  419. }
  420. memset(self->rx_buff.head, 0, self->rx_buff.truesize);
  421. memset(self->tx_buff.head, 0, self->tx_buff.truesize);
  422.    
  423. /* Need to store self somewhere */
  424. dev_self[dev_count++] = self;
  425. /* Steal the network device from irport */
  426. self->netdev = irport->netdev;
  427. self->irport = irport;
  428. irport->priv = self;
  429. /* Initialize IO */
  430. self->io           = &irport->io;
  431. self->io->fir_base  = fir_base;
  432.         self->io->sir_base  = sir_base; /* Used by irport */
  433.         self->io->fir_ext   = CHIP_IO_EXTENT;
  434.         self->io->sir_ext   = 8; /* Used by irport */
  435. if (ircc_irq < 255) {
  436. if (ircc_irq!=irq)
  437. MESSAGE("%s, Overriding IRQ - chip says %d, using %dn",
  438. driver_name, self->io->irq, ircc_irq);
  439. self->io->irq = ircc_irq;
  440. }
  441. else
  442. self->io->irq = irq;
  443. if (ircc_dma < 255) {
  444. if (ircc_dma!=dma)
  445. MESSAGE("%s, Overriding DMA - chip says %d, using %dn",
  446. driver_name, self->io->dma, ircc_dma);
  447. self->io->dma = ircc_dma;
  448. }
  449. else
  450. self->io->dma = dma;
  451. request_region(fir_base, CHIP_IO_EXTENT, driver_name);
  452. /* Initialize QoS for this device */
  453. irda_init_max_qos_capabilies(&irport->qos);
  454. /* The only value we must override it the baudrate */
  455. irport->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  456. IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8);
  457. irport->qos.min_turn_time.bits = 0x07;
  458. irport->qos.window_size.bits = 0x01;
  459. irda_qos_bits_to_value(&irport->qos);
  460. irport->flags = IFF_FIR|IFF_MIR|IFF_SIR|IFF_DMA|IFF_PIO;
  461. self->rx_buff.in_frame = FALSE;
  462. self->rx_buff.state = OUTSIDE_FRAME;
  463. self->tx_buff.data = self->tx_buff.head;
  464. self->rx_buff.data = self->rx_buff.head;
  465. /* Override the speed change function, since we must control it now */
  466. irport->change_speed = &ircc_change_speed;
  467. irport->interrupt    = &ircc_interrupt;
  468. self->netdev->open   = &ircc_net_open;
  469. self->netdev->stop   = &ircc_net_close;
  470. irport_start(self->irport);
  471.         self->pmdev = pm_register(PM_SYS_DEV, PM_SYS_IRDA, ircc_pmproc);
  472.         if (self->pmdev)
  473.                 self->pmdev->data = self;
  474. /* Power on device */
  475. outb(0x00, fir_base+IRCC_MASTER);
  476. return 0;
  477. }
  478. /*
  479.  * Function ircc_change_speed (self, baud)
  480.  *
  481.  *    Change the speed of the device
  482.  *
  483.  */
  484. static void ircc_change_speed(void *priv, u32 speed)
  485. {
  486. int iobase, ir_mode, ctrl, fast; 
  487. struct ircc_cb *self = (struct ircc_cb *) priv;
  488. struct net_device *dev;
  489. IRDA_DEBUG(0, __FUNCTION__ "n");
  490. ASSERT(self != NULL, return;);
  491. dev = self->netdev;
  492. iobase = self->io->fir_base;
  493. /* Update accounting for new speed */
  494. self->io->speed = speed;
  495. outb(IRCC_MASTER_RESET, iobase+IRCC_MASTER);
  496. outb(0x00, iobase+IRCC_MASTER);
  497. switch (speed) {
  498. default:
  499. IRDA_DEBUG(0, __FUNCTION__ "(), unknown baud rate of %dn", 
  500.    speed);
  501. /* FALLTHROUGH */
  502. case 9600:
  503. case 19200:
  504. case 38400:
  505. case 57600:
  506. case 115200:
  507. ir_mode = IRCC_CFGA_IRDA_SIR_A;
  508. ctrl = 0;
  509. fast = 0;
  510. break;
  511. case 576000:
  512. ir_mode = IRCC_CFGA_IRDA_HDLC;
  513. ctrl = IRCC_CRC;
  514. fast = 0;
  515. IRDA_DEBUG(0, __FUNCTION__ "(), handling baud of 576000n");
  516. break;
  517. case 1152000:
  518. ir_mode = IRCC_CFGA_IRDA_HDLC;
  519. ctrl = IRCC_1152 | IRCC_CRC;
  520. fast = 0;
  521. IRDA_DEBUG(0, __FUNCTION__ "(), handling baud of 1152000n");
  522. break;
  523. case 4000000:
  524. ir_mode = IRCC_CFGA_IRDA_4PPM;
  525. ctrl = IRCC_CRC;
  526. fast = IRCC_LCR_A_FAST;
  527. IRDA_DEBUG(0, __FUNCTION__ "(), handling baud of 4000000n");
  528. break;
  529. }
  530. register_bank(iobase, 0);
  531. outb(0, iobase+IRCC_IER);
  532. outb(IRCC_MASTER_INT_EN, iobase+IRCC_MASTER);
  533. /* Make special FIR init if necessary */
  534. if (speed > 115200) {
  535. irport_stop(self->irport);
  536. /* Install FIR transmit handler */
  537. dev->hard_start_xmit = &ircc_hard_xmit;
  538. /* 
  539.  * Don't know why we have to do this, but FIR interrupts 
  540.  * stops working if we remove it.
  541.  */
  542. /* outb(UART_MCR_OUT2, self->io->sir_base + UART_MCR); */
  543. /* Be ready for incoming frames */
  544. ircc_dma_receive(self, iobase);
  545. } else {
  546. /* Install SIR transmit handler */
  547. dev->hard_start_xmit = &irport_hard_xmit;
  548. irport_start(self->irport);
  549.         IRDA_DEBUG(0, __FUNCTION__ 
  550.    "(), using irport to change speed to %dn", speed);
  551. irport_change_speed(self->irport, speed);
  552. }
  553. register_bank(iobase, 1);
  554. outb(((inb(iobase+IRCC_SCE_CFGA) & 0x87) | ir_mode), 
  555.      iobase+IRCC_SCE_CFGA);
  556. #ifdef SMC_669 /* Uses pin 88/89 for Rx/Tx */
  557. outb(((inb(iobase+IRCC_SCE_CFGB) & 0x3f) | IRCC_CFGB_MUX_COM), 
  558.      iobase+IRCC_SCE_CFGB);
  559. #else
  560. outb(((inb(iobase+IRCC_SCE_CFGB) & 0x3f) | IRCC_CFGB_MUX_IR),
  561.      iobase+IRCC_SCE_CFGB);
  562. #endif
  563. (void) inb(iobase+IRCC_FIFO_THRESHOLD);
  564. outb(64, iobase+IRCC_FIFO_THRESHOLD);
  565. register_bank(iobase, 4);
  566. outb((inb(iobase+IRCC_CONTROL) & 0x30) | ctrl, iobase+IRCC_CONTROL);
  567. register_bank(iobase, 0);
  568. outb(fast, iobase+IRCC_LCR_A);
  569. netif_start_queue(dev);
  570. }
  571. /*
  572.  * Function ircc_hard_xmit (skb, dev)
  573.  *
  574.  *    Transmit the frame!
  575.  *
  576.  */
  577. static int ircc_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  578. {
  579. struct irport_cb *irport;
  580. struct ircc_cb *self;
  581. unsigned long flags;
  582. s32 speed;
  583. int iobase;
  584. int mtt;
  585. irport = (struct irport_cb *) dev->priv;
  586. self = (struct ircc_cb *) irport->priv;
  587. ASSERT(self != NULL, return 0;);
  588. iobase = self->io->fir_base;
  589. netif_stop_queue(dev);
  590. /* Check if we need to change the speed after this frame */
  591. speed = irda_get_next_speed(skb);
  592. if ((speed != self->io->speed) && (speed != -1)) {
  593. /* Check for empty frame */
  594. if (!skb->len) {
  595. ircc_change_speed(self, speed); 
  596. dev_kfree_skb(skb);
  597. return 0;
  598. } else
  599. self->new_speed = speed;
  600. }
  601. spin_lock_irqsave(&self->lock, flags);
  602. memcpy(self->tx_buff.head, skb->data, skb->len);
  603. self->tx_buff.len = skb->len;
  604. self->tx_buff.data = self->tx_buff.head;
  605. mtt = irda_get_mtt(skb);
  606. if (mtt) {
  607. int bofs;
  608. /* 
  609.  * Compute how many BOFs (STA or PA's) we need to waste the
  610.  * min turn time given the speed of the link.
  611.  */
  612. bofs = mtt * (self->io->speed / 1000) / 8000;
  613. if (bofs > 4095)
  614. bofs = 4095;
  615. ircc_dma_xmit(self, iobase, bofs);
  616. } else {
  617. /* Transmit frame */
  618. ircc_dma_xmit(self, iobase, 0);
  619. }
  620. spin_unlock_irqrestore(&self->lock, flags);
  621. dev_kfree_skb(skb);
  622. return 0;
  623. }
  624. /*
  625.  * Function ircc_dma_xmit (self, iobase)
  626.  *
  627.  *    Transmit data using DMA
  628.  *
  629.  */
  630. static void ircc_dma_xmit(struct ircc_cb *self, int iobase, int bofs)
  631. {
  632. u8 ctrl;
  633. IRDA_DEBUG(2, __FUNCTION__ "n");
  634. #if 0
  635. /* Disable Rx */
  636. register_bank(iobase, 0);
  637. outb(0x00, iobase+IRCC_LCR_B);
  638. #endif
  639. register_bank(iobase, 1);
  640. outb(inb(iobase+IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE, 
  641.      iobase+IRCC_SCE_CFGB);
  642. self->io->direction = IO_XMIT;
  643. /* Set BOF additional count for generating the min turn time */
  644. register_bank(iobase, 4);
  645. outb(bofs & 0xff, iobase+IRCC_BOF_COUNT_LO);
  646. ctrl = inb(iobase+IRCC_CONTROL) & 0xf0;
  647. outb(ctrl | ((bofs >> 8) & 0x0f), iobase+IRCC_BOF_COUNT_HI);
  648. /* Set max Tx frame size */
  649. outb(self->tx_buff.len >> 8, iobase+IRCC_TX_SIZE_HI);
  650. outb(self->tx_buff.len & 0xff, iobase+IRCC_TX_SIZE_LO);
  651. /* Setup DMA controller (must be done after enabling chip DMA) */
  652. setup_dma(self->io->dma, self->tx_buff.data, self->tx_buff.len, 
  653.   DMA_TX_MODE);
  654. outb(UART_MCR_OUT2, self->io->sir_base + UART_MCR);
  655. /* Enable burst mode chip Tx DMA */
  656. register_bank(iobase, 1);
  657. outb(inb(iobase+IRCC_SCE_CFGB) | IRCC_CFGB_DMA_ENABLE |
  658.      IRCC_CFGB_DMA_BURST, iobase+IRCC_SCE_CFGB);
  659. /* Enable interrupt */
  660. outb(IRCC_MASTER_INT_EN, iobase+IRCC_MASTER);
  661. register_bank(iobase, 0);
  662. outb(IRCC_IER_ACTIVE_FRAME | IRCC_IER_EOM, iobase+IRCC_IER);
  663. /* Enable transmit */
  664. outb(IRCC_LCR_B_SCE_TRANSMIT|IRCC_LCR_B_SIP_ENABLE, iobase+IRCC_LCR_B);
  665. }
  666. /*
  667.  * Function ircc_dma_xmit_complete (self)
  668.  *
  669.  *    The transfer of a frame in finished. This function will only be called 
  670.  *    by the interrupt handler
  671.  *
  672.  */
  673. static void ircc_dma_xmit_complete(struct ircc_cb *self, int iobase)
  674. {
  675. IRDA_DEBUG(2, __FUNCTION__ "n");
  676. #if 0
  677. /* Disable Tx */
  678. register_bank(iobase, 0);
  679. outb(0x00, iobase+IRCC_LCR_B);
  680. #endif
  681. register_bank(self->io->fir_base, 1);
  682. outb(inb(self->io->fir_base+IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE,
  683.      self->io->fir_base+IRCC_SCE_CFGB);
  684. /* Check for underrrun! */
  685. register_bank(iobase, 0);
  686. if (inb(iobase+IRCC_LSR) & IRCC_LSR_UNDERRUN) {
  687. self->irport->stats.tx_errors++;
  688. self->irport->stats.tx_fifo_errors++;
  689. /* Reset error condition */
  690. register_bank(iobase, 0);
  691. outb(IRCC_MASTER_ERROR_RESET, iobase+IRCC_MASTER);
  692. outb(0x00, iobase+IRCC_MASTER);
  693. } else {
  694. self->irport->stats.tx_packets++;
  695. self->irport->stats.tx_bytes +=  self->tx_buff.len;
  696. }
  697. /* Check if it's time to change the speed */
  698. if (self->new_speed) {
  699. ircc_change_speed(self, self->new_speed);
  700. self->new_speed = 0;
  701. }
  702. netif_wake_queue(self->netdev);
  703. }
  704. /*
  705.  * Function ircc_dma_receive (self)
  706.  *
  707.  *    Get ready for receiving a frame. The device will initiate a DMA
  708.  *    if it starts to receive a frame.
  709.  *
  710.  */
  711. static int ircc_dma_receive(struct ircc_cb *self, int iobase) 
  712. {
  713. #if 0
  714. /* Turn off chip DMA */
  715. register_bank(iobase, 1);
  716. outb(inb(iobase+IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE, 
  717.      iobase+IRCC_SCE_CFGB);
  718. #endif
  719. setup_dma(self->io->dma, self->rx_buff.data, self->rx_buff.truesize, 
  720.   DMA_RX_MODE);
  721. /* Set max Rx frame size */
  722. register_bank(iobase, 4);
  723. outb((2050 >> 8) & 0x0f, iobase+IRCC_RX_SIZE_HI);
  724. outb(2050 & 0xff, iobase+IRCC_RX_SIZE_LO);
  725. self->io->direction = IO_RECV;
  726. self->rx_buff.data = self->rx_buff.head;
  727. /* Setup DMA controller */
  728. /* Enable receiver */
  729. register_bank(iobase, 0);
  730. outb(IRCC_LCR_B_SCE_RECEIVE | IRCC_LCR_B_SIP_ENABLE, 
  731.      iobase+IRCC_LCR_B);
  732. /* Enable burst mode chip Rx DMA */
  733. register_bank(iobase, 1);
  734. outb(inb(iobase+IRCC_SCE_CFGB) | IRCC_CFGB_DMA_ENABLE | 
  735.      IRCC_CFGB_DMA_BURST, iobase+IRCC_SCE_CFGB);
  736. return 0;
  737. }
  738. /*
  739.  * Function ircc_dma_receive_complete (self)
  740.  *
  741.  *    Finished with receiving frames
  742.  *
  743.  */
  744. static void ircc_dma_receive_complete(struct ircc_cb *self, int iobase)
  745. {
  746. struct sk_buff *skb;
  747. int len, msgcnt;
  748. IRDA_DEBUG(2, __FUNCTION__ "n");
  749. #if 0
  750. /* Disable Rx */
  751. register_bank(iobase, 0);
  752. outb(0x00, iobase+IRCC_LCR_B);
  753. #endif
  754. register_bank(iobase, 0);
  755. msgcnt = inb(iobase+IRCC_LCR_B) & 0x08;
  756. IRDA_DEBUG(2, __FUNCTION__ ": dma count = %dn",
  757.    get_dma_residue(self->io->dma));
  758. len = self->rx_buff.truesize - get_dma_residue(self->io->dma);
  759. /* Remove CRC */
  760. if (self->io->speed < 4000000)
  761. len -= 2;
  762. else
  763. len -= 4;
  764. if ((len < 2) || (len > 2050)) {
  765. WARNING(__FUNCTION__ "(), bogus len=%dn", len);
  766. return;
  767. }
  768. IRDA_DEBUG(2, __FUNCTION__ ": msgcnt = %d, len=%dn", msgcnt, len);
  769. skb = dev_alloc_skb(len+1);
  770. if (!skb)  {
  771. WARNING(__FUNCTION__ "(), memory squeeze, dropping frame.n");
  772. return;
  773. }
  774. /* Make sure IP header gets aligned */
  775. skb_reserve(skb, 1); 
  776. memcpy(skb_put(skb, len), self->rx_buff.data, len);
  777. self->irport->stats.rx_packets++;
  778. self->irport->stats.rx_bytes += len;
  779. skb->dev = self->netdev;
  780. skb->mac.raw  = skb->data;
  781. skb->protocol = htons(ETH_P_IRDA);
  782. netif_rx(skb);
  783. }
  784. /*
  785.  * Function ircc_interrupt (irq, dev_id, regs)
  786.  *
  787.  *    An interrupt from the chip has arrived. Time to do some work
  788.  *
  789.  */
  790. static void ircc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  791. {
  792. struct net_device *dev = (struct net_device *) dev_id;
  793. struct irport_cb *irport;
  794. struct ircc_cb *self;
  795. int iobase, iir;
  796. if (dev == NULL) {
  797. printk(KERN_WARNING "%s: irq %d for unknown device.n", 
  798.        driver_name, irq);
  799. return;
  800. }
  801. irport = (struct irport_cb *) dev->priv;
  802. ASSERT(irport != NULL, return;);
  803. self = (struct ircc_cb *) irport->priv;
  804. ASSERT(self != NULL, return;);
  805. /* Check if we should use the SIR interrupt handler */
  806. if (self->io->speed < 576000) {
  807. irport_interrupt(irq, dev_id, regs);
  808. return;
  809. }
  810. iobase = self->io->fir_base;
  811. spin_lock(&self->lock);
  812. register_bank(iobase, 0);
  813. iir = inb(iobase+IRCC_IIR);
  814. /* Disable interrupts */
  815. outb(0, iobase+IRCC_IER);
  816. IRDA_DEBUG(2, __FUNCTION__ "(), iir = 0x%02xn", iir);
  817. if (iir & IRCC_IIR_EOM) {
  818. if (self->io->direction == IO_RECV)
  819. ircc_dma_receive_complete(self, iobase);
  820. else
  821. ircc_dma_xmit_complete(self, iobase);
  822. ircc_dma_receive(self, iobase);
  823. }
  824. /* Enable interrupts again */
  825. register_bank(iobase, 0);
  826. outb(IRCC_IER_ACTIVE_FRAME|IRCC_IER_EOM, iobase+IRCC_IER);
  827. spin_unlock(&self->lock);
  828. }
  829. #if 0 /* unused */
  830. /*
  831.  * Function ircc_is_receiving (self)
  832.  *
  833.  *    Return TRUE is we are currently receiving a frame
  834.  *
  835.  */
  836. static int ircc_is_receiving(struct ircc_cb *self)
  837. {
  838. int status = FALSE;
  839. /* int iobase; */
  840. IRDA_DEBUG(0, __FUNCTION__ "n");
  841. ASSERT(self != NULL, return FALSE;);
  842. IRDA_DEBUG(0, __FUNCTION__ ": dma count = %dn",
  843.    get_dma_residue(self->io->dma));
  844. status = (self->rx_buff.state != OUTSIDE_FRAME);
  845. return status;
  846. }
  847. #endif /* unused */
  848. /*
  849.  * Function ircc_net_open (dev)
  850.  *
  851.  *    Start the device
  852.  *
  853.  */
  854. static int ircc_net_open(struct net_device *dev)
  855. {
  856. struct irport_cb *irport;
  857. struct ircc_cb *self;
  858. int iobase;
  859. IRDA_DEBUG(0, __FUNCTION__ "n");
  860. ASSERT(dev != NULL, return -1;);
  861. irport = (struct irport_cb *) dev->priv;
  862. self = (struct ircc_cb *) irport->priv;
  863. ASSERT(self != NULL, return 0;);
  864. iobase = self->io->fir_base;
  865. irport_net_open(dev); /* irport allocates the irq */
  866. /*
  867.  * Always allocate the DMA channel after the IRQ,
  868.  * and clean up on failure.
  869.  */
  870. if (request_dma(self->io->dma, dev->name)) {
  871. irport_net_close(dev);
  872. WARNING(__FUNCTION__ "(), unable to allocate DMA=%dn", self->io->dma);
  873. return -EAGAIN;
  874. }
  875. MOD_INC_USE_COUNT;
  876. return 0;
  877. }
  878. /*
  879.  * Function ircc_net_close (dev)
  880.  *
  881.  *    Stop the device
  882.  *
  883.  */
  884. static int ircc_net_close(struct net_device *dev)
  885. {
  886. struct irport_cb *irport;
  887. struct ircc_cb *self;
  888. int iobase;
  889. IRDA_DEBUG(0, __FUNCTION__ "n");
  890. ASSERT(dev != NULL, return -1;);
  891. irport = (struct irport_cb *) dev->priv;
  892. self = (struct ircc_cb *) irport->priv;
  893. ASSERT(self != NULL, return 0;);
  894. iobase = self->io->fir_base;
  895. irport_net_close(dev);
  896. disable_dma(self->io->dma);
  897. free_dma(self->io->dma);
  898. MOD_DEC_USE_COUNT;
  899. return 0;
  900. }
  901. static void ircc_suspend(struct ircc_cb *self)
  902. {
  903. MESSAGE("%s, Suspendingn", driver_name);
  904. if (self->io->suspended)
  905. return;
  906. ircc_net_close(self->netdev);
  907. self->io->suspended = 1;
  908. }
  909. static void ircc_wakeup(struct ircc_cb *self)
  910. {
  911. unsigned long flags;
  912. if (!self->io->suspended)
  913. return;
  914. save_flags(flags);
  915. cli();
  916. ircc_net_open(self->netdev);
  917. restore_flags(flags);
  918. MESSAGE("%s, Waking upn", driver_name);
  919. }
  920. static int ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data)
  921. {
  922.         struct ircc_cb *self = (struct ircc_cb*) dev->data;
  923.         if (self) {
  924.                 switch (rqst) {
  925.                 case PM_SUSPEND:
  926.                         ircc_suspend(self);
  927.                         break;
  928.                 case PM_RESUME:
  929.                         ircc_wakeup(self);
  930.                         break;
  931.                 }
  932.         }
  933. return 0;
  934. }
  935. #ifdef MODULE
  936. /*
  937.  * Function ircc_close (self)
  938.  *
  939.  *    Close driver instance
  940.  *
  941.  */
  942. #ifdef MODULE
  943. static int __exit ircc_close(struct ircc_cb *self)
  944. {
  945. int iobase;
  946. IRDA_DEBUG(0, __FUNCTION__ "n");
  947. ASSERT(self != NULL, return -1;);
  948.         iobase = self->irport->io.fir_base;
  949. irport_close(self->irport);
  950. /* Stop interrupts */
  951. register_bank(iobase, 0);
  952. outb(0, iobase+IRCC_IER);
  953. outb(IRCC_MASTER_RESET, iobase+IRCC_MASTER);
  954. outb(0x00, iobase+IRCC_MASTER);
  955. #if 0
  956. /* Reset to SIR mode */
  957. register_bank(iobase, 1);
  958.         outb(IRCC_CFGA_IRDA_SIR_A|IRCC_CFGA_TX_POLARITY, iobase+IRCC_SCE_CFGA);
  959.         outb(IRCC_CFGB_IR, iobase+IRCC_SCE_CFGB);
  960. #endif
  961. /* Release the PORT that this driver is using */
  962. IRDA_DEBUG(0, __FUNCTION__ "(), releasing 0x%03xn", 
  963.    self->io->fir_base);
  964. release_region(self->io->fir_base, self->io->fir_ext);
  965. if (self->tx_buff.head)
  966. kfree(self->tx_buff.head);
  967. if (self->rx_buff.head)
  968. kfree(self->rx_buff.head);
  969. kfree(self);
  970. return 0;
  971. }
  972. #endif /* MODULE */
  973. int __init smc_init(void)
  974. {
  975. return ircc_init();
  976. }
  977. void __exit smc_cleanup(void)
  978. {
  979. int i;
  980. IRDA_DEBUG(0, __FUNCTION__ "n");
  981. for (i=0; i < 2; i++) {
  982. if (dev_self[i])
  983. ircc_close(dev_self[i]);
  984. }
  985. }
  986. module_init(smc_init);
  987. module_exit(smc_cleanup);
  988.  
  989. MODULE_AUTHOR("Thomas Davis <tadavis@jps.net>");
  990. MODULE_DESCRIPTION("SMC IrCC controller driver");
  991. MODULE_LICENSE("GPL");
  992. MODULE_PARM(ircc_dma, "1i");
  993. MODULE_PARM_DESC(ircc_dma, "DMA channel");
  994. MODULE_PARM(ircc_irq, "1i");
  995. MODULE_PARM_DESC(ircc_irq, "IRQ line");
  996. MODULE_PARM(ircc_fir, "1-4i");
  997. MODULE_PARM_DESC(ircc_fir, "FIR Base Address");
  998. MODULE_PARM(ircc_sir, "1-4i");
  999. MODULE_PARM_DESC(ircc_sir, "SIR Base Address");
  1000. MODULE_PARM(ircc_cfg, "1-4i");
  1001. MODULE_PARM_DESC(ircc_cfg, "Configuration register base address");
  1002. #endif /* MODULE */