amiserial.c 47 KB

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