amiserial.c 53 KB

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