irport.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /*********************************************************************
  2. *
  3. * Filename: irport.c
  4. * Version: 1.0
  5. * Description: Half duplex serial port SIR driver for IrDA.
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Aug 3 13:49:59 1997
  9. * Modified at: Fri Jan 28 20:22:38 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. * Sources: serial.c by Linus Torvalds
  12. *
  13. * Copyright (c) 1997, 1998, 1999-2000 Dag Brattli, All Rights Reserved.
  14. * Copyright (c) 2000-2003 Jean Tourrilhes, All Rights Reserved.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. *
  31. * This driver is ment to be a small half duplex serial driver to be
  32. * used for IR-chipsets that has a UART (16550) compatibility mode.
  33. * Eventually it will replace irtty, because of irtty has some
  34. * problems that is hard to get around when we don't have control
  35. * over the serial driver. This driver may also be used by FIR
  36. * drivers to handle SIR mode for them.
  37. *
  38. ********************************************************************/
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/types.h>
  42. #include <linux/ioport.h>
  43. #include <linux/slab.h>
  44. #include <linux/string.h>
  45. #include <linux/skbuff.h>
  46. #include <linux/serial_reg.h>
  47. #include <linux/errno.h>
  48. #include <linux/init.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/delay.h>
  51. #include <linux/rtnetlink.h>
  52. #include <linux/bitops.h>
  53. #include <asm/system.h>
  54. #include <asm/io.h>
  55. #include <net/irda/irda.h>
  56. #include <net/irda/wrapper.h>
  57. #include "irport.h"
  58. #define IO_EXTENT 8
  59. /*
  60. * Currently you'll need to set these values using insmod like this:
  61. * insmod irport io=0x3e8 irq=11
  62. */
  63. static unsigned int io[] = { ~0, ~0, ~0, ~0 };
  64. static unsigned int irq[] = { 0, 0, 0, 0 };
  65. static unsigned int qos_mtt_bits = 0x03;
  66. static struct irport_cb *dev_self[] = { NULL, NULL, NULL, NULL};
  67. static char *driver_name = "irport";
  68. static inline void irport_write_wakeup(struct irport_cb *self);
  69. static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len);
  70. static inline void irport_receive(struct irport_cb *self);
  71. static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq,
  72. int cmd);
  73. static inline int irport_is_receiving(struct irport_cb *self);
  74. static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts);
  75. static int irport_raw_write(struct net_device *dev, __u8 *buf, int len);
  76. static struct net_device_stats *irport_net_get_stats(struct net_device *dev);
  77. static int irport_change_speed_complete(struct irda_task *task);
  78. static void irport_timeout(struct net_device *dev);
  79. static irqreturn_t irport_interrupt(int irq, void *dev_id,
  80. struct pt_regs *regs);
  81. static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev);
  82. static void irport_change_speed(void *priv, __u32 speed);
  83. static int irport_net_open(struct net_device *dev);
  84. static int irport_net_close(struct net_device *dev);
  85. static struct irport_cb *
  86. irport_open(int i, unsigned int iobase, unsigned int irq)
  87. {
  88. struct net_device *dev;
  89. struct irport_cb *self;
  90. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  91. /* Lock the port that we need */
  92. if (!request_region(iobase, IO_EXTENT, driver_name)) {
  93. IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
  94. __FUNCTION__, iobase);
  95. goto err_out1;
  96. }
  97. /*
  98. * Allocate new instance of the driver
  99. */
  100. dev = alloc_irdadev(sizeof(struct irport_cb));
  101. if (!dev) {
  102. IRDA_ERROR("%s(), can't allocate memory for "
  103. "irda device!\n", __FUNCTION__);
  104. goto err_out2;
  105. }
  106. self = dev->priv;
  107. spin_lock_init(&self->lock);
  108. /* Need to store self somewhere */
  109. dev_self[i] = self;
  110. self->priv = self;
  111. self->index = i;
  112. /* Initialize IO */
  113. self->io.sir_base = iobase;
  114. self->io.sir_ext = IO_EXTENT;
  115. self->io.irq = irq;
  116. self->io.fifo_size = 16; /* 16550A and compatible */
  117. /* Initialize QoS for this device */
  118. irda_init_max_qos_capabilies(&self->qos);
  119. self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  120. IR_115200;
  121. self->qos.min_turn_time.bits = qos_mtt_bits;
  122. irda_qos_bits_to_value(&self->qos);
  123. /* Bootstrap ZeroCopy Rx */
  124. self->rx_buff.truesize = IRDA_SKB_MAX_MTU;
  125. self->rx_buff.skb = __dev_alloc_skb(self->rx_buff.truesize,
  126. GFP_KERNEL);
  127. if (self->rx_buff.skb == NULL) {
  128. IRDA_ERROR("%s(), can't allocate memory for "
  129. "receive buffer!\n", __FUNCTION__);
  130. goto err_out3;
  131. }
  132. skb_reserve(self->rx_buff.skb, 1);
  133. self->rx_buff.head = self->rx_buff.skb->data;
  134. /* No need to memset the buffer, unless you are really pedantic */
  135. /* Finish setup the Rx buffer descriptor */
  136. self->rx_buff.in_frame = FALSE;
  137. self->rx_buff.state = OUTSIDE_FRAME;
  138. self->rx_buff.data = self->rx_buff.head;
  139. /* Specify how much memory we want */
  140. self->tx_buff.truesize = 4000;
  141. /* Allocate memory if needed */
  142. if (self->tx_buff.truesize > 0) {
  143. self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize,
  144. GFP_KERNEL);
  145. if (self->tx_buff.head == NULL) {
  146. IRDA_ERROR("%s(), can't allocate memory for "
  147. "transmit buffer!\n", __FUNCTION__);
  148. goto err_out4;
  149. }
  150. memset(self->tx_buff.head, 0, self->tx_buff.truesize);
  151. }
  152. self->tx_buff.data = self->tx_buff.head;
  153. self->netdev = dev;
  154. /* Keep track of module usage */
  155. SET_MODULE_OWNER(dev);
  156. /* May be overridden by piggyback drivers */
  157. self->interrupt = irport_interrupt;
  158. self->change_speed = irport_change_speed;
  159. /* Override the network functions we need to use */
  160. dev->hard_start_xmit = irport_hard_xmit;
  161. dev->tx_timeout = irport_timeout;
  162. dev->watchdog_timeo = HZ; /* Allow time enough for speed change */
  163. dev->open = irport_net_open;
  164. dev->stop = irport_net_close;
  165. dev->get_stats = irport_net_get_stats;
  166. dev->do_ioctl = irport_net_ioctl;
  167. /* Make ifconfig display some details */
  168. dev->base_addr = iobase;
  169. dev->irq = irq;
  170. if (register_netdev(dev)) {
  171. IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
  172. goto err_out5;
  173. }
  174. IRDA_MESSAGE("IrDA: Registered device %s (irport io=0x%X irq=%d)\n",
  175. dev->name, iobase, irq);
  176. return self;
  177. err_out5:
  178. kfree(self->tx_buff.head);
  179. err_out4:
  180. kfree_skb(self->rx_buff.skb);
  181. err_out3:
  182. free_netdev(dev);
  183. dev_self[i] = NULL;
  184. err_out2:
  185. release_region(iobase, IO_EXTENT);
  186. err_out1:
  187. return NULL;
  188. }
  189. static int irport_close(struct irport_cb *self)
  190. {
  191. IRDA_ASSERT(self != NULL, return -1;);
  192. /* We are not using any dongle anymore! */
  193. if (self->dongle)
  194. irda_device_dongle_cleanup(self->dongle);
  195. self->dongle = NULL;
  196. /* Remove netdevice */
  197. unregister_netdev(self->netdev);
  198. /* Release the IO-port that this driver is using */
  199. IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n",
  200. __FUNCTION__, self->io.sir_base);
  201. release_region(self->io.sir_base, self->io.sir_ext);
  202. kfree(self->tx_buff.head);
  203. if (self->rx_buff.skb)
  204. kfree_skb(self->rx_buff.skb);
  205. self->rx_buff.skb = NULL;
  206. /* Remove ourselves */
  207. dev_self[self->index] = NULL;
  208. free_netdev(self->netdev);
  209. return 0;
  210. }
  211. static void irport_stop(struct irport_cb *self)
  212. {
  213. int iobase;
  214. iobase = self->io.sir_base;
  215. /* We can't lock, we may be called from a FIR driver - Jean II */
  216. /* We are not transmitting any more */
  217. self->transmitting = 0;
  218. /* Reset UART */
  219. outb(0, iobase+UART_MCR);
  220. /* Turn off interrupts */
  221. outb(0, iobase+UART_IER);
  222. }
  223. static void irport_start(struct irport_cb *self)
  224. {
  225. int iobase;
  226. iobase = self->io.sir_base;
  227. irport_stop(self);
  228. /* We can't lock, we may be called from a FIR driver - Jean II */
  229. /* Initialize UART */
  230. outb(UART_LCR_WLEN8, iobase+UART_LCR); /* Reset DLAB */
  231. outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
  232. /* Turn on interrups */
  233. outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER);
  234. }
  235. /*
  236. * Function irport_get_fcr (speed)
  237. *
  238. * Compute value of fcr
  239. *
  240. */
  241. static inline unsigned int irport_get_fcr(__u32 speed)
  242. {
  243. unsigned int fcr; /* FIFO control reg */
  244. /* Enable fifos */
  245. fcr = UART_FCR_ENABLE_FIFO;
  246. /*
  247. * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
  248. * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
  249. * about this timeout since it will always be fast enough.
  250. */
  251. if (speed < 38400)
  252. fcr |= UART_FCR_TRIGGER_1;
  253. else
  254. //fcr |= UART_FCR_TRIGGER_14;
  255. fcr |= UART_FCR_TRIGGER_8;
  256. return(fcr);
  257. }
  258. /*
  259. * Function irport_change_speed (self, speed)
  260. *
  261. * Set speed of IrDA port to specified baudrate
  262. *
  263. * This function should be called with irq off and spin-lock.
  264. */
  265. static void irport_change_speed(void *priv, __u32 speed)
  266. {
  267. struct irport_cb *self = (struct irport_cb *) priv;
  268. int iobase;
  269. unsigned int fcr; /* FIFO control reg */
  270. unsigned int lcr; /* Line control reg */
  271. int divisor;
  272. IRDA_ASSERT(self != NULL, return;);
  273. IRDA_ASSERT(speed != 0, return;);
  274. IRDA_DEBUG(1, "%s(), Setting speed to: %d - iobase=%#x\n",
  275. __FUNCTION__, speed, self->io.sir_base);
  276. /* We can't lock, we may be called from a FIR driver - Jean II */
  277. iobase = self->io.sir_base;
  278. /* Update accounting for new speed */
  279. self->io.speed = speed;
  280. /* Turn off interrupts */
  281. outb(0, iobase+UART_IER);
  282. divisor = SPEED_MAX/speed;
  283. /* Get proper fifo configuration */
  284. fcr = irport_get_fcr(speed);
  285. /* IrDA ports use 8N1 */
  286. lcr = UART_LCR_WLEN8;
  287. outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
  288. outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
  289. outb(divisor >> 8, iobase+UART_DLM);
  290. outb(lcr, iobase+UART_LCR); /* Set 8N1 */
  291. outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
  292. /* Turn on interrups */
  293. /* This will generate a fatal interrupt storm.
  294. * People calling us will do that properly - Jean II */
  295. //outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
  296. }
  297. /*
  298. * Function __irport_change_speed (instance, state, param)
  299. *
  300. * State machine for changing speed of the device. We do it this way since
  301. * we cannot use schedule_timeout() when we are in interrupt context
  302. *
  303. */
  304. static int __irport_change_speed(struct irda_task *task)
  305. {
  306. struct irport_cb *self;
  307. __u32 speed = (__u32) task->param;
  308. unsigned long flags = 0;
  309. int wasunlocked = 0;
  310. int ret = 0;
  311. IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies);
  312. self = (struct irport_cb *) task->instance;
  313. IRDA_ASSERT(self != NULL, return -1;);
  314. /* Locking notes : this function may be called from irq context with
  315. * spinlock, via irport_write_wakeup(), or from non-interrupt without
  316. * spinlock (from the task timer). Yuck !
  317. * This is ugly, and unsafe is the spinlock is not already aquired.
  318. * This will be fixed when irda-task get rewritten.
  319. * Jean II */
  320. if (!spin_is_locked(&self->lock)) {
  321. spin_lock_irqsave(&self->lock, flags);
  322. wasunlocked = 1;
  323. }
  324. switch (task->state) {
  325. case IRDA_TASK_INIT:
  326. case IRDA_TASK_WAIT:
  327. /* Are we ready to change speed yet? */
  328. if (self->tx_buff.len > 0) {
  329. task->state = IRDA_TASK_WAIT;
  330. /* Try again later */
  331. ret = msecs_to_jiffies(20);
  332. break;
  333. }
  334. if (self->dongle)
  335. irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
  336. else
  337. irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
  338. break;
  339. case IRDA_TASK_CHILD_INIT:
  340. /* Go to default speed */
  341. self->change_speed(self->priv, 9600);
  342. /* Change speed of dongle */
  343. if (irda_task_execute(self->dongle,
  344. self->dongle->issue->change_speed,
  345. NULL, task, (void *) speed))
  346. {
  347. /* Dongle need more time to change its speed */
  348. irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
  349. /* Give dongle 1 sec to finish */
  350. ret = msecs_to_jiffies(1000);
  351. } else
  352. /* Child finished immediately */
  353. irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
  354. break;
  355. case IRDA_TASK_CHILD_WAIT:
  356. IRDA_WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
  357. ret = -1;
  358. break;
  359. case IRDA_TASK_CHILD_DONE:
  360. /* Finally we are ready to change the speed */
  361. self->change_speed(self->priv, speed);
  362. irda_task_next_state(task, IRDA_TASK_DONE);
  363. break;
  364. default:
  365. IRDA_ERROR("%s(), unknown state %d\n",
  366. __FUNCTION__, task->state);
  367. irda_task_next_state(task, IRDA_TASK_DONE);
  368. ret = -1;
  369. break;
  370. }
  371. /* Put stuff in the state we found them - Jean II */
  372. if(wasunlocked) {
  373. spin_unlock_irqrestore(&self->lock, flags);
  374. }
  375. return ret;
  376. }
  377. /*
  378. * Function irport_change_speed_complete (task)
  379. *
  380. * Called when the change speed operation completes
  381. *
  382. */
  383. static int irport_change_speed_complete(struct irda_task *task)
  384. {
  385. struct irport_cb *self;
  386. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  387. self = (struct irport_cb *) task->instance;
  388. IRDA_ASSERT(self != NULL, return -1;);
  389. IRDA_ASSERT(self->netdev != NULL, return -1;);
  390. /* Finished changing speed, so we are not busy any longer */
  391. /* Signal network layer so it can try to send the frame */
  392. netif_wake_queue(self->netdev);
  393. return 0;
  394. }
  395. /*
  396. * Function irport_timeout (struct net_device *dev)
  397. *
  398. * The networking layer thinks we timed out.
  399. *
  400. */
  401. static void irport_timeout(struct net_device *dev)
  402. {
  403. struct irport_cb *self;
  404. int iobase;
  405. int iir, lsr;
  406. unsigned long flags;
  407. self = (struct irport_cb *) dev->priv;
  408. IRDA_ASSERT(self != NULL, return;);
  409. iobase = self->io.sir_base;
  410. IRDA_WARNING("%s: transmit timed out, jiffies = %ld, trans_start = %ld\n",
  411. dev->name, jiffies, dev->trans_start);
  412. spin_lock_irqsave(&self->lock, flags);
  413. /* Debug what's happening... */
  414. /* Get interrupt status */
  415. lsr = inb(iobase+UART_LSR);
  416. /* Read interrupt register */
  417. iir = inb(iobase+UART_IIR);
  418. IRDA_DEBUG(0, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
  419. __FUNCTION__, iir, lsr, iobase);
  420. IRDA_DEBUG(0, "%s(), transmitting=%d, remain=%d, done=%d\n",
  421. __FUNCTION__, self->transmitting, self->tx_buff.len,
  422. self->tx_buff.data - self->tx_buff.head);
  423. /* Now, restart the port */
  424. irport_start(self);
  425. self->change_speed(self->priv, self->io.speed);
  426. /* This will re-enable irqs */
  427. outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
  428. dev->trans_start = jiffies;
  429. spin_unlock_irqrestore(&self->lock, flags);
  430. netif_wake_queue(dev);
  431. }
  432. /*
  433. * Function irport_wait_hw_transmitter_finish ()
  434. *
  435. * Wait for the real end of HW transmission
  436. *
  437. * The UART is a strict FIFO, and we get called only when we have finished
  438. * pushing data to the FIFO, so the maximum amount of time we must wait
  439. * is only for the FIFO to drain out.
  440. *
  441. * We use a simple calibrated loop. We may need to adjust the loop
  442. * delay (udelay) to balance I/O traffic and latency. And we also need to
  443. * adjust the maximum timeout.
  444. * It would probably be better to wait for the proper interrupt,
  445. * but it doesn't seem to be available.
  446. *
  447. * We can't use jiffies or kernel timers because :
  448. * 1) We are called from the interrupt handler, which disable softirqs,
  449. * so jiffies won't be increased
  450. * 2) Jiffies granularity is usually very coarse (10ms), and we don't
  451. * want to wait that long to detect stuck hardware.
  452. * Jean II
  453. */
  454. static void irport_wait_hw_transmitter_finish(struct irport_cb *self)
  455. {
  456. int iobase;
  457. int count = 1000; /* 1 ms */
  458. iobase = self->io.sir_base;
  459. /* Calibrated busy loop */
  460. while((count-- > 0) && !(inb(iobase+UART_LSR) & UART_LSR_TEMT))
  461. udelay(1);
  462. if(count == 0)
  463. IRDA_DEBUG(0, "%s(): stuck transmitter\n", __FUNCTION__);
  464. }
  465. /*
  466. * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
  467. *
  468. * Transmits the current frame until FIFO is full, then
  469. * waits until the next transmitt interrupt, and continues until the
  470. * frame is transmitted.
  471. */
  472. static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  473. {
  474. struct irport_cb *self;
  475. unsigned long flags;
  476. int iobase;
  477. s32 speed;
  478. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  479. IRDA_ASSERT(dev != NULL, return 0;);
  480. self = (struct irport_cb *) dev->priv;
  481. IRDA_ASSERT(self != NULL, return 0;);
  482. iobase = self->io.sir_base;
  483. netif_stop_queue(dev);
  484. /* Make sure tests & speed change are atomic */
  485. spin_lock_irqsave(&self->lock, flags);
  486. /* Check if we need to change the speed */
  487. speed = irda_get_next_speed(skb);
  488. if ((speed != self->io.speed) && (speed != -1)) {
  489. /* Check for empty frame */
  490. if (!skb->len) {
  491. /*
  492. * We send frames one by one in SIR mode (no
  493. * pipelining), so at this point, if we were sending
  494. * a previous frame, we just received the interrupt
  495. * telling us it is finished (UART_IIR_THRI).
  496. * Therefore, waiting for the transmitter to really
  497. * finish draining the fifo won't take too long.
  498. * And the interrupt handler is not expected to run.
  499. * - Jean II */
  500. irport_wait_hw_transmitter_finish(self);
  501. /* Better go there already locked - Jean II */
  502. irda_task_execute(self, __irport_change_speed,
  503. irport_change_speed_complete,
  504. NULL, (void *) speed);
  505. dev->trans_start = jiffies;
  506. spin_unlock_irqrestore(&self->lock, flags);
  507. dev_kfree_skb(skb);
  508. return 0;
  509. } else
  510. self->new_speed = speed;
  511. }
  512. /* Init tx buffer */
  513. self->tx_buff.data = self->tx_buff.head;
  514. /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
  515. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  516. self->tx_buff.truesize);
  517. self->stats.tx_bytes += self->tx_buff.len;
  518. /* We are transmitting */
  519. self->transmitting = 1;
  520. /* Turn on transmit finished interrupt. Will fire immediately! */
  521. outb(UART_IER_THRI, iobase+UART_IER);
  522. dev->trans_start = jiffies;
  523. spin_unlock_irqrestore(&self->lock, flags);
  524. dev_kfree_skb(skb);
  525. return 0;
  526. }
  527. /*
  528. * Function irport_write (driver)
  529. *
  530. * Fill Tx FIFO with transmit data
  531. *
  532. * Called only from irport_write_wakeup()
  533. */
  534. static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
  535. {
  536. int actual = 0;
  537. /* Fill FIFO with current frame */
  538. while ((actual < fifo_size) && (actual < len)) {
  539. /* Transmit next byte */
  540. outb(buf[actual], iobase+UART_TX);
  541. actual++;
  542. }
  543. return actual;
  544. }
  545. /*
  546. * Function irport_write_wakeup (tty)
  547. *
  548. * Called by the driver when there's room for more data. If we have
  549. * more packets to send, we send them here.
  550. *
  551. * Called only from irport_interrupt()
  552. * Make sure this function is *not* called while we are receiving,
  553. * otherwise we will reset fifo and loose data :-(
  554. */
  555. static inline void irport_write_wakeup(struct irport_cb *self)
  556. {
  557. int actual = 0;
  558. int iobase;
  559. unsigned int fcr;
  560. IRDA_ASSERT(self != NULL, return;);
  561. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  562. iobase = self->io.sir_base;
  563. /* Finished with frame? */
  564. if (self->tx_buff.len > 0) {
  565. /* Write data left in transmit buffer */
  566. actual = irport_write(iobase, self->io.fifo_size,
  567. self->tx_buff.data, self->tx_buff.len);
  568. self->tx_buff.data += actual;
  569. self->tx_buff.len -= actual;
  570. } else {
  571. /*
  572. * Now serial buffer is almost free & we can start
  573. * transmission of another packet. But first we must check
  574. * if we need to change the speed of the hardware
  575. */
  576. if (self->new_speed) {
  577. irport_wait_hw_transmitter_finish(self);
  578. irda_task_execute(self, __irport_change_speed,
  579. irport_change_speed_complete,
  580. NULL, (void *) self->new_speed);
  581. self->new_speed = 0;
  582. } else {
  583. /* Tell network layer that we want more frames */
  584. netif_wake_queue(self->netdev);
  585. }
  586. self->stats.tx_packets++;
  587. /*
  588. * Reset Rx FIFO to make sure that all reflected transmit data
  589. * is discarded. This is needed for half duplex operation
  590. */
  591. fcr = irport_get_fcr(self->io.speed);
  592. fcr |= UART_FCR_CLEAR_RCVR;
  593. outb(fcr, iobase+UART_FCR);
  594. /* Finished transmitting */
  595. self->transmitting = 0;
  596. /* Turn on receive interrupts */
  597. outb(UART_IER_RDI, iobase+UART_IER);
  598. IRDA_DEBUG(1, "%s() : finished Tx\n", __FUNCTION__);
  599. }
  600. }
  601. /*
  602. * Function irport_receive (self)
  603. *
  604. * Receive one frame from the infrared port
  605. *
  606. * Called only from irport_interrupt()
  607. */
  608. static inline void irport_receive(struct irport_cb *self)
  609. {
  610. int boguscount = 0;
  611. int iobase;
  612. IRDA_ASSERT(self != NULL, return;);
  613. iobase = self->io.sir_base;
  614. /*
  615. * Receive all characters in Rx FIFO, unwrap and unstuff them.
  616. * async_unwrap_char will deliver all found frames
  617. */
  618. do {
  619. async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
  620. inb(iobase+UART_RX));
  621. /* Make sure we don't stay here too long */
  622. if (boguscount++ > 32) {
  623. IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
  624. break;
  625. }
  626. } while (inb(iobase+UART_LSR) & UART_LSR_DR);
  627. }
  628. /*
  629. * Function irport_interrupt (irq, dev_id, regs)
  630. *
  631. * Interrupt handler
  632. */
  633. static irqreturn_t irport_interrupt(int irq, void *dev_id,
  634. struct pt_regs *regs)
  635. {
  636. struct net_device *dev = (struct net_device *) dev_id;
  637. struct irport_cb *self;
  638. int boguscount = 0;
  639. int iobase;
  640. int iir, lsr;
  641. int handled = 0;
  642. if (!dev) {
  643. IRDA_WARNING("%s() irq %d for unknown device.\n", __FUNCTION__, irq);
  644. return IRQ_NONE;
  645. }
  646. self = (struct irport_cb *) dev->priv;
  647. spin_lock(&self->lock);
  648. iobase = self->io.sir_base;
  649. /* Cut'n'paste interrupt routine from serial.c
  650. * This version try to minimise latency and I/O operations.
  651. * Simplified and modified to enforce half duplex operation.
  652. * - Jean II */
  653. /* Check status even is iir reg is cleared, more robust and
  654. * eliminate a read on the I/O bus - Jean II */
  655. do {
  656. /* Get interrupt status ; Clear interrupt */
  657. lsr = inb(iobase+UART_LSR);
  658. /* Are we receiving or transmitting ? */
  659. if(!self->transmitting) {
  660. /* Received something ? */
  661. if (lsr & UART_LSR_DR)
  662. irport_receive(self);
  663. } else {
  664. /* Room in Tx fifo ? */
  665. if (lsr & (UART_LSR_THRE | UART_LSR_TEMT))
  666. irport_write_wakeup(self);
  667. }
  668. /* A bit hackish, but working as expected... Jean II */
  669. if(lsr & (UART_LSR_THRE | UART_LSR_TEMT | UART_LSR_DR))
  670. handled = 1;
  671. /* Make sure we don't stay here to long */
  672. if (boguscount++ > 10) {
  673. IRDA_WARNING("%s() irq handler looping : lsr=%02x\n",
  674. __FUNCTION__, lsr);
  675. break;
  676. }
  677. /* Read interrupt register */
  678. iir = inb(iobase+UART_IIR);
  679. /* Enable this debug only when no other options and at low
  680. * bit rates, otherwise it may cause Rx overruns (lsr=63).
  681. * - Jean II */
  682. IRDA_DEBUG(6, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
  683. __FUNCTION__, iir, lsr, iobase);
  684. /* As long as interrupt pending... */
  685. } while ((iir & UART_IIR_NO_INT) == 0);
  686. spin_unlock(&self->lock);
  687. return IRQ_RETVAL(handled);
  688. }
  689. /*
  690. * Function irport_net_open (dev)
  691. *
  692. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  693. *
  694. */
  695. static int irport_net_open(struct net_device *dev)
  696. {
  697. struct irport_cb *self;
  698. int iobase;
  699. char hwname[16];
  700. unsigned long flags;
  701. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  702. IRDA_ASSERT(dev != NULL, return -1;);
  703. self = (struct irport_cb *) dev->priv;
  704. iobase = self->io.sir_base;
  705. if (request_irq(self->io.irq, self->interrupt, 0, dev->name,
  706. (void *) dev)) {
  707. IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
  708. __FUNCTION__, self->io.irq);
  709. return -EAGAIN;
  710. }
  711. spin_lock_irqsave(&self->lock, flags);
  712. /* Init uart */
  713. irport_start(self);
  714. /* Set 9600 bauds per default, including at the dongle */
  715. irda_task_execute(self, __irport_change_speed,
  716. irport_change_speed_complete,
  717. NULL, (void *) 9600);
  718. spin_unlock_irqrestore(&self->lock, flags);
  719. /* Give self a hardware name */
  720. sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base);
  721. /*
  722. * Open new IrLAP layer instance, now that everything should be
  723. * initialized properly
  724. */
  725. self->irlap = irlap_open(dev, &self->qos, hwname);
  726. /* Ready to play! */
  727. netif_start_queue(dev);
  728. return 0;
  729. }
  730. /*
  731. * Function irport_net_close (self)
  732. *
  733. * Network device is taken down. Usually this is done by
  734. * "ifconfig irda0 down"
  735. */
  736. static int irport_net_close(struct net_device *dev)
  737. {
  738. struct irport_cb *self;
  739. int iobase;
  740. unsigned long flags;
  741. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  742. IRDA_ASSERT(dev != NULL, return -1;);
  743. self = (struct irport_cb *) dev->priv;
  744. IRDA_ASSERT(self != NULL, return -1;);
  745. iobase = self->io.sir_base;
  746. /* Stop device */
  747. netif_stop_queue(dev);
  748. /* Stop and remove instance of IrLAP */
  749. if (self->irlap)
  750. irlap_close(self->irlap);
  751. self->irlap = NULL;
  752. spin_lock_irqsave(&self->lock, flags);
  753. irport_stop(self);
  754. spin_unlock_irqrestore(&self->lock, flags);
  755. free_irq(self->io.irq, dev);
  756. return 0;
  757. }
  758. /*
  759. * Function irport_is_receiving (self)
  760. *
  761. * Returns true is we are currently receiving data
  762. *
  763. */
  764. static inline int irport_is_receiving(struct irport_cb *self)
  765. {
  766. return (self->rx_buff.state != OUTSIDE_FRAME);
  767. }
  768. /*
  769. * Function irport_set_dtr_rts (tty, dtr, rts)
  770. *
  771. * This function can be used by dongles etc. to set or reset the status
  772. * of the dtr and rts lines
  773. */
  774. static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts)
  775. {
  776. struct irport_cb *self = dev->priv;
  777. int iobase;
  778. IRDA_ASSERT(self != NULL, return -1;);
  779. iobase = self->io.sir_base;
  780. if (dtr)
  781. dtr = UART_MCR_DTR;
  782. if (rts)
  783. rts = UART_MCR_RTS;
  784. outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
  785. return 0;
  786. }
  787. static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
  788. {
  789. struct irport_cb *self = (struct irport_cb *) dev->priv;
  790. int actual = 0;
  791. int iobase;
  792. IRDA_ASSERT(self != NULL, return -1;);
  793. iobase = self->io.sir_base;
  794. /* Tx FIFO should be empty! */
  795. if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
  796. IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n", __FUNCTION__);
  797. return -1;
  798. }
  799. /* Fill FIFO with current frame */
  800. while (actual < len) {
  801. /* Transmit next byte */
  802. outb(buf[actual], iobase+UART_TX);
  803. actual++;
  804. }
  805. return actual;
  806. }
  807. /*
  808. * Function irport_net_ioctl (dev, rq, cmd)
  809. *
  810. * Process IOCTL commands for this device
  811. *
  812. */
  813. static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  814. {
  815. struct if_irda_req *irq = (struct if_irda_req *) rq;
  816. struct irport_cb *self;
  817. dongle_t *dongle;
  818. unsigned long flags;
  819. int ret = 0;
  820. IRDA_ASSERT(dev != NULL, return -1;);
  821. self = dev->priv;
  822. IRDA_ASSERT(self != NULL, return -1;);
  823. IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
  824. switch (cmd) {
  825. case SIOCSBANDWIDTH: /* Set bandwidth */
  826. if (!capable(CAP_NET_ADMIN))
  827. ret = -EPERM;
  828. else
  829. irda_task_execute(self, __irport_change_speed, NULL,
  830. NULL, (void *) irq->ifr_baudrate);
  831. break;
  832. case SIOCSDONGLE: /* Set dongle */
  833. if (!capable(CAP_NET_ADMIN)) {
  834. ret = -EPERM;
  835. break;
  836. }
  837. /* Locking :
  838. * irda_device_dongle_init() can't be locked.
  839. * irda_task_execute() doesn't need to be locked.
  840. * Jean II
  841. */
  842. /* Initialize dongle */
  843. dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
  844. if (!dongle)
  845. break;
  846. dongle->set_mode = NULL;
  847. dongle->read = NULL;
  848. dongle->write = irport_raw_write;
  849. dongle->set_dtr_rts = irport_set_dtr_rts;
  850. /* Now initialize the dongle! */
  851. dongle->issue->open(dongle, &self->qos);
  852. /* Reset dongle */
  853. irda_task_execute(dongle, dongle->issue->reset, NULL, NULL,
  854. NULL);
  855. /* Make dongle available to driver only now to avoid
  856. * race conditions - Jean II */
  857. self->dongle = dongle;
  858. break;
  859. case SIOCSMEDIABUSY: /* Set media busy */
  860. if (!capable(CAP_NET_ADMIN)) {
  861. ret = -EPERM;
  862. break;
  863. }
  864. irda_device_set_media_busy(self->netdev, TRUE);
  865. break;
  866. case SIOCGRECEIVING: /* Check if we are receiving right now */
  867. irq->ifr_receiving = irport_is_receiving(self);
  868. break;
  869. case SIOCSDTRRTS:
  870. if (!capable(CAP_NET_ADMIN)) {
  871. ret = -EPERM;
  872. break;
  873. }
  874. /* No real need to lock... */
  875. spin_lock_irqsave(&self->lock, flags);
  876. irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
  877. spin_unlock_irqrestore(&self->lock, flags);
  878. break;
  879. default:
  880. ret = -EOPNOTSUPP;
  881. }
  882. return ret;
  883. }
  884. static struct net_device_stats *irport_net_get_stats(struct net_device *dev)
  885. {
  886. struct irport_cb *self = (struct irport_cb *) dev->priv;
  887. return &self->stats;
  888. }
  889. static int __init irport_init(void)
  890. {
  891. int i;
  892. for (i=0; (io[i] < 2000) && (i < 4); i++) {
  893. if (irport_open(i, io[i], irq[i]) != NULL)
  894. return 0;
  895. }
  896. /*
  897. * Maybe something failed, but we can still be usable for FIR drivers
  898. */
  899. return 0;
  900. }
  901. /*
  902. * Function irport_cleanup ()
  903. *
  904. * Close all configured ports
  905. *
  906. */
  907. static void __exit irport_cleanup(void)
  908. {
  909. int i;
  910. IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
  911. for (i=0; i < 4; i++) {
  912. if (dev_self[i])
  913. irport_close(dev_self[i]);
  914. }
  915. }
  916. module_param_array(io, int, NULL, 0);
  917. MODULE_PARM_DESC(io, "Base I/O addresses");
  918. module_param_array(irq, int, NULL, 0);
  919. MODULE_PARM_DESC(irq, "IRQ lines");
  920. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  921. MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
  922. MODULE_LICENSE("GPL");
  923. module_init(irport_init);
  924. module_exit(irport_cleanup);