irport.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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_probe (void)
  237. *
  238. * Start IO port
  239. *
  240. */
  241. int irport_probe(int iobase)
  242. {
  243. IRDA_DEBUG(4, "%s(), iobase=%#x\n", __FUNCTION__, iobase);
  244. return 0;
  245. }
  246. /*
  247. * Function irport_get_fcr (speed)
  248. *
  249. * Compute value of fcr
  250. *
  251. */
  252. static inline unsigned int irport_get_fcr(__u32 speed)
  253. {
  254. unsigned int fcr; /* FIFO control reg */
  255. /* Enable fifos */
  256. fcr = UART_FCR_ENABLE_FIFO;
  257. /*
  258. * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
  259. * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
  260. * about this timeout since it will always be fast enough.
  261. */
  262. if (speed < 38400)
  263. fcr |= UART_FCR_TRIGGER_1;
  264. else
  265. //fcr |= UART_FCR_TRIGGER_14;
  266. fcr |= UART_FCR_TRIGGER_8;
  267. return(fcr);
  268. }
  269. /*
  270. * Function irport_change_speed (self, speed)
  271. *
  272. * Set speed of IrDA port to specified baudrate
  273. *
  274. * This function should be called with irq off and spin-lock.
  275. */
  276. static void irport_change_speed(void *priv, __u32 speed)
  277. {
  278. struct irport_cb *self = (struct irport_cb *) priv;
  279. int iobase;
  280. unsigned int fcr; /* FIFO control reg */
  281. unsigned int lcr; /* Line control reg */
  282. int divisor;
  283. IRDA_ASSERT(self != NULL, return;);
  284. IRDA_ASSERT(speed != 0, return;);
  285. IRDA_DEBUG(1, "%s(), Setting speed to: %d - iobase=%#x\n",
  286. __FUNCTION__, speed, self->io.sir_base);
  287. /* We can't lock, we may be called from a FIR driver - Jean II */
  288. iobase = self->io.sir_base;
  289. /* Update accounting for new speed */
  290. self->io.speed = speed;
  291. /* Turn off interrupts */
  292. outb(0, iobase+UART_IER);
  293. divisor = SPEED_MAX/speed;
  294. /* Get proper fifo configuration */
  295. fcr = irport_get_fcr(speed);
  296. /* IrDA ports use 8N1 */
  297. lcr = UART_LCR_WLEN8;
  298. outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
  299. outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
  300. outb(divisor >> 8, iobase+UART_DLM);
  301. outb(lcr, iobase+UART_LCR); /* Set 8N1 */
  302. outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
  303. /* Turn on interrups */
  304. /* This will generate a fatal interrupt storm.
  305. * People calling us will do that properly - Jean II */
  306. //outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
  307. }
  308. /*
  309. * Function __irport_change_speed (instance, state, param)
  310. *
  311. * State machine for changing speed of the device. We do it this way since
  312. * we cannot use schedule_timeout() when we are in interrupt context
  313. *
  314. */
  315. int __irport_change_speed(struct irda_task *task)
  316. {
  317. struct irport_cb *self;
  318. __u32 speed = (__u32) task->param;
  319. unsigned long flags = 0;
  320. int wasunlocked = 0;
  321. int ret = 0;
  322. IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies);
  323. self = (struct irport_cb *) task->instance;
  324. IRDA_ASSERT(self != NULL, return -1;);
  325. /* Locking notes : this function may be called from irq context with
  326. * spinlock, via irport_write_wakeup(), or from non-interrupt without
  327. * spinlock (from the task timer). Yuck !
  328. * This is ugly, and unsafe is the spinlock is not already aquired.
  329. * This will be fixed when irda-task get rewritten.
  330. * Jean II */
  331. if (!spin_is_locked(&self->lock)) {
  332. spin_lock_irqsave(&self->lock, flags);
  333. wasunlocked = 1;
  334. }
  335. switch (task->state) {
  336. case IRDA_TASK_INIT:
  337. case IRDA_TASK_WAIT:
  338. /* Are we ready to change speed yet? */
  339. if (self->tx_buff.len > 0) {
  340. task->state = IRDA_TASK_WAIT;
  341. /* Try again later */
  342. ret = msecs_to_jiffies(20);
  343. break;
  344. }
  345. if (self->dongle)
  346. irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
  347. else
  348. irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
  349. break;
  350. case IRDA_TASK_CHILD_INIT:
  351. /* Go to default speed */
  352. self->change_speed(self->priv, 9600);
  353. /* Change speed of dongle */
  354. if (irda_task_execute(self->dongle,
  355. self->dongle->issue->change_speed,
  356. NULL, task, (void *) speed))
  357. {
  358. /* Dongle need more time to change its speed */
  359. irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
  360. /* Give dongle 1 sec to finish */
  361. ret = msecs_to_jiffies(1000);
  362. } else
  363. /* Child finished immediately */
  364. irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
  365. break;
  366. case IRDA_TASK_CHILD_WAIT:
  367. IRDA_WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
  368. ret = -1;
  369. break;
  370. case IRDA_TASK_CHILD_DONE:
  371. /* Finally we are ready to change the speed */
  372. self->change_speed(self->priv, speed);
  373. irda_task_next_state(task, IRDA_TASK_DONE);
  374. break;
  375. default:
  376. IRDA_ERROR("%s(), unknown state %d\n",
  377. __FUNCTION__, task->state);
  378. irda_task_next_state(task, IRDA_TASK_DONE);
  379. ret = -1;
  380. break;
  381. }
  382. /* Put stuff in the state we found them - Jean II */
  383. if(wasunlocked) {
  384. spin_unlock_irqrestore(&self->lock, flags);
  385. }
  386. return ret;
  387. }
  388. /*
  389. * Function irport_change_speed_complete (task)
  390. *
  391. * Called when the change speed operation completes
  392. *
  393. */
  394. static int irport_change_speed_complete(struct irda_task *task)
  395. {
  396. struct irport_cb *self;
  397. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  398. self = (struct irport_cb *) task->instance;
  399. IRDA_ASSERT(self != NULL, return -1;);
  400. IRDA_ASSERT(self->netdev != NULL, return -1;);
  401. /* Finished changing speed, so we are not busy any longer */
  402. /* Signal network layer so it can try to send the frame */
  403. netif_wake_queue(self->netdev);
  404. return 0;
  405. }
  406. /*
  407. * Function irport_timeout (struct net_device *dev)
  408. *
  409. * The networking layer thinks we timed out.
  410. *
  411. */
  412. static void irport_timeout(struct net_device *dev)
  413. {
  414. struct irport_cb *self;
  415. int iobase;
  416. int iir, lsr;
  417. unsigned long flags;
  418. self = (struct irport_cb *) dev->priv;
  419. IRDA_ASSERT(self != NULL, return;);
  420. iobase = self->io.sir_base;
  421. IRDA_WARNING("%s: transmit timed out, jiffies = %ld, trans_start = %ld\n",
  422. dev->name, jiffies, dev->trans_start);
  423. spin_lock_irqsave(&self->lock, flags);
  424. /* Debug what's happening... */
  425. /* Get interrupt status */
  426. lsr = inb(iobase+UART_LSR);
  427. /* Read interrupt register */
  428. iir = inb(iobase+UART_IIR);
  429. IRDA_DEBUG(0, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
  430. __FUNCTION__, iir, lsr, iobase);
  431. IRDA_DEBUG(0, "%s(), transmitting=%d, remain=%d, done=%d\n",
  432. __FUNCTION__, self->transmitting, self->tx_buff.len,
  433. self->tx_buff.data - self->tx_buff.head);
  434. /* Now, restart the port */
  435. irport_start(self);
  436. self->change_speed(self->priv, self->io.speed);
  437. /* This will re-enable irqs */
  438. outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
  439. dev->trans_start = jiffies;
  440. spin_unlock_irqrestore(&self->lock, flags);
  441. netif_wake_queue(dev);
  442. }
  443. /*
  444. * Function irport_wait_hw_transmitter_finish ()
  445. *
  446. * Wait for the real end of HW transmission
  447. *
  448. * The UART is a strict FIFO, and we get called only when we have finished
  449. * pushing data to the FIFO, so the maximum amount of time we must wait
  450. * is only for the FIFO to drain out.
  451. *
  452. * We use a simple calibrated loop. We may need to adjust the loop
  453. * delay (udelay) to balance I/O traffic and latency. And we also need to
  454. * adjust the maximum timeout.
  455. * It would probably be better to wait for the proper interrupt,
  456. * but it doesn't seem to be available.
  457. *
  458. * We can't use jiffies or kernel timers because :
  459. * 1) We are called from the interrupt handler, which disable softirqs,
  460. * so jiffies won't be increased
  461. * 2) Jiffies granularity is usually very coarse (10ms), and we don't
  462. * want to wait that long to detect stuck hardware.
  463. * Jean II
  464. */
  465. static void irport_wait_hw_transmitter_finish(struct irport_cb *self)
  466. {
  467. int iobase;
  468. int count = 1000; /* 1 ms */
  469. iobase = self->io.sir_base;
  470. /* Calibrated busy loop */
  471. while((count-- > 0) && !(inb(iobase+UART_LSR) & UART_LSR_TEMT))
  472. udelay(1);
  473. if(count == 0)
  474. IRDA_DEBUG(0, "%s(): stuck transmitter\n", __FUNCTION__);
  475. }
  476. /*
  477. * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
  478. *
  479. * Transmits the current frame until FIFO is full, then
  480. * waits until the next transmitt interrupt, and continues until the
  481. * frame is transmitted.
  482. */
  483. static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  484. {
  485. struct irport_cb *self;
  486. unsigned long flags;
  487. int iobase;
  488. s32 speed;
  489. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  490. IRDA_ASSERT(dev != NULL, return 0;);
  491. self = (struct irport_cb *) dev->priv;
  492. IRDA_ASSERT(self != NULL, return 0;);
  493. iobase = self->io.sir_base;
  494. netif_stop_queue(dev);
  495. /* Make sure tests & speed change are atomic */
  496. spin_lock_irqsave(&self->lock, flags);
  497. /* Check if we need to change the speed */
  498. speed = irda_get_next_speed(skb);
  499. if ((speed != self->io.speed) && (speed != -1)) {
  500. /* Check for empty frame */
  501. if (!skb->len) {
  502. /*
  503. * We send frames one by one in SIR mode (no
  504. * pipelining), so at this point, if we were sending
  505. * a previous frame, we just received the interrupt
  506. * telling us it is finished (UART_IIR_THRI).
  507. * Therefore, waiting for the transmitter to really
  508. * finish draining the fifo won't take too long.
  509. * And the interrupt handler is not expected to run.
  510. * - Jean II */
  511. irport_wait_hw_transmitter_finish(self);
  512. /* Better go there already locked - Jean II */
  513. irda_task_execute(self, __irport_change_speed,
  514. irport_change_speed_complete,
  515. NULL, (void *) speed);
  516. dev->trans_start = jiffies;
  517. spin_unlock_irqrestore(&self->lock, flags);
  518. dev_kfree_skb(skb);
  519. return 0;
  520. } else
  521. self->new_speed = speed;
  522. }
  523. /* Init tx buffer */
  524. self->tx_buff.data = self->tx_buff.head;
  525. /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
  526. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  527. self->tx_buff.truesize);
  528. self->stats.tx_bytes += self->tx_buff.len;
  529. /* We are transmitting */
  530. self->transmitting = 1;
  531. /* Turn on transmit finished interrupt. Will fire immediately! */
  532. outb(UART_IER_THRI, iobase+UART_IER);
  533. dev->trans_start = jiffies;
  534. spin_unlock_irqrestore(&self->lock, flags);
  535. dev_kfree_skb(skb);
  536. return 0;
  537. }
  538. /*
  539. * Function irport_write (driver)
  540. *
  541. * Fill Tx FIFO with transmit data
  542. *
  543. * Called only from irport_write_wakeup()
  544. */
  545. static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
  546. {
  547. int actual = 0;
  548. /* Fill FIFO with current frame */
  549. while ((actual < fifo_size) && (actual < len)) {
  550. /* Transmit next byte */
  551. outb(buf[actual], iobase+UART_TX);
  552. actual++;
  553. }
  554. return actual;
  555. }
  556. /*
  557. * Function irport_write_wakeup (tty)
  558. *
  559. * Called by the driver when there's room for more data. If we have
  560. * more packets to send, we send them here.
  561. *
  562. * Called only from irport_interrupt()
  563. * Make sure this function is *not* called while we are receiving,
  564. * otherwise we will reset fifo and loose data :-(
  565. */
  566. static inline void irport_write_wakeup(struct irport_cb *self)
  567. {
  568. int actual = 0;
  569. int iobase;
  570. unsigned int fcr;
  571. IRDA_ASSERT(self != NULL, return;);
  572. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  573. iobase = self->io.sir_base;
  574. /* Finished with frame? */
  575. if (self->tx_buff.len > 0) {
  576. /* Write data left in transmit buffer */
  577. actual = irport_write(iobase, self->io.fifo_size,
  578. self->tx_buff.data, self->tx_buff.len);
  579. self->tx_buff.data += actual;
  580. self->tx_buff.len -= actual;
  581. } else {
  582. /*
  583. * Now serial buffer is almost free & we can start
  584. * transmission of another packet. But first we must check
  585. * if we need to change the speed of the hardware
  586. */
  587. if (self->new_speed) {
  588. irport_wait_hw_transmitter_finish(self);
  589. irda_task_execute(self, __irport_change_speed,
  590. irport_change_speed_complete,
  591. NULL, (void *) self->new_speed);
  592. self->new_speed = 0;
  593. } else {
  594. /* Tell network layer that we want more frames */
  595. netif_wake_queue(self->netdev);
  596. }
  597. self->stats.tx_packets++;
  598. /*
  599. * Reset Rx FIFO to make sure that all reflected transmit data
  600. * is discarded. This is needed for half duplex operation
  601. */
  602. fcr = irport_get_fcr(self->io.speed);
  603. fcr |= UART_FCR_CLEAR_RCVR;
  604. outb(fcr, iobase+UART_FCR);
  605. /* Finished transmitting */
  606. self->transmitting = 0;
  607. /* Turn on receive interrupts */
  608. outb(UART_IER_RDI, iobase+UART_IER);
  609. IRDA_DEBUG(1, "%s() : finished Tx\n", __FUNCTION__);
  610. }
  611. }
  612. /*
  613. * Function irport_receive (self)
  614. *
  615. * Receive one frame from the infrared port
  616. *
  617. * Called only from irport_interrupt()
  618. */
  619. static inline void irport_receive(struct irport_cb *self)
  620. {
  621. int boguscount = 0;
  622. int iobase;
  623. IRDA_ASSERT(self != NULL, return;);
  624. iobase = self->io.sir_base;
  625. /*
  626. * Receive all characters in Rx FIFO, unwrap and unstuff them.
  627. * async_unwrap_char will deliver all found frames
  628. */
  629. do {
  630. async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
  631. inb(iobase+UART_RX));
  632. /* Make sure we don't stay here too long */
  633. if (boguscount++ > 32) {
  634. IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
  635. break;
  636. }
  637. } while (inb(iobase+UART_LSR) & UART_LSR_DR);
  638. }
  639. /*
  640. * Function irport_interrupt (irq, dev_id, regs)
  641. *
  642. * Interrupt handler
  643. */
  644. static irqreturn_t irport_interrupt(int irq, void *dev_id,
  645. struct pt_regs *regs)
  646. {
  647. struct net_device *dev = (struct net_device *) dev_id;
  648. struct irport_cb *self;
  649. int boguscount = 0;
  650. int iobase;
  651. int iir, lsr;
  652. int handled = 0;
  653. if (!dev) {
  654. IRDA_WARNING("%s() irq %d for unknown device.\n", __FUNCTION__, irq);
  655. return IRQ_NONE;
  656. }
  657. self = (struct irport_cb *) dev->priv;
  658. spin_lock(&self->lock);
  659. iobase = self->io.sir_base;
  660. /* Cut'n'paste interrupt routine from serial.c
  661. * This version try to minimise latency and I/O operations.
  662. * Simplified and modified to enforce half duplex operation.
  663. * - Jean II */
  664. /* Check status even is iir reg is cleared, more robust and
  665. * eliminate a read on the I/O bus - Jean II */
  666. do {
  667. /* Get interrupt status ; Clear interrupt */
  668. lsr = inb(iobase+UART_LSR);
  669. /* Are we receiving or transmitting ? */
  670. if(!self->transmitting) {
  671. /* Received something ? */
  672. if (lsr & UART_LSR_DR)
  673. irport_receive(self);
  674. } else {
  675. /* Room in Tx fifo ? */
  676. if (lsr & (UART_LSR_THRE | UART_LSR_TEMT))
  677. irport_write_wakeup(self);
  678. }
  679. /* A bit hackish, but working as expected... Jean II */
  680. if(lsr & (UART_LSR_THRE | UART_LSR_TEMT | UART_LSR_DR))
  681. handled = 1;
  682. /* Make sure we don't stay here to long */
  683. if (boguscount++ > 10) {
  684. IRDA_WARNING("%s() irq handler looping : lsr=%02x\n",
  685. __FUNCTION__, lsr);
  686. break;
  687. }
  688. /* Read interrupt register */
  689. iir = inb(iobase+UART_IIR);
  690. /* Enable this debug only when no other options and at low
  691. * bit rates, otherwise it may cause Rx overruns (lsr=63).
  692. * - Jean II */
  693. IRDA_DEBUG(6, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
  694. __FUNCTION__, iir, lsr, iobase);
  695. /* As long as interrupt pending... */
  696. } while ((iir & UART_IIR_NO_INT) == 0);
  697. spin_unlock(&self->lock);
  698. return IRQ_RETVAL(handled);
  699. }
  700. /*
  701. * Function irport_net_open (dev)
  702. *
  703. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  704. *
  705. */
  706. static int irport_net_open(struct net_device *dev)
  707. {
  708. struct irport_cb *self;
  709. int iobase;
  710. char hwname[16];
  711. unsigned long flags;
  712. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  713. IRDA_ASSERT(dev != NULL, return -1;);
  714. self = (struct irport_cb *) dev->priv;
  715. iobase = self->io.sir_base;
  716. if (request_irq(self->io.irq, self->interrupt, 0, dev->name,
  717. (void *) dev)) {
  718. IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
  719. __FUNCTION__, self->io.irq);
  720. return -EAGAIN;
  721. }
  722. spin_lock_irqsave(&self->lock, flags);
  723. /* Init uart */
  724. irport_start(self);
  725. /* Set 9600 bauds per default, including at the dongle */
  726. irda_task_execute(self, __irport_change_speed,
  727. irport_change_speed_complete,
  728. NULL, (void *) 9600);
  729. spin_unlock_irqrestore(&self->lock, flags);
  730. /* Give self a hardware name */
  731. sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base);
  732. /*
  733. * Open new IrLAP layer instance, now that everything should be
  734. * initialized properly
  735. */
  736. self->irlap = irlap_open(dev, &self->qos, hwname);
  737. /* Ready to play! */
  738. netif_start_queue(dev);
  739. return 0;
  740. }
  741. /*
  742. * Function irport_net_close (self)
  743. *
  744. * Network device is taken down. Usually this is done by
  745. * "ifconfig irda0 down"
  746. */
  747. static int irport_net_close(struct net_device *dev)
  748. {
  749. struct irport_cb *self;
  750. int iobase;
  751. unsigned long flags;
  752. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  753. IRDA_ASSERT(dev != NULL, return -1;);
  754. self = (struct irport_cb *) dev->priv;
  755. IRDA_ASSERT(self != NULL, return -1;);
  756. iobase = self->io.sir_base;
  757. /* Stop device */
  758. netif_stop_queue(dev);
  759. /* Stop and remove instance of IrLAP */
  760. if (self->irlap)
  761. irlap_close(self->irlap);
  762. self->irlap = NULL;
  763. spin_lock_irqsave(&self->lock, flags);
  764. irport_stop(self);
  765. spin_unlock_irqrestore(&self->lock, flags);
  766. free_irq(self->io.irq, dev);
  767. return 0;
  768. }
  769. /*
  770. * Function irport_is_receiving (self)
  771. *
  772. * Returns true is we are currently receiving data
  773. *
  774. */
  775. static inline int irport_is_receiving(struct irport_cb *self)
  776. {
  777. return (self->rx_buff.state != OUTSIDE_FRAME);
  778. }
  779. /*
  780. * Function irport_set_dtr_rts (tty, dtr, rts)
  781. *
  782. * This function can be used by dongles etc. to set or reset the status
  783. * of the dtr and rts lines
  784. */
  785. static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts)
  786. {
  787. struct irport_cb *self = dev->priv;
  788. int iobase;
  789. IRDA_ASSERT(self != NULL, return -1;);
  790. iobase = self->io.sir_base;
  791. if (dtr)
  792. dtr = UART_MCR_DTR;
  793. if (rts)
  794. rts = UART_MCR_RTS;
  795. outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
  796. return 0;
  797. }
  798. static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
  799. {
  800. struct irport_cb *self = (struct irport_cb *) dev->priv;
  801. int actual = 0;
  802. int iobase;
  803. IRDA_ASSERT(self != NULL, return -1;);
  804. iobase = self->io.sir_base;
  805. /* Tx FIFO should be empty! */
  806. if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
  807. IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n", __FUNCTION__);
  808. return -1;
  809. }
  810. /* Fill FIFO with current frame */
  811. while (actual < len) {
  812. /* Transmit next byte */
  813. outb(buf[actual], iobase+UART_TX);
  814. actual++;
  815. }
  816. return actual;
  817. }
  818. /*
  819. * Function irport_net_ioctl (dev, rq, cmd)
  820. *
  821. * Process IOCTL commands for this device
  822. *
  823. */
  824. static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  825. {
  826. struct if_irda_req *irq = (struct if_irda_req *) rq;
  827. struct irport_cb *self;
  828. dongle_t *dongle;
  829. unsigned long flags;
  830. int ret = 0;
  831. IRDA_ASSERT(dev != NULL, return -1;);
  832. self = dev->priv;
  833. IRDA_ASSERT(self != NULL, return -1;);
  834. IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
  835. switch (cmd) {
  836. case SIOCSBANDWIDTH: /* Set bandwidth */
  837. if (!capable(CAP_NET_ADMIN))
  838. ret = -EPERM;
  839. else
  840. irda_task_execute(self, __irport_change_speed, NULL,
  841. NULL, (void *) irq->ifr_baudrate);
  842. break;
  843. case SIOCSDONGLE: /* Set dongle */
  844. if (!capable(CAP_NET_ADMIN)) {
  845. ret = -EPERM;
  846. break;
  847. }
  848. /* Locking :
  849. * irda_device_dongle_init() can't be locked.
  850. * irda_task_execute() doesn't need to be locked.
  851. * Jean II
  852. */
  853. /* Initialize dongle */
  854. dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
  855. if (!dongle)
  856. break;
  857. dongle->set_mode = NULL;
  858. dongle->read = NULL;
  859. dongle->write = irport_raw_write;
  860. dongle->set_dtr_rts = irport_set_dtr_rts;
  861. /* Now initialize the dongle! */
  862. dongle->issue->open(dongle, &self->qos);
  863. /* Reset dongle */
  864. irda_task_execute(dongle, dongle->issue->reset, NULL, NULL,
  865. NULL);
  866. /* Make dongle available to driver only now to avoid
  867. * race conditions - Jean II */
  868. self->dongle = dongle;
  869. break;
  870. case SIOCSMEDIABUSY: /* Set media busy */
  871. if (!capable(CAP_NET_ADMIN)) {
  872. ret = -EPERM;
  873. break;
  874. }
  875. irda_device_set_media_busy(self->netdev, TRUE);
  876. break;
  877. case SIOCGRECEIVING: /* Check if we are receiving right now */
  878. irq->ifr_receiving = irport_is_receiving(self);
  879. break;
  880. case SIOCSDTRRTS:
  881. if (!capable(CAP_NET_ADMIN)) {
  882. ret = -EPERM;
  883. break;
  884. }
  885. /* No real need to lock... */
  886. spin_lock_irqsave(&self->lock, flags);
  887. irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
  888. spin_unlock_irqrestore(&self->lock, flags);
  889. break;
  890. default:
  891. ret = -EOPNOTSUPP;
  892. }
  893. return ret;
  894. }
  895. static struct net_device_stats *irport_net_get_stats(struct net_device *dev)
  896. {
  897. struct irport_cb *self = (struct irport_cb *) dev->priv;
  898. return &self->stats;
  899. }
  900. static int __init irport_init(void)
  901. {
  902. int i;
  903. for (i=0; (io[i] < 2000) && (i < 4); i++) {
  904. if (irport_open(i, io[i], irq[i]) != NULL)
  905. return 0;
  906. }
  907. /*
  908. * Maybe something failed, but we can still be usable for FIR drivers
  909. */
  910. return 0;
  911. }
  912. /*
  913. * Function irport_cleanup ()
  914. *
  915. * Close all configured ports
  916. *
  917. */
  918. static void __exit irport_cleanup(void)
  919. {
  920. int i;
  921. IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
  922. for (i=0; i < 4; i++) {
  923. if (dev_self[i])
  924. irport_close(dev_self[i]);
  925. }
  926. }
  927. MODULE_PARM(io, "1-4i");
  928. MODULE_PARM_DESC(io, "Base I/O addresses");
  929. MODULE_PARM(irq, "1-4i");
  930. MODULE_PARM_DESC(irq, "IRQ lines");
  931. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  932. MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
  933. MODULE_LICENSE("GPL");
  934. module_init(irport_init);
  935. module_exit(irport_cleanup);