amiserial.c 53 KB

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