amiserial.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Serial driver for the amiga builtin port.
  4. *
  5. * This code was created by taking serial.c version 4.30 from kernel
  6. * release 2.3.22, replacing all hardware related stuff with the
  7. * corresponding amiga hardware actions, and removing all irrelevant
  8. * code. As a consequence, it uses many of the constants and names
  9. * associated with the registers and bits of 16550 compatible UARTS -
  10. * but only to keep track of status, etc in the state variables. It
  11. * was done this was to make it easier to keep the code in line with
  12. * (non hardware specific) changes to serial.c.
  13. *
  14. * The port is registered with the tty driver as minor device 64, and
  15. * therefore other ports should only use 65 upwards.
  16. *
  17. * Richard Lucock 28/12/99
  18. *
  19. * Copyright (C) 1991, 1992 Linus Torvalds
  20. * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
  21. * 1998, 1999 Theodore Ts'o
  22. *
  23. */
  24. /* Set of debugging defines */
  25. #undef SERIAL_DEBUG_INTR
  26. #undef SERIAL_DEBUG_OPEN
  27. #undef SERIAL_DEBUG_FLOW
  28. #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  29. /*
  30. * End of serial driver configuration section.
  31. */
  32. #include <linux/bitops.h>
  33. #include <linux/circ_buf.h>
  34. #include <linux/console.h>
  35. #include <linux/delay.h>
  36. #include <linux/errno.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/init.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/ioport.h>
  41. #include <linux/kernel.h>
  42. #include <linux/major.h>
  43. #include <linux/mm.h>
  44. #include <linux/module.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/ptrace.h>
  47. #include <linux/seq_file.h>
  48. #include <linux/serial.h>
  49. #include <linux/serial_reg.h>
  50. #include <linux/serial_core.h>
  51. #include <linux/sched.h>
  52. #include <linux/signal.h>
  53. #include <linux/slab.h>
  54. #include <linux/string.h>
  55. #include <linux/timer.h>
  56. #include <linux/tty_flip.h>
  57. #include <linux/tty.h>
  58. #include <linux/types.h>
  59. #include <linux/uaccess.h>
  60. #include <asm/amigahw.h>
  61. #include <asm/amigaints.h>
  62. #include <asm/irq.h>
  63. #include <asm/setup.h>
  64. struct serial_state {
  65. struct tty_port tport;
  66. struct circ_buf xmit;
  67. struct async_icount icount;
  68. unsigned long port;
  69. int baud_base;
  70. int custom_divisor;
  71. int read_status_mask;
  72. int ignore_status_mask;
  73. int timeout;
  74. int quot;
  75. int IER; /* Interrupt Enable Register */
  76. int MCR; /* Modem control register */
  77. u8 x_char; /* xon/xoff character */
  78. };
  79. static struct tty_driver *serial_driver;
  80. /* number of characters left in xmit buffer before we ask for more */
  81. #define WAKEUP_CHARS 256
  82. #define XMIT_FIFO_SIZE 1
  83. static unsigned char current_ctl_bits;
  84. static void change_speed(struct tty_struct *tty, struct serial_state *info,
  85. const struct ktermios *old);
  86. static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
  87. static struct serial_state serial_state;
  88. /* some serial hardware definitions */
  89. #define SDR_OVRUN (1<<15)
  90. #define SDR_RBF (1<<14)
  91. #define SDR_TBE (1<<13)
  92. #define SDR_TSRE (1<<12)
  93. #define SERPER_PARENB (1<<15)
  94. #define AC_SETCLR (1<<15)
  95. #define AC_UARTBRK (1<<11)
  96. #define SER_DTR (1<<7)
  97. #define SER_RTS (1<<6)
  98. #define SER_DCD (1<<5)
  99. #define SER_CTS (1<<4)
  100. #define SER_DSR (1<<3)
  101. static __inline__ void rtsdtr_ctrl(int bits)
  102. {
  103. ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
  104. }
  105. /*
  106. * ------------------------------------------------------------
  107. * rs_stop() and rs_start()
  108. *
  109. * This routines are called before setting or resetting tty->flow.stopped.
  110. * They enable or disable transmitter interrupts, as necessary.
  111. * ------------------------------------------------------------
  112. */
  113. static void rs_stop(struct tty_struct *tty)
  114. {
  115. struct serial_state *info = tty->driver_data;
  116. unsigned long flags;
  117. local_irq_save(flags);
  118. if (info->IER & UART_IER_THRI) {
  119. info->IER &= ~UART_IER_THRI;
  120. /* disable Tx interrupt and remove any pending interrupts */
  121. amiga_custom.intena = IF_TBE;
  122. mb();
  123. amiga_custom.intreq = IF_TBE;
  124. mb();
  125. }
  126. local_irq_restore(flags);
  127. }
  128. static void rs_start(struct tty_struct *tty)
  129. {
  130. struct serial_state *info = tty->driver_data;
  131. unsigned long flags;
  132. local_irq_save(flags);
  133. if (info->xmit.head != info->xmit.tail
  134. && info->xmit.buf
  135. && !(info->IER & UART_IER_THRI)) {
  136. info->IER |= UART_IER_THRI;
  137. amiga_custom.intena = IF_SETCLR | IF_TBE;
  138. mb();
  139. /* set a pending Tx Interrupt, transmitter should restart now */
  140. amiga_custom.intreq = IF_SETCLR | IF_TBE;
  141. mb();
  142. }
  143. local_irq_restore(flags);
  144. }
  145. /*
  146. * ----------------------------------------------------------------------
  147. *
  148. * Here start the interrupt handling routines.
  149. *
  150. * -----------------------------------------------------------------------
  151. */
  152. static void receive_chars(struct serial_state *info)
  153. {
  154. int status;
  155. int serdatr;
  156. u8 ch, flag;
  157. struct async_icount *icount;
  158. bool overrun = false;
  159. icount = &info->icount;
  160. status = UART_LSR_DR; /* We obviously have a character! */
  161. serdatr = amiga_custom.serdatr;
  162. mb();
  163. amiga_custom.intreq = IF_RBF;
  164. mb();
  165. if((serdatr & 0x1ff) == 0)
  166. status |= UART_LSR_BI;
  167. if(serdatr & SDR_OVRUN)
  168. status |= UART_LSR_OE;
  169. ch = serdatr & 0xff;
  170. icount->rx++;
  171. #ifdef SERIAL_DEBUG_INTR
  172. printk("DR%02x:%02x...", ch, status);
  173. #endif
  174. flag = TTY_NORMAL;
  175. /*
  176. * We don't handle parity or frame errors - but I have left
  177. * the code in, since I'm not sure that the errors can't be
  178. * detected.
  179. */
  180. if (status & (UART_LSR_BI | UART_LSR_PE |
  181. UART_LSR_FE | UART_LSR_OE)) {
  182. /*
  183. * For statistics only
  184. */
  185. if (status & UART_LSR_BI) {
  186. status &= ~(UART_LSR_FE | UART_LSR_PE);
  187. icount->brk++;
  188. } else if (status & UART_LSR_PE)
  189. icount->parity++;
  190. else if (status & UART_LSR_FE)
  191. icount->frame++;
  192. if (status & UART_LSR_OE)
  193. icount->overrun++;
  194. /*
  195. * Now check to see if character should be
  196. * ignored, and mask off conditions which
  197. * should be ignored.
  198. */
  199. if (status & info->ignore_status_mask)
  200. return;
  201. status &= info->read_status_mask;
  202. if (status & (UART_LSR_BI)) {
  203. #ifdef SERIAL_DEBUG_INTR
  204. printk("handling break....");
  205. #endif
  206. flag = TTY_BREAK;
  207. if (info->tport.flags & ASYNC_SAK)
  208. do_SAK(info->tport.tty);
  209. } else if (status & UART_LSR_PE)
  210. flag = TTY_PARITY;
  211. else if (status & UART_LSR_FE)
  212. flag = TTY_FRAME;
  213. if (status & UART_LSR_OE) {
  214. /*
  215. * Overrun is special, since it's
  216. * reported immediately, and doesn't
  217. * affect the current character
  218. */
  219. overrun = true;
  220. }
  221. }
  222. tty_insert_flip_char(&info->tport, ch, flag);
  223. if (overrun)
  224. tty_insert_flip_char(&info->tport, 0, TTY_OVERRUN);
  225. tty_flip_buffer_push(&info->tport);
  226. }
  227. static void transmit_chars(struct serial_state *info)
  228. {
  229. amiga_custom.intreq = IF_TBE;
  230. mb();
  231. if (info->x_char) {
  232. amiga_custom.serdat = info->x_char | 0x100;
  233. mb();
  234. info->icount.tx++;
  235. info->x_char = 0;
  236. return;
  237. }
  238. if (info->xmit.head == info->xmit.tail
  239. || info->tport.tty->flow.stopped
  240. || info->tport.tty->hw_stopped) {
  241. info->IER &= ~UART_IER_THRI;
  242. amiga_custom.intena = IF_TBE;
  243. mb();
  244. return;
  245. }
  246. amiga_custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
  247. mb();
  248. info->xmit.tail = info->xmit.tail & (UART_XMIT_SIZE - 1);
  249. info->icount.tx++;
  250. if (CIRC_CNT(info->xmit.head,
  251. info->xmit.tail,
  252. UART_XMIT_SIZE) < WAKEUP_CHARS)
  253. tty_wakeup(info->tport.tty);
  254. #ifdef SERIAL_DEBUG_INTR
  255. printk("THRE...");
  256. #endif
  257. if (info->xmit.head == info->xmit.tail) {
  258. amiga_custom.intena = IF_TBE;
  259. mb();
  260. info->IER &= ~UART_IER_THRI;
  261. }
  262. }
  263. static void check_modem_status(struct serial_state *info)
  264. {
  265. struct tty_port *port = &info->tport;
  266. unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  267. unsigned char dstatus;
  268. struct async_icount *icount;
  269. /* Determine bits that have changed */
  270. dstatus = status ^ current_ctl_bits;
  271. current_ctl_bits = status;
  272. if (dstatus) {
  273. icount = &info->icount;
  274. /* update input line counters */
  275. if (dstatus & SER_DSR)
  276. icount->dsr++;
  277. if (dstatus & SER_DCD) {
  278. icount->dcd++;
  279. }
  280. if (dstatus & SER_CTS)
  281. icount->cts++;
  282. wake_up_interruptible(&port->delta_msr_wait);
  283. }
  284. if (tty_port_check_carrier(port) && (dstatus & SER_DCD)) {
  285. #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
  286. printk("ttyS%d CD now %s...", info->line,
  287. (!(status & SER_DCD)) ? "on" : "off");
  288. #endif
  289. if (!(status & SER_DCD))
  290. wake_up_interruptible(&port->open_wait);
  291. else {
  292. #ifdef SERIAL_DEBUG_OPEN
  293. printk("doing serial hangup...");
  294. #endif
  295. if (port->tty)
  296. tty_hangup(port->tty);
  297. }
  298. }
  299. if (tty_port_cts_enabled(port)) {
  300. if (port->tty->hw_stopped) {
  301. if (!(status & SER_CTS)) {
  302. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  303. printk("CTS tx start...");
  304. #endif
  305. port->tty->hw_stopped = false;
  306. info->IER |= UART_IER_THRI;
  307. amiga_custom.intena = IF_SETCLR | IF_TBE;
  308. mb();
  309. /* set a pending Tx Interrupt, transmitter should restart now */
  310. amiga_custom.intreq = IF_SETCLR | IF_TBE;
  311. mb();
  312. tty_wakeup(port->tty);
  313. return;
  314. }
  315. } else {
  316. if ((status & SER_CTS)) {
  317. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  318. printk("CTS tx stop...");
  319. #endif
  320. port->tty->hw_stopped = true;
  321. info->IER &= ~UART_IER_THRI;
  322. /* disable Tx interrupt and remove any pending interrupts */
  323. amiga_custom.intena = IF_TBE;
  324. mb();
  325. amiga_custom.intreq = IF_TBE;
  326. mb();
  327. }
  328. }
  329. }
  330. }
  331. static irqreturn_t ser_vbl_int( int irq, void *data)
  332. {
  333. /* vbl is just a periodic interrupt we tie into to update modem status */
  334. struct serial_state *info = data;
  335. /*
  336. * TBD - is it better to unregister from this interrupt or to
  337. * ignore it if MSI is clear ?
  338. */
  339. if(info->IER & UART_IER_MSI)
  340. check_modem_status(info);
  341. return IRQ_HANDLED;
  342. }
  343. static irqreturn_t ser_rx_int(int irq, void *dev_id)
  344. {
  345. struct serial_state *info = dev_id;
  346. #ifdef SERIAL_DEBUG_INTR
  347. printk("ser_rx_int...");
  348. #endif
  349. if (!info->tport.tty)
  350. return IRQ_NONE;
  351. receive_chars(info);
  352. #ifdef SERIAL_DEBUG_INTR
  353. printk("end.\n");
  354. #endif
  355. return IRQ_HANDLED;
  356. }
  357. static irqreturn_t ser_tx_int(int irq, void *dev_id)
  358. {
  359. struct serial_state *info = dev_id;
  360. if (amiga_custom.serdatr & SDR_TBE) {
  361. #ifdef SERIAL_DEBUG_INTR
  362. printk("ser_tx_int...");
  363. #endif
  364. if (!info->tport.tty)
  365. return IRQ_NONE;
  366. transmit_chars(info);
  367. #ifdef SERIAL_DEBUG_INTR
  368. printk("end.\n");
  369. #endif
  370. }
  371. return IRQ_HANDLED;
  372. }
  373. /*
  374. * -------------------------------------------------------------------
  375. * Here ends the serial interrupt routines.
  376. * -------------------------------------------------------------------
  377. */
  378. /*
  379. * ---------------------------------------------------------------
  380. * Low level utility subroutines for the serial driver: routines to
  381. * figure out the appropriate timeout for an interrupt chain, routines
  382. * to initialize and startup a serial port, and routines to shutdown a
  383. * serial port. Useful stuff like that.
  384. * ---------------------------------------------------------------
  385. */
  386. static int startup(struct tty_struct *tty, struct serial_state *info)
  387. {
  388. struct tty_port *port = &info->tport;
  389. unsigned long flags;
  390. int retval=0;
  391. unsigned long page;
  392. page = get_zeroed_page(GFP_KERNEL);
  393. if (!page)
  394. return -ENOMEM;
  395. local_irq_save(flags);
  396. if (tty_port_initialized(port)) {
  397. free_page(page);
  398. goto errout;
  399. }
  400. if (info->xmit.buf)
  401. free_page(page);
  402. else
  403. info->xmit.buf = (unsigned char *) page;
  404. #ifdef SERIAL_DEBUG_OPEN
  405. printk("starting up ttys%d ...", info->line);
  406. #endif
  407. /* Clear anything in the input buffer */
  408. amiga_custom.intreq = IF_RBF;
  409. mb();
  410. retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
  411. if (retval) {
  412. if (capable(CAP_SYS_ADMIN)) {
  413. set_bit(TTY_IO_ERROR, &tty->flags);
  414. retval = 0;
  415. }
  416. goto errout;
  417. }
  418. /* enable both Rx and Tx interrupts */
  419. amiga_custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
  420. mb();
  421. info->IER = UART_IER_MSI;
  422. /* remember current state of the DCD and CTS bits */
  423. current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  424. info->MCR = 0;
  425. if (C_BAUD(tty))
  426. info->MCR = SER_DTR | SER_RTS;
  427. rtsdtr_ctrl(info->MCR);
  428. clear_bit(TTY_IO_ERROR, &tty->flags);
  429. info->xmit.head = info->xmit.tail = 0;
  430. /*
  431. * and set the speed of the serial port
  432. */
  433. change_speed(tty, info, NULL);
  434. tty_port_set_initialized(port, true);
  435. local_irq_restore(flags);
  436. return 0;
  437. errout:
  438. local_irq_restore(flags);
  439. return retval;
  440. }
  441. /*
  442. * This routine will shutdown a serial port; interrupts are disabled, and
  443. * DTR is dropped if the hangup on close termio flag is on.
  444. */
  445. static void shutdown(struct tty_struct *tty, struct serial_state *info)
  446. {
  447. unsigned long flags;
  448. if (!tty_port_initialized(&info->tport))
  449. return;
  450. #ifdef SERIAL_DEBUG_OPEN
  451. printk("Shutting down serial port %d ....\n", info->line);
  452. #endif
  453. local_irq_save(flags); /* Disable interrupts */
  454. /*
  455. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  456. * here so the queue might never be waken up
  457. */
  458. wake_up_interruptible(&info->tport.delta_msr_wait);
  459. /*
  460. * Free the IRQ, if necessary
  461. */
  462. free_irq(IRQ_AMIGA_VERTB, info);
  463. free_page((unsigned long)info->xmit.buf);
  464. info->xmit.buf = NULL;
  465. info->IER = 0;
  466. amiga_custom.intena = IF_RBF | IF_TBE;
  467. mb();
  468. /* disable break condition */
  469. amiga_custom.adkcon = AC_UARTBRK;
  470. mb();
  471. if (C_HUPCL(tty))
  472. info->MCR &= ~(SER_DTR|SER_RTS);
  473. rtsdtr_ctrl(info->MCR);
  474. set_bit(TTY_IO_ERROR, &tty->flags);
  475. tty_port_set_initialized(&info->tport, false);
  476. local_irq_restore(flags);
  477. }
  478. /*
  479. * This routine is called to set the UART divisor registers to match
  480. * the specified baud rate for a serial port.
  481. */
  482. static void change_speed(struct tty_struct *tty, struct serial_state *info,
  483. const struct ktermios *old_termios)
  484. {
  485. struct tty_port *port = &info->tport;
  486. int quot = 0, baud_base, baud;
  487. unsigned cflag, cval = 0;
  488. int bits;
  489. unsigned long flags;
  490. cflag = tty->termios.c_cflag;
  491. /* Byte size is always 8 bits plus parity bit if requested */
  492. cval = 3; bits = 10;
  493. if (cflag & CSTOPB) {
  494. cval |= 0x04;
  495. bits++;
  496. }
  497. if (cflag & PARENB) {
  498. cval |= UART_LCR_PARITY;
  499. bits++;
  500. }
  501. if (!(cflag & PARODD))
  502. cval |= UART_LCR_EPAR;
  503. if (cflag & CMSPAR)
  504. cval |= UART_LCR_SPAR;
  505. /* Determine divisor based on baud rate */
  506. baud = tty_get_baud_rate(tty);
  507. if (!baud)
  508. baud = 9600; /* B0 transition handled in rs_set_termios */
  509. baud_base = info->baud_base;
  510. if (baud == 38400 && (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  511. quot = info->custom_divisor;
  512. else {
  513. if (baud == 134)
  514. /* Special case since 134 is really 134.5 */
  515. quot = (2*baud_base / 269);
  516. else if (baud)
  517. quot = baud_base / baud;
  518. }
  519. /* If the quotient is zero refuse the change */
  520. if (!quot && old_termios) {
  521. /* FIXME: Will need updating for new tty in the end */
  522. tty->termios.c_cflag &= ~CBAUD;
  523. tty->termios.c_cflag |= (old_termios->c_cflag & CBAUD);
  524. baud = tty_get_baud_rate(tty);
  525. if (!baud)
  526. baud = 9600;
  527. if (baud == 38400 &&
  528. (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  529. quot = info->custom_divisor;
  530. else {
  531. if (baud == 134)
  532. /* Special case since 134 is really 134.5 */
  533. quot = (2*baud_base / 269);
  534. else if (baud)
  535. quot = baud_base / baud;
  536. }
  537. }
  538. /* As a last resort, if the quotient is zero, default to 9600 bps */
  539. if (!quot)
  540. quot = baud_base / 9600;
  541. info->quot = quot;
  542. info->timeout = (XMIT_FIFO_SIZE*HZ*bits*quot) / baud_base;
  543. info->timeout += HZ/50; /* Add .02 seconds of slop */
  544. /* CTS flow control flag and modem status interrupts */
  545. info->IER &= ~UART_IER_MSI;
  546. if (port->flags & ASYNC_HARDPPS_CD)
  547. info->IER |= UART_IER_MSI;
  548. tty_port_set_cts_flow(port, cflag & CRTSCTS);
  549. if (cflag & CRTSCTS)
  550. info->IER |= UART_IER_MSI;
  551. tty_port_set_check_carrier(port, ~cflag & CLOCAL);
  552. if (~cflag & CLOCAL)
  553. info->IER |= UART_IER_MSI;
  554. /* TBD:
  555. * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
  556. */
  557. /*
  558. * Set up parity check flag
  559. */
  560. info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
  561. if (I_INPCK(tty))
  562. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  563. if (I_BRKINT(tty) || I_PARMRK(tty))
  564. info->read_status_mask |= UART_LSR_BI;
  565. /*
  566. * Characters to ignore
  567. */
  568. info->ignore_status_mask = 0;
  569. if (I_IGNPAR(tty))
  570. info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  571. if (I_IGNBRK(tty)) {
  572. info->ignore_status_mask |= UART_LSR_BI;
  573. /*
  574. * If we're ignore parity and break indicators, ignore
  575. * overruns too. (For real raw support).
  576. */
  577. if (I_IGNPAR(tty))
  578. info->ignore_status_mask |= UART_LSR_OE;
  579. }
  580. /*
  581. * !!! ignore all characters if CREAD is not set
  582. */
  583. if ((cflag & CREAD) == 0)
  584. info->ignore_status_mask |= UART_LSR_DR;
  585. local_irq_save(flags);
  586. {
  587. short serper;
  588. /* Set up the baud rate */
  589. serper = quot - 1;
  590. /* Enable or disable parity bit */
  591. if(cval & UART_LCR_PARITY)
  592. serper |= (SERPER_PARENB);
  593. amiga_custom.serper = serper;
  594. mb();
  595. }
  596. local_irq_restore(flags);
  597. }
  598. static int rs_put_char(struct tty_struct *tty, u8 ch)
  599. {
  600. struct serial_state *info;
  601. unsigned long flags;
  602. info = tty->driver_data;
  603. if (!info->xmit.buf)
  604. return 0;
  605. local_irq_save(flags);
  606. if (CIRC_SPACE(info->xmit.head,
  607. info->xmit.tail,
  608. UART_XMIT_SIZE) == 0) {
  609. local_irq_restore(flags);
  610. return 0;
  611. }
  612. info->xmit.buf[info->xmit.head++] = ch;
  613. info->xmit.head &= UART_XMIT_SIZE - 1;
  614. local_irq_restore(flags);
  615. return 1;
  616. }
  617. static void rs_flush_chars(struct tty_struct *tty)
  618. {
  619. struct serial_state *info = tty->driver_data;
  620. unsigned long flags;
  621. if (info->xmit.head == info->xmit.tail
  622. || tty->flow.stopped
  623. || tty->hw_stopped
  624. || !info->xmit.buf)
  625. return;
  626. local_irq_save(flags);
  627. info->IER |= UART_IER_THRI;
  628. amiga_custom.intena = IF_SETCLR | IF_TBE;
  629. mb();
  630. /* set a pending Tx Interrupt, transmitter should restart now */
  631. amiga_custom.intreq = IF_SETCLR | IF_TBE;
  632. mb();
  633. local_irq_restore(flags);
  634. }
  635. static ssize_t rs_write(struct tty_struct * tty, const u8 *buf, size_t count)
  636. {
  637. int c, ret = 0;
  638. struct serial_state *info = tty->driver_data;
  639. unsigned long flags;
  640. if (!info->xmit.buf)
  641. return 0;
  642. local_irq_save(flags);
  643. while (1) {
  644. c = CIRC_SPACE_TO_END(info->xmit.head,
  645. info->xmit.tail,
  646. UART_XMIT_SIZE);
  647. if (count < c)
  648. c = count;
  649. if (c <= 0) {
  650. break;
  651. }
  652. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  653. info->xmit.head = (info->xmit.head + c) & (UART_XMIT_SIZE - 1);
  654. buf += c;
  655. count -= c;
  656. ret += c;
  657. }
  658. local_irq_restore(flags);
  659. if (info->xmit.head != info->xmit.tail
  660. && !tty->flow.stopped
  661. && !tty->hw_stopped
  662. && !(info->IER & UART_IER_THRI)) {
  663. info->IER |= UART_IER_THRI;
  664. local_irq_disable();
  665. amiga_custom.intena = IF_SETCLR | IF_TBE;
  666. mb();
  667. /* set a pending Tx Interrupt, transmitter should restart now */
  668. amiga_custom.intreq = IF_SETCLR | IF_TBE;
  669. mb();
  670. local_irq_restore(flags);
  671. }
  672. return ret;
  673. }
  674. static unsigned int rs_write_room(struct tty_struct *tty)
  675. {
  676. struct serial_state *info = tty->driver_data;
  677. return CIRC_SPACE(info->xmit.head, info->xmit.tail, UART_XMIT_SIZE);
  678. }
  679. static unsigned int rs_chars_in_buffer(struct tty_struct *tty)
  680. {
  681. struct serial_state *info = tty->driver_data;
  682. return CIRC_CNT(info->xmit.head, info->xmit.tail, UART_XMIT_SIZE);
  683. }
  684. static void rs_flush_buffer(struct tty_struct *tty)
  685. {
  686. struct serial_state *info = tty->driver_data;
  687. unsigned long flags;
  688. local_irq_save(flags);
  689. info->xmit.head = info->xmit.tail = 0;
  690. local_irq_restore(flags);
  691. tty_wakeup(tty);
  692. }
  693. /*
  694. * This function is used to send a high-priority XON/XOFF character to
  695. * the device
  696. */
  697. static void rs_send_xchar(struct tty_struct *tty, u8 ch)
  698. {
  699. struct serial_state *info = tty->driver_data;
  700. unsigned long flags;
  701. info->x_char = ch;
  702. if (ch) {
  703. /* Make sure transmit interrupts are on */
  704. /* Check this ! */
  705. local_irq_save(flags);
  706. if(!(amiga_custom.intenar & IF_TBE)) {
  707. amiga_custom.intena = IF_SETCLR | IF_TBE;
  708. mb();
  709. /* set a pending Tx Interrupt, transmitter should restart now */
  710. amiga_custom.intreq = IF_SETCLR | IF_TBE;
  711. mb();
  712. }
  713. local_irq_restore(flags);
  714. info->IER |= UART_IER_THRI;
  715. }
  716. }
  717. /*
  718. * ------------------------------------------------------------
  719. * rs_throttle()
  720. *
  721. * This routine is called by the upper-layer tty layer to signal that
  722. * incoming characters should be throttled.
  723. * ------------------------------------------------------------
  724. */
  725. static void rs_throttle(struct tty_struct * tty)
  726. {
  727. struct serial_state *info = tty->driver_data;
  728. unsigned long flags;
  729. #ifdef SERIAL_DEBUG_THROTTLE
  730. printk("throttle %s ....\n", tty_name(tty));
  731. #endif
  732. if (I_IXOFF(tty))
  733. rs_send_xchar(tty, STOP_CHAR(tty));
  734. if (C_CRTSCTS(tty))
  735. info->MCR &= ~SER_RTS;
  736. local_irq_save(flags);
  737. rtsdtr_ctrl(info->MCR);
  738. local_irq_restore(flags);
  739. }
  740. static void rs_unthrottle(struct tty_struct * tty)
  741. {
  742. struct serial_state *info = tty->driver_data;
  743. unsigned long flags;
  744. #ifdef SERIAL_DEBUG_THROTTLE
  745. printk("unthrottle %s ....\n", tty_name(tty));
  746. #endif
  747. if (I_IXOFF(tty)) {
  748. if (info->x_char)
  749. info->x_char = 0;
  750. else
  751. rs_send_xchar(tty, START_CHAR(tty));
  752. }
  753. if (C_CRTSCTS(tty))
  754. info->MCR |= SER_RTS;
  755. local_irq_save(flags);
  756. rtsdtr_ctrl(info->MCR);
  757. local_irq_restore(flags);
  758. }
  759. /*
  760. * ------------------------------------------------------------
  761. * rs_ioctl() and friends
  762. * ------------------------------------------------------------
  763. */
  764. static int get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
  765. {
  766. struct serial_state *state = tty->driver_data;
  767. unsigned int close_delay, closing_wait;
  768. tty_lock(tty);
  769. close_delay = jiffies_to_msecs(state->tport.close_delay) / 10;
  770. closing_wait = state->tport.closing_wait;
  771. if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
  772. closing_wait = jiffies_to_msecs(closing_wait) / 10;
  773. ss->line = tty->index;
  774. ss->port = state->port;
  775. ss->flags = state->tport.flags;
  776. ss->xmit_fifo_size = XMIT_FIFO_SIZE;
  777. ss->baud_base = state->baud_base;
  778. ss->close_delay = close_delay;
  779. ss->closing_wait = closing_wait;
  780. ss->custom_divisor = state->custom_divisor;
  781. tty_unlock(tty);
  782. return 0;
  783. }
  784. static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
  785. {
  786. struct serial_state *state = tty->driver_data;
  787. struct tty_port *port = &state->tport;
  788. bool change_spd;
  789. int retval = 0;
  790. unsigned int close_delay, closing_wait;
  791. tty_lock(tty);
  792. change_spd = ((ss->flags ^ port->flags) & ASYNC_SPD_MASK) ||
  793. ss->custom_divisor != state->custom_divisor;
  794. if (ss->irq || ss->port != state->port ||
  795. ss->xmit_fifo_size != XMIT_FIFO_SIZE) {
  796. tty_unlock(tty);
  797. return -EINVAL;
  798. }
  799. close_delay = msecs_to_jiffies(ss->close_delay * 10);
  800. closing_wait = ss->closing_wait;
  801. if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
  802. closing_wait = msecs_to_jiffies(closing_wait * 10);
  803. if (!capable(CAP_SYS_ADMIN)) {
  804. if ((ss->baud_base != state->baud_base) ||
  805. (close_delay != port->close_delay) ||
  806. (closing_wait != port->closing_wait) ||
  807. ((ss->flags & ~ASYNC_USR_MASK) !=
  808. (port->flags & ~ASYNC_USR_MASK))) {
  809. tty_unlock(tty);
  810. return -EPERM;
  811. }
  812. port->flags = ((port->flags & ~ASYNC_USR_MASK) |
  813. (ss->flags & ASYNC_USR_MASK));
  814. state->custom_divisor = ss->custom_divisor;
  815. goto check_and_exit;
  816. }
  817. if (ss->baud_base < 9600) {
  818. tty_unlock(tty);
  819. return -EINVAL;
  820. }
  821. /*
  822. * OK, past this point, all the error checking has been done.
  823. * At this point, we start making changes.....
  824. */
  825. state->baud_base = ss->baud_base;
  826. port->flags = ((port->flags & ~ASYNC_FLAGS) |
  827. (ss->flags & ASYNC_FLAGS));
  828. state->custom_divisor = ss->custom_divisor;
  829. port->close_delay = close_delay;
  830. port->closing_wait = closing_wait;
  831. check_and_exit:
  832. if (tty_port_initialized(port)) {
  833. if (change_spd) {
  834. /* warn about deprecation unless clearing */
  835. if (ss->flags & ASYNC_SPD_MASK)
  836. dev_warn_ratelimited(tty->dev, "use of SPD flags is deprecated\n");
  837. change_speed(tty, state, NULL);
  838. }
  839. } else
  840. retval = startup(tty, state);
  841. tty_unlock(tty);
  842. return retval;
  843. }
  844. /*
  845. * get_lsr_info - get line status register info
  846. *
  847. * Purpose: Let user call ioctl() to get info when the UART physically
  848. * is emptied. On bus types like RS485, the transmitter must
  849. * release the bus after transmitting. This must be done when
  850. * the transmit shift register is empty, not be done when the
  851. * transmit holding register is empty. This functionality
  852. * allows an RS485 driver to be written in user space.
  853. */
  854. static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
  855. {
  856. unsigned char status;
  857. unsigned int result;
  858. unsigned long flags;
  859. local_irq_save(flags);
  860. status = amiga_custom.serdatr;
  861. mb();
  862. local_irq_restore(flags);
  863. result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
  864. if (copy_to_user(value, &result, sizeof(int)))
  865. return -EFAULT;
  866. return 0;
  867. }
  868. static int rs_tiocmget(struct tty_struct *tty)
  869. {
  870. struct serial_state *info = tty->driver_data;
  871. unsigned char control, status;
  872. unsigned long flags;
  873. if (tty_io_error(tty))
  874. return -EIO;
  875. control = info->MCR;
  876. local_irq_save(flags);
  877. status = ciab.pra;
  878. local_irq_restore(flags);
  879. return ((control & SER_RTS) ? TIOCM_RTS : 0)
  880. | ((control & SER_DTR) ? TIOCM_DTR : 0)
  881. | (!(status & SER_DCD) ? TIOCM_CAR : 0)
  882. | (!(status & SER_DSR) ? TIOCM_DSR : 0)
  883. | (!(status & SER_CTS) ? TIOCM_CTS : 0);
  884. }
  885. static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
  886. unsigned int clear)
  887. {
  888. struct serial_state *info = tty->driver_data;
  889. unsigned long flags;
  890. if (tty_io_error(tty))
  891. return -EIO;
  892. local_irq_save(flags);
  893. if (set & TIOCM_RTS)
  894. info->MCR |= SER_RTS;
  895. if (set & TIOCM_DTR)
  896. info->MCR |= SER_DTR;
  897. if (clear & TIOCM_RTS)
  898. info->MCR &= ~SER_RTS;
  899. if (clear & TIOCM_DTR)
  900. info->MCR &= ~SER_DTR;
  901. rtsdtr_ctrl(info->MCR);
  902. local_irq_restore(flags);
  903. return 0;
  904. }
  905. /*
  906. * rs_break() --- routine which turns the break handling on or off
  907. */
  908. static int rs_break(struct tty_struct *tty, int break_state)
  909. {
  910. unsigned long flags;
  911. local_irq_save(flags);
  912. if (break_state == -1)
  913. amiga_custom.adkcon = AC_SETCLR | AC_UARTBRK;
  914. else
  915. amiga_custom.adkcon = AC_UARTBRK;
  916. mb();
  917. local_irq_restore(flags);
  918. return 0;
  919. }
  920. /*
  921. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  922. * Return: write counters to the user passed counter struct
  923. * NB: both 1->0 and 0->1 transitions are counted except for
  924. * RI where only 0->1 is counted.
  925. */
  926. static int rs_get_icount(struct tty_struct *tty,
  927. struct serial_icounter_struct *icount)
  928. {
  929. struct serial_state *info = tty->driver_data;
  930. struct async_icount cnow;
  931. unsigned long flags;
  932. local_irq_save(flags);
  933. cnow = info->icount;
  934. local_irq_restore(flags);
  935. icount->cts = cnow.cts;
  936. icount->dsr = cnow.dsr;
  937. icount->rng = cnow.rng;
  938. icount->dcd = cnow.dcd;
  939. icount->rx = cnow.rx;
  940. icount->tx = cnow.tx;
  941. icount->frame = cnow.frame;
  942. icount->overrun = cnow.overrun;
  943. icount->parity = cnow.parity;
  944. icount->brk = cnow.brk;
  945. icount->buf_overrun = cnow.buf_overrun;
  946. return 0;
  947. }
  948. static int rs_ioctl(struct tty_struct *tty,
  949. unsigned int cmd, unsigned long arg)
  950. {
  951. struct serial_state *info = tty->driver_data;
  952. struct async_icount cprev, cnow; /* kernel counter temps */
  953. void __user *argp = (void __user *)arg;
  954. unsigned long flags;
  955. DEFINE_WAIT(wait);
  956. int ret;
  957. if ((cmd != TIOCSERCONFIG) &&
  958. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  959. if (tty_io_error(tty))
  960. return -EIO;
  961. }
  962. switch (cmd) {
  963. case TIOCSERCONFIG:
  964. return 0;
  965. case TIOCSERGETLSR: /* Get line status register */
  966. return get_lsr_info(info, argp);
  967. /*
  968. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  969. * - mask passed in arg for lines of interest
  970. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  971. * Caller should use TIOCGICOUNT to see which one it was
  972. */
  973. case TIOCMIWAIT:
  974. local_irq_save(flags);
  975. /* note the counters on entry */
  976. cprev = info->icount;
  977. local_irq_restore(flags);
  978. while (1) {
  979. prepare_to_wait(&info->tport.delta_msr_wait,
  980. &wait, TASK_INTERRUPTIBLE);
  981. local_irq_save(flags);
  982. cnow = info->icount; /* atomic copy */
  983. local_irq_restore(flags);
  984. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  985. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  986. ret = -EIO; /* no change => error */
  987. break;
  988. }
  989. if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  990. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  991. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  992. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  993. ret = 0;
  994. break;
  995. }
  996. schedule();
  997. /* see if a signal did it */
  998. if (signal_pending(current)) {
  999. ret = -ERESTARTSYS;
  1000. break;
  1001. }
  1002. cprev = cnow;
  1003. }
  1004. finish_wait(&info->tport.delta_msr_wait, &wait);
  1005. return ret;
  1006. default:
  1007. return -ENOIOCTLCMD;
  1008. }
  1009. return 0;
  1010. }
  1011. static void rs_set_termios(struct tty_struct *tty, const struct ktermios *old_termios)
  1012. {
  1013. struct serial_state *info = tty->driver_data;
  1014. unsigned long flags;
  1015. unsigned int cflag = tty->termios.c_cflag;
  1016. change_speed(tty, info, old_termios);
  1017. /* Handle transition to B0 status */
  1018. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
  1019. info->MCR &= ~(SER_DTR|SER_RTS);
  1020. local_irq_save(flags);
  1021. rtsdtr_ctrl(info->MCR);
  1022. local_irq_restore(flags);
  1023. }
  1024. /* Handle transition away from B0 status */
  1025. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  1026. info->MCR |= SER_DTR;
  1027. if (!C_CRTSCTS(tty) || !tty_throttled(tty))
  1028. info->MCR |= SER_RTS;
  1029. local_irq_save(flags);
  1030. rtsdtr_ctrl(info->MCR);
  1031. local_irq_restore(flags);
  1032. }
  1033. /* Handle turning off CRTSCTS */
  1034. if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
  1035. tty->hw_stopped = false;
  1036. rs_start(tty);
  1037. }
  1038. #if 0
  1039. /*
  1040. * No need to wake up processes in open wait, since they
  1041. * sample the CLOCAL flag once, and don't recheck it.
  1042. * XXX It's not clear whether the current behavior is correct
  1043. * or not. Hence, this may change.....
  1044. */
  1045. if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
  1046. wake_up_interruptible(&info->open_wait);
  1047. #endif
  1048. }
  1049. /*
  1050. * ------------------------------------------------------------
  1051. * rs_close()
  1052. *
  1053. * This routine is called when the serial port gets closed. First, we
  1054. * wait for the last remaining data to be sent. Then, we unlink its
  1055. * async structure from the interrupt chain if necessary, and we free
  1056. * that IRQ if nothing is left in the chain.
  1057. * ------------------------------------------------------------
  1058. */
  1059. static void rs_close(struct tty_struct *tty, struct file * filp)
  1060. {
  1061. struct serial_state *state = tty->driver_data;
  1062. struct tty_port *port = &state->tport;
  1063. if (tty_port_close_start(port, tty, filp) == 0)
  1064. return;
  1065. /*
  1066. * At this point we stop accepting input. To do this, we
  1067. * disable the receive line status interrupts, and tell the
  1068. * interrupt driver to stop checking the data ready bit in the
  1069. * line status register.
  1070. */
  1071. state->read_status_mask &= ~UART_LSR_DR;
  1072. if (tty_port_initialized(port)) {
  1073. /* disable receive interrupts */
  1074. amiga_custom.intena = IF_RBF;
  1075. mb();
  1076. /* clear any pending receive interrupt */
  1077. amiga_custom.intreq = IF_RBF;
  1078. mb();
  1079. /*
  1080. * Before we drop DTR, make sure the UART transmitter
  1081. * has completely drained; this is especially
  1082. * important if there is a transmit FIFO!
  1083. */
  1084. rs_wait_until_sent(tty, state->timeout);
  1085. }
  1086. shutdown(tty, state);
  1087. rs_flush_buffer(tty);
  1088. tty_ldisc_flush(tty);
  1089. port->tty = NULL;
  1090. tty_port_close_end(port, tty);
  1091. }
  1092. /*
  1093. * rs_wait_until_sent() --- wait until the transmitter is empty
  1094. */
  1095. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1096. {
  1097. struct serial_state *info = tty->driver_data;
  1098. unsigned long orig_jiffies, char_time;
  1099. int lsr;
  1100. orig_jiffies = jiffies;
  1101. /*
  1102. * Set the check interval to be 1/5 of the estimated time to
  1103. * send a single character, and make it at least 1. The check
  1104. * interval should also be less than the timeout.
  1105. *
  1106. * Note: we have to use pretty tight timings here to satisfy
  1107. * the NIST-PCTS.
  1108. */
  1109. char_time = (info->timeout - HZ/50) / XMIT_FIFO_SIZE;
  1110. char_time = char_time / 5;
  1111. if (char_time == 0)
  1112. char_time = 1;
  1113. if (timeout)
  1114. char_time = min_t(unsigned long, char_time, timeout);
  1115. /*
  1116. * If the transmitter hasn't cleared in twice the approximate
  1117. * amount of time to send the entire FIFO, it probably won't
  1118. * ever clear. This assumes the UART isn't doing flow
  1119. * control, which is currently the case. Hence, if it ever
  1120. * takes longer than info->timeout, this is probably due to a
  1121. * UART bug of some kind. So, we clamp the timeout parameter at
  1122. * 2*info->timeout.
  1123. */
  1124. if (!timeout || timeout > 2*info->timeout)
  1125. timeout = 2*info->timeout;
  1126. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1127. printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
  1128. printk("jiff=%lu...", jiffies);
  1129. #endif
  1130. while(!((lsr = amiga_custom.serdatr) & SDR_TSRE)) {
  1131. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1132. printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
  1133. #endif
  1134. msleep_interruptible(jiffies_to_msecs(char_time));
  1135. if (signal_pending(current))
  1136. break;
  1137. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1138. break;
  1139. }
  1140. __set_current_state(TASK_RUNNING);
  1141. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1142. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1143. #endif
  1144. }
  1145. /*
  1146. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1147. */
  1148. static void rs_hangup(struct tty_struct *tty)
  1149. {
  1150. struct serial_state *info = tty->driver_data;
  1151. rs_flush_buffer(tty);
  1152. shutdown(tty, info);
  1153. info->tport.count = 0;
  1154. tty_port_set_active(&info->tport, false);
  1155. info->tport.tty = NULL;
  1156. wake_up_interruptible(&info->tport.open_wait);
  1157. }
  1158. /*
  1159. * This routine is called whenever a serial port is opened. It
  1160. * enables interrupts for a serial port, linking in its async structure into
  1161. * the IRQ chain. It also performs the serial-specific
  1162. * initialization for the tty structure.
  1163. */
  1164. static int rs_open(struct tty_struct *tty, struct file * filp)
  1165. {
  1166. struct tty_port *port = tty->port;
  1167. struct serial_state *info = container_of(port, struct serial_state,
  1168. tport);
  1169. int retval;
  1170. port->count++;
  1171. port->tty = tty;
  1172. tty->driver_data = info;
  1173. retval = startup(tty, info);
  1174. if (retval) {
  1175. return retval;
  1176. }
  1177. return tty_port_block_til_ready(port, tty, filp);
  1178. }
  1179. /*
  1180. * /proc fs routines....
  1181. */
  1182. static inline void line_info(struct seq_file *m, int line,
  1183. struct serial_state *state)
  1184. {
  1185. char stat_buf[30], control, status;
  1186. unsigned long flags;
  1187. seq_printf(m, "%d: uart:amiga_builtin", line);
  1188. local_irq_save(flags);
  1189. status = ciab.pra;
  1190. control = tty_port_initialized(&state->tport) ? state->MCR : status;
  1191. local_irq_restore(flags);
  1192. stat_buf[0] = 0;
  1193. stat_buf[1] = 0;
  1194. if(!(control & SER_RTS))
  1195. strcat(stat_buf, "|RTS");
  1196. if(!(status & SER_CTS))
  1197. strcat(stat_buf, "|CTS");
  1198. if(!(control & SER_DTR))
  1199. strcat(stat_buf, "|DTR");
  1200. if(!(status & SER_DSR))
  1201. strcat(stat_buf, "|DSR");
  1202. if(!(status & SER_DCD))
  1203. strcat(stat_buf, "|CD");
  1204. if (state->quot)
  1205. seq_printf(m, " baud:%d", state->baud_base / state->quot);
  1206. seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
  1207. if (state->icount.frame)
  1208. seq_printf(m, " fe:%d", state->icount.frame);
  1209. if (state->icount.parity)
  1210. seq_printf(m, " pe:%d", state->icount.parity);
  1211. if (state->icount.brk)
  1212. seq_printf(m, " brk:%d", state->icount.brk);
  1213. if (state->icount.overrun)
  1214. seq_printf(m, " oe:%d", state->icount.overrun);
  1215. /*
  1216. * Last thing is the RS-232 status lines
  1217. */
  1218. seq_printf(m, " %s\n", stat_buf+1);
  1219. }
  1220. static int rs_proc_show(struct seq_file *m, void *v)
  1221. {
  1222. seq_printf(m, "serinfo:1.0 driver:4.30\n");
  1223. line_info(m, 0, &serial_state);
  1224. return 0;
  1225. }
  1226. /*
  1227. * ---------------------------------------------------------------------
  1228. * rs_init() and friends
  1229. *
  1230. * rs_init() is called at boot-time to initialize the serial driver.
  1231. * ---------------------------------------------------------------------
  1232. */
  1233. static const struct tty_operations serial_ops = {
  1234. .open = rs_open,
  1235. .close = rs_close,
  1236. .write = rs_write,
  1237. .put_char = rs_put_char,
  1238. .flush_chars = rs_flush_chars,
  1239. .write_room = rs_write_room,
  1240. .chars_in_buffer = rs_chars_in_buffer,
  1241. .flush_buffer = rs_flush_buffer,
  1242. .ioctl = rs_ioctl,
  1243. .throttle = rs_throttle,
  1244. .unthrottle = rs_unthrottle,
  1245. .set_termios = rs_set_termios,
  1246. .stop = rs_stop,
  1247. .start = rs_start,
  1248. .hangup = rs_hangup,
  1249. .break_ctl = rs_break,
  1250. .send_xchar = rs_send_xchar,
  1251. .wait_until_sent = rs_wait_until_sent,
  1252. .tiocmget = rs_tiocmget,
  1253. .tiocmset = rs_tiocmset,
  1254. .get_icount = rs_get_icount,
  1255. .set_serial = set_serial_info,
  1256. .get_serial = get_serial_info,
  1257. .proc_show = rs_proc_show,
  1258. };
  1259. static bool amiga_carrier_raised(struct tty_port *port)
  1260. {
  1261. return !(ciab.pra & SER_DCD);
  1262. }
  1263. static void amiga_dtr_rts(struct tty_port *port, bool active)
  1264. {
  1265. struct serial_state *info = container_of(port, struct serial_state,
  1266. tport);
  1267. unsigned long flags;
  1268. if (active)
  1269. info->MCR |= SER_DTR|SER_RTS;
  1270. else
  1271. info->MCR &= ~(SER_DTR|SER_RTS);
  1272. local_irq_save(flags);
  1273. rtsdtr_ctrl(info->MCR);
  1274. local_irq_restore(flags);
  1275. }
  1276. static const struct tty_port_operations amiga_port_ops = {
  1277. .carrier_raised = amiga_carrier_raised,
  1278. .dtr_rts = amiga_dtr_rts,
  1279. };
  1280. /*
  1281. * The serial driver boot-time initialization code!
  1282. */
  1283. static int __init amiga_serial_probe(struct platform_device *pdev)
  1284. {
  1285. struct serial_state *state = &serial_state;
  1286. struct tty_driver *driver;
  1287. unsigned long flags;
  1288. int error;
  1289. driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
  1290. if (IS_ERR(driver))
  1291. return PTR_ERR(driver);
  1292. /* Initialize the tty_driver structure */
  1293. driver->driver_name = "amiserial";
  1294. driver->name = "ttyS";
  1295. driver->major = TTY_MAJOR;
  1296. driver->minor_start = 64;
  1297. driver->type = TTY_DRIVER_TYPE_SERIAL;
  1298. driver->subtype = SERIAL_TYPE_NORMAL;
  1299. driver->init_termios = tty_std_termios;
  1300. driver->init_termios.c_cflag =
  1301. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1302. tty_set_operations(driver, &serial_ops);
  1303. memset(state, 0, sizeof(*state));
  1304. state->port = (int)&amiga_custom.serdatr; /* Just to give it a value */
  1305. tty_port_init(&state->tport);
  1306. state->tport.ops = &amiga_port_ops;
  1307. tty_port_link_device(&state->tport, driver, 0);
  1308. error = tty_register_driver(driver);
  1309. if (error)
  1310. goto fail_tty_driver_kref_put;
  1311. printk(KERN_INFO "ttyS0 is the amiga builtin serial port\n");
  1312. /* Hardware set up */
  1313. state->baud_base = amiga_colorclock;
  1314. /* set ISRs, and then disable the rx interrupts */
  1315. error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
  1316. if (error)
  1317. goto fail_unregister;
  1318. error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
  1319. "serial RX", state);
  1320. if (error)
  1321. goto fail_free_irq;
  1322. local_irq_save(flags);
  1323. /* turn off Rx and Tx interrupts */
  1324. amiga_custom.intena = IF_RBF | IF_TBE;
  1325. mb();
  1326. /* clear any pending interrupt */
  1327. amiga_custom.intreq = IF_RBF | IF_TBE;
  1328. mb();
  1329. local_irq_restore(flags);
  1330. /*
  1331. * set the appropriate directions for the modem control flags,
  1332. * and clear RTS and DTR
  1333. */
  1334. ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
  1335. ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
  1336. platform_set_drvdata(pdev, state);
  1337. serial_driver = driver;
  1338. return 0;
  1339. fail_free_irq:
  1340. free_irq(IRQ_AMIGA_TBE, state);
  1341. fail_unregister:
  1342. tty_unregister_driver(driver);
  1343. fail_tty_driver_kref_put:
  1344. tty_port_destroy(&state->tport);
  1345. tty_driver_kref_put(driver);
  1346. return error;
  1347. }
  1348. static void __exit amiga_serial_remove(struct platform_device *pdev)
  1349. {
  1350. struct serial_state *state = platform_get_drvdata(pdev);
  1351. tty_unregister_driver(serial_driver);
  1352. tty_driver_kref_put(serial_driver);
  1353. tty_port_destroy(&state->tport);
  1354. free_irq(IRQ_AMIGA_TBE, state);
  1355. free_irq(IRQ_AMIGA_RBF, state);
  1356. }
  1357. /*
  1358. * amiga_serial_remove() lives in .exit.text. For drivers registered via
  1359. * module_platform_driver_probe() this is ok because they cannot get unbound at
  1360. * runtime. So mark the driver struct with __refdata to prevent modpost
  1361. * triggering a section mismatch warning.
  1362. */
  1363. static struct platform_driver amiga_serial_driver __refdata = {
  1364. .remove_new = __exit_p(amiga_serial_remove),
  1365. .driver = {
  1366. .name = "amiga-serial",
  1367. },
  1368. };
  1369. module_platform_driver_probe(amiga_serial_driver, amiga_serial_probe);
  1370. #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
  1371. /*
  1372. * ------------------------------------------------------------
  1373. * Serial console driver
  1374. * ------------------------------------------------------------
  1375. */
  1376. static void amiga_serial_putc(char c)
  1377. {
  1378. amiga_custom.serdat = (unsigned char)c | 0x100;
  1379. while (!(amiga_custom.serdatr & 0x2000))
  1380. barrier();
  1381. }
  1382. /*
  1383. * Print a string to the serial port trying not to disturb
  1384. * any possible real use of the port...
  1385. *
  1386. * The console must be locked when we get here.
  1387. */
  1388. static void serial_console_write(struct console *co, const char *s,
  1389. unsigned count)
  1390. {
  1391. unsigned short intena = amiga_custom.intenar;
  1392. amiga_custom.intena = IF_TBE;
  1393. while (count--) {
  1394. if (*s == '\n')
  1395. amiga_serial_putc('\r');
  1396. amiga_serial_putc(*s++);
  1397. }
  1398. amiga_custom.intena = IF_SETCLR | (intena & IF_TBE);
  1399. }
  1400. static struct tty_driver *serial_console_device(struct console *c, int *index)
  1401. {
  1402. *index = 0;
  1403. return serial_driver;
  1404. }
  1405. static struct console sercons = {
  1406. .name = "ttyS",
  1407. .write = serial_console_write,
  1408. .device = serial_console_device,
  1409. .flags = CON_PRINTBUFFER,
  1410. .index = -1,
  1411. };
  1412. /*
  1413. * Register console.
  1414. */
  1415. static int __init amiserial_console_init(void)
  1416. {
  1417. if (!MACH_IS_AMIGA)
  1418. return -ENODEV;
  1419. register_console(&sercons);
  1420. return 0;
  1421. }
  1422. console_initcall(amiserial_console_init);
  1423. #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
  1424. MODULE_DESCRIPTION("Serial driver for the amiga builtin port");
  1425. MODULE_LICENSE("GPL");
  1426. MODULE_ALIAS("platform:amiga-serial");