amiserial.c 53 KB

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