amiserial.c 46 KB

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