ser_a2232.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /* drivers/char/ser_a2232.c */
  2. /* $Id: ser_a2232.c,v 1.1.1.1 2007/06/12 07:27:10 eyryu Exp $ */
  3. /* Linux serial driver for the Amiga A2232 board */
  4. /* This driver is MAINTAINED. Before applying any changes, please contact
  5. * the author.
  6. */
  7. /* Copyright (c) 2000-2001 Enver Haase <ehaase@inf.fu-berlin.de>
  8. * alias The A2232 driver project <A2232@gmx.net>
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. */
  26. /***************************** Documentation ************************/
  27. /*
  28. * This driver is in EXPERIMENTAL state. That means I could not find
  29. * someone with five A2232 boards with 35 ports running at 19200 bps
  30. * at the same time and test the machine's behaviour.
  31. * However, I know that you can performance-tweak this driver (see
  32. * the source code).
  33. * One thing to consider is the time this driver consumes during the
  34. * Amiga's vertical blank interrupt. Everything that is to be done
  35. * _IS DONE_ when entering the vertical blank interrupt handler of
  36. * this driver.
  37. * However, it would be more sane to only do the job for only ONE card
  38. * instead of ALL cards at a time; or, more generally, to handle only
  39. * SOME ports instead of ALL ports at a time.
  40. * However, as long as no-one runs into problems I guess I shouldn't
  41. * change the driver as it runs fine for me :) .
  42. *
  43. * Version history of this file:
  44. * 0.4 Resolved licensing issues.
  45. * 0.3 Inclusion in the Linux/m68k tree, small fixes.
  46. * 0.2 Added documentation, minor typo fixes.
  47. * 0.1 Initial release.
  48. *
  49. * TO DO:
  50. * - Handle incoming BREAK events. I guess "Stevens: Advanced
  51. * Programming in the UNIX(R) Environment" is a good reference
  52. * on what is to be done.
  53. * - When installing as a module, don't simply 'printk' text, but
  54. * send it to the TTY used by the user.
  55. *
  56. * THANKS TO:
  57. * - Jukka Marin (65EC02 code).
  58. * - The other NetBSD developers on whose A2232 driver I had a
  59. * pretty close look. However, I didn't copy any code so it
  60. * is okay to put my code under the GPL and include it into
  61. * Linux.
  62. */
  63. /***************************** End of Documentation *****************/
  64. /***************************** Defines ******************************/
  65. /*
  66. * Enables experimental 115200 (normal) 230400 (turbo) baud rate.
  67. * The A2232 specification states it can only operate at speeds up to
  68. * 19200 bits per second, and I was not able to send a file via
  69. * "sz"/"rz" and a null-modem cable from one A2232 port to another
  70. * at 115200 bits per second.
  71. * However, this might work for you.
  72. */
  73. #undef A2232_SPEEDHACK
  74. /*
  75. * Default is not to use RTS/CTS so you could be talked to death.
  76. */
  77. #define A2232_SUPPRESS_RTSCTS_WARNING
  78. /************************* End of Defines ***************************/
  79. /***************************** Includes *****************************/
  80. #include <linux/module.h>
  81. #include <linux/types.h>
  82. #include <linux/interrupt.h>
  83. #include <linux/kernel.h>
  84. #include <linux/errno.h>
  85. #include <linux/tty.h>
  86. #include <asm/setup.h>
  87. #include <asm/amigaints.h>
  88. #include <asm/amigahw.h>
  89. #include <linux/zorro.h>
  90. #include <asm/irq.h>
  91. #include <linux/mutex.h>
  92. #include <linux/delay.h>
  93. #include <linux/serial.h>
  94. #include <linux/generic_serial.h>
  95. #include <linux/tty_flip.h>
  96. #include "ser_a2232.h"
  97. #include "ser_a2232fw.h"
  98. /************************* End of Includes **************************/
  99. /***************************** Prototypes ***************************/
  100. /* The interrupt service routine */
  101. static irqreturn_t a2232_vbl_inter(int irq, void *data);
  102. /* Initialize the port structures */
  103. static void a2232_init_portstructs(void);
  104. /* Initialize and register TTY drivers. */
  105. /* returns 0 IFF successful */
  106. static int a2232_init_drivers(void);
  107. /* BEGIN GENERIC_SERIAL PROTOTYPES */
  108. static void a2232_disable_tx_interrupts(void *ptr);
  109. static void a2232_enable_tx_interrupts(void *ptr);
  110. static void a2232_disable_rx_interrupts(void *ptr);
  111. static void a2232_enable_rx_interrupts(void *ptr);
  112. static int a2232_get_CD(void *ptr);
  113. static void a2232_shutdown_port(void *ptr);
  114. static int a2232_set_real_termios(void *ptr);
  115. static int a2232_chars_in_buffer(void *ptr);
  116. static void a2232_close(void *ptr);
  117. static void a2232_hungup(void *ptr);
  118. /* static void a2232_getserial (void *ptr, struct serial_struct *sp); */
  119. /* END GENERIC_SERIAL PROTOTYPES */
  120. /* Functions that the TTY driver struct expects */
  121. static int a2232_ioctl(struct tty_struct *tty, struct file *file,
  122. unsigned int cmd, unsigned long arg);
  123. static void a2232_throttle(struct tty_struct *tty);
  124. static void a2232_unthrottle(struct tty_struct *tty);
  125. static int a2232_open(struct tty_struct * tty, struct file * filp);
  126. /************************* End of Prototypes ************************/
  127. /***************************** Global variables *********************/
  128. /*---------------------------------------------------------------------------
  129. * Interface from generic_serial.c back here
  130. *--------------------------------------------------------------------------*/
  131. static struct real_driver a2232_real_driver = {
  132. a2232_disable_tx_interrupts,
  133. a2232_enable_tx_interrupts,
  134. a2232_disable_rx_interrupts,
  135. a2232_enable_rx_interrupts,
  136. a2232_get_CD,
  137. a2232_shutdown_port,
  138. a2232_set_real_termios,
  139. a2232_chars_in_buffer,
  140. a2232_close,
  141. a2232_hungup,
  142. NULL /* a2232_getserial */
  143. };
  144. static void *a2232_driver_ID = &a2232_driver_ID; // Some memory address WE own.
  145. /* Ports structs */
  146. static struct a2232_port a2232_ports[MAX_A2232_BOARDS*NUMLINES];
  147. /* TTY driver structs */
  148. static struct tty_driver *a2232_driver;
  149. /* nr of cards completely (all ports) and correctly configured */
  150. static int nr_a2232;
  151. /* zorro_dev structs for the A2232's */
  152. static struct zorro_dev *zd_a2232[MAX_A2232_BOARDS];
  153. /***************************** End of Global variables **************/
  154. /* Helper functions */
  155. static inline volatile struct a2232memory *a2232mem(unsigned int board)
  156. {
  157. return (volatile struct a2232memory *)ZTWO_VADDR(zd_a2232[board]->resource.start);
  158. }
  159. static inline volatile struct a2232status *a2232stat(unsigned int board,
  160. unsigned int portonboard)
  161. {
  162. volatile struct a2232memory *mem = a2232mem(board);
  163. return &(mem->Status[portonboard]);
  164. }
  165. static inline void a2232_receive_char(struct a2232_port *port, int ch, int err)
  166. {
  167. /* Mostly stolen from other drivers.
  168. Maybe one could implement a more efficient version by not only
  169. transferring one character at a time.
  170. */
  171. struct tty_struct *tty = port->gs.tty;
  172. #if 0
  173. switch(err) {
  174. case TTY_BREAK:
  175. break;
  176. case TTY_PARITY:
  177. break;
  178. case TTY_OVERRUN:
  179. break;
  180. case TTY_FRAME:
  181. break;
  182. }
  183. #endif
  184. tty_insert_flip_char(tty, ch, err);
  185. tty_flip_buffer_push(tty);
  186. }
  187. /***************************** Functions ****************************/
  188. /*** BEGIN OF REAL_DRIVER FUNCTIONS ***/
  189. static void a2232_disable_tx_interrupts(void *ptr)
  190. {
  191. struct a2232_port *port;
  192. volatile struct a2232status *stat;
  193. unsigned long flags;
  194. port = ptr;
  195. stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
  196. stat->OutDisable = -1;
  197. /* Does this here really have to be? */
  198. local_irq_save(flags);
  199. port->gs.flags &= ~GS_TX_INTEN;
  200. local_irq_restore(flags);
  201. }
  202. static void a2232_enable_tx_interrupts(void *ptr)
  203. {
  204. struct a2232_port *port;
  205. volatile struct a2232status *stat;
  206. unsigned long flags;
  207. port = ptr;
  208. stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
  209. stat->OutDisable = 0;
  210. /* Does this here really have to be? */
  211. local_irq_save(flags);
  212. port->gs.flags |= GS_TX_INTEN;
  213. local_irq_restore(flags);
  214. }
  215. static void a2232_disable_rx_interrupts(void *ptr)
  216. {
  217. struct a2232_port *port;
  218. port = ptr;
  219. port->disable_rx = -1;
  220. }
  221. static void a2232_enable_rx_interrupts(void *ptr)
  222. {
  223. struct a2232_port *port;
  224. port = ptr;
  225. port->disable_rx = 0;
  226. }
  227. static int a2232_get_CD(void *ptr)
  228. {
  229. return ((struct a2232_port *) ptr)->cd_status;
  230. }
  231. static void a2232_shutdown_port(void *ptr)
  232. {
  233. struct a2232_port *port;
  234. volatile struct a2232status *stat;
  235. unsigned long flags;
  236. port = ptr;
  237. stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
  238. local_irq_save(flags);
  239. port->gs.flags &= ~GS_ACTIVE;
  240. if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL) {
  241. /* Set DTR and RTS to Low, flush output.
  242. The NetBSD driver "msc.c" does it this way. */
  243. stat->Command = ( (stat->Command & ~A2232CMD_CMask) |
  244. A2232CMD_Close );
  245. stat->OutFlush = -1;
  246. stat->Setup = -1;
  247. }
  248. local_irq_restore(flags);
  249. /* After analyzing control flow, I think a2232_shutdown_port
  250. is actually the last call from the system when at application
  251. level someone issues a "echo Hello >>/dev/ttyY0".
  252. Therefore I think the MOD_DEC_USE_COUNT should be here and
  253. not in "a2232_close()". See the comment in "sx.c", too.
  254. If you run into problems, compile this driver into the
  255. kernel instead of compiling it as a module. */
  256. }
  257. static int a2232_set_real_termios(void *ptr)
  258. {
  259. unsigned int cflag, baud, chsize, stopb, parity, softflow;
  260. int rate;
  261. int a2232_param, a2232_cmd;
  262. unsigned long flags;
  263. unsigned int i;
  264. struct a2232_port *port = ptr;
  265. volatile struct a2232status *status;
  266. volatile struct a2232memory *mem;
  267. if (!port->gs.tty || !port->gs.tty->termios) return 0;
  268. status = a2232stat(port->which_a2232, port->which_port_on_a2232);
  269. mem = a2232mem(port->which_a2232);
  270. a2232_param = a2232_cmd = 0;
  271. // get baud rate
  272. baud = port->gs.baud;
  273. if (baud == 0) {
  274. /* speed == 0 -> drop DTR, do nothing else */
  275. local_irq_save(flags);
  276. // Clear DTR (and RTS... mhhh).
  277. status->Command = ( (status->Command & ~A2232CMD_CMask) |
  278. A2232CMD_Close );
  279. status->OutFlush = -1;
  280. status->Setup = -1;
  281. local_irq_restore(flags);
  282. return 0;
  283. }
  284. rate = A2232_BAUD_TABLE_NOAVAIL;
  285. for (i=0; i < A2232_BAUD_TABLE_NUM_RATES * 3; i += 3){
  286. if (a2232_baud_table[i] == baud){
  287. if (mem->Common.Crystal == A2232_TURBO) rate = a2232_baud_table[i+2];
  288. else rate = a2232_baud_table[i+1];
  289. }
  290. }
  291. if (rate == A2232_BAUD_TABLE_NOAVAIL){
  292. printk("a2232: Board %d Port %d unsupported baud rate: %d baud. Using another.\n",port->which_a2232,port->which_port_on_a2232,baud);
  293. // This is useful for both (turbo or normal) Crystal versions.
  294. rate = A2232PARAM_B9600;
  295. }
  296. a2232_param |= rate;
  297. cflag = port->gs.tty->termios->c_cflag;
  298. // get character size
  299. chsize = cflag & CSIZE;
  300. switch (chsize){
  301. case CS8: a2232_param |= A2232PARAM_8Bit; break;
  302. case CS7: a2232_param |= A2232PARAM_7Bit; break;
  303. case CS6: a2232_param |= A2232PARAM_6Bit; break;
  304. case CS5: a2232_param |= A2232PARAM_5Bit; break;
  305. default: printk("a2232: Board %d Port %d unsupported character size: %d. Using 8 data bits.\n",
  306. port->which_a2232,port->which_port_on_a2232,chsize);
  307. a2232_param |= A2232PARAM_8Bit; break;
  308. }
  309. // get number of stop bits
  310. stopb = cflag & CSTOPB;
  311. if (stopb){ // two stop bits instead of one
  312. printk("a2232: Board %d Port %d 2 stop bits unsupported. Using 1 stop bit.\n",
  313. port->which_a2232,port->which_port_on_a2232);
  314. }
  315. // Warn if RTS/CTS not wanted
  316. if (!(cflag & CRTSCTS)){
  317. #ifndef A2232_SUPPRESS_RTSCTS_WARNING
  318. printk("a2232: Board %d Port %d cannot switch off firmware-implemented RTS/CTS hardware flow control.\n",
  319. port->which_a2232,port->which_port_on_a2232);
  320. #endif
  321. }
  322. /* I think this is correct.
  323. However, IXOFF means _input_ flow control and I wonder
  324. if one should care about IXON _output_ flow control,
  325. too. If this makes problems, one should turn the A2232
  326. firmware XON/XOFF "SoftFlow" flow control off and use
  327. the conventional way of inserting START/STOP characters
  328. by hand in throttle()/unthrottle().
  329. */
  330. softflow = !!( port->gs.tty->termios->c_iflag & IXOFF );
  331. // get Parity (Enabled/Disabled? If Enabled, Odd or Even?)
  332. parity = cflag & (PARENB | PARODD);
  333. if (parity & PARENB){
  334. if (parity & PARODD){
  335. a2232_cmd |= A2232CMD_OddParity;
  336. }
  337. else{
  338. a2232_cmd |= A2232CMD_EvenParity;
  339. }
  340. }
  341. else a2232_cmd |= A2232CMD_NoParity;
  342. /* Hmm. Maybe an own a2232_port structure
  343. member would be cleaner? */
  344. if (cflag & CLOCAL)
  345. port->gs.flags &= ~ASYNC_CHECK_CD;
  346. else
  347. port->gs.flags |= ASYNC_CHECK_CD;
  348. /* Now we have all parameters and can go to set them: */
  349. local_irq_save(flags);
  350. status->Param = a2232_param | A2232PARAM_RcvBaud;
  351. status->Command = a2232_cmd | A2232CMD_Open | A2232CMD_Enable;
  352. status->SoftFlow = softflow;
  353. status->OutDisable = 0;
  354. status->Setup = -1;
  355. local_irq_restore(flags);
  356. return 0;
  357. }
  358. static int a2232_chars_in_buffer(void *ptr)
  359. {
  360. struct a2232_port *port;
  361. volatile struct a2232status *status;
  362. unsigned char ret; /* we need modulo-256 arithmetics */
  363. port = ptr;
  364. status = a2232stat(port->which_a2232, port->which_port_on_a2232);
  365. #if A2232_IOBUFLEN != 256
  366. #error "Re-Implement a2232_chars_in_buffer()!"
  367. #endif
  368. ret = (status->OutHead - status->OutTail);
  369. return ret;
  370. }
  371. static void a2232_close(void *ptr)
  372. {
  373. a2232_disable_tx_interrupts(ptr);
  374. a2232_disable_rx_interrupts(ptr);
  375. /* see the comment in a2232_shutdown_port above. */
  376. }
  377. static void a2232_hungup(void *ptr)
  378. {
  379. a2232_close(ptr);
  380. }
  381. /*** END OF REAL_DRIVER FUNCTIONS ***/
  382. /*** BEGIN FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
  383. static int a2232_ioctl( struct tty_struct *tty, struct file *file,
  384. unsigned int cmd, unsigned long arg)
  385. {
  386. return -ENOIOCTLCMD;
  387. }
  388. static void a2232_throttle(struct tty_struct *tty)
  389. {
  390. /* Throttle: System cannot take another chars: Drop RTS or
  391. send the STOP char or whatever.
  392. The A2232 firmware does RTS/CTS anyway, and XON/XOFF
  393. if switched on. So the only thing we can do at this
  394. layer here is not taking any characters out of the
  395. A2232 buffer any more. */
  396. struct a2232_port *port = (struct a2232_port *) tty->driver_data;
  397. port->throttle_input = -1;
  398. }
  399. static void a2232_unthrottle(struct tty_struct *tty)
  400. {
  401. /* Unthrottle: dual to "throttle()" above. */
  402. struct a2232_port *port = (struct a2232_port *) tty->driver_data;
  403. port->throttle_input = 0;
  404. }
  405. static int a2232_open(struct tty_struct * tty, struct file * filp)
  406. {
  407. /* More or less stolen from other drivers. */
  408. int line;
  409. int retval;
  410. struct a2232_port *port;
  411. line = tty->index;
  412. port = &a2232_ports[line];
  413. tty->driver_data = port;
  414. port->gs.tty = tty;
  415. port->gs.count++;
  416. retval = gs_init_port(&port->gs);
  417. if (retval) {
  418. port->gs.count--;
  419. return retval;
  420. }
  421. port->gs.flags |= GS_ACTIVE;
  422. retval = gs_block_til_ready(port, filp);
  423. if (retval) {
  424. port->gs.count--;
  425. return retval;
  426. }
  427. a2232_enable_rx_interrupts(port);
  428. return 0;
  429. }
  430. /*** END OF FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
  431. static irqreturn_t a2232_vbl_inter(int irq, void *data)
  432. {
  433. #if A2232_IOBUFLEN != 256
  434. #error "Re-Implement a2232_vbl_inter()!"
  435. #endif
  436. struct a2232_port *port;
  437. volatile struct a2232memory *mem;
  438. volatile struct a2232status *status;
  439. unsigned char newhead;
  440. unsigned char bufpos; /* Must be unsigned char. We need the modulo-256 arithmetics */
  441. unsigned char ncd, ocd, ccd; /* names consistent with the NetBSD driver */
  442. volatile u_char *ibuf, *cbuf, *obuf;
  443. int ch, err, n, p;
  444. for (n = 0; n < nr_a2232; n++){ /* for every completely initialized A2232 board */
  445. mem = a2232mem(n);
  446. for (p = 0; p < NUMLINES; p++){ /* for every port on this board */
  447. err = 0;
  448. port = &a2232_ports[n*NUMLINES+p];
  449. if ( port->gs.flags & GS_ACTIVE ){ /* if the port is used */
  450. status = a2232stat(n,p);
  451. if (!port->disable_rx && !port->throttle_input){ /* If input is not disabled */
  452. newhead = status->InHead; /* 65EC02 write pointer */
  453. bufpos = status->InTail;
  454. /* check for input for this port */
  455. if (newhead != bufpos) {
  456. /* buffer for input chars/events */
  457. ibuf = mem->InBuf[p];
  458. /* data types of bytes in ibuf */
  459. cbuf = mem->InCtl[p];
  460. /* do for all chars */
  461. while (bufpos != newhead) {
  462. /* which type of input data? */
  463. switch (cbuf[bufpos]) {
  464. /* switch on input event (CD, BREAK, etc.) */
  465. case A2232INCTL_EVENT:
  466. switch (ibuf[bufpos++]) {
  467. case A2232EVENT_Break:
  468. /* TODO: Handle BREAK signal */
  469. break;
  470. /* A2232EVENT_CarrierOn and A2232EVENT_CarrierOff are
  471. handled in a separate queue and should not occur here. */
  472. case A2232EVENT_Sync:
  473. printk("A2232: 65EC02 software sent SYNC event, don't know what to do. Ignoring.");
  474. break;
  475. default:
  476. printk("A2232: 65EC02 software broken, unknown event type %d occurred.\n",ibuf[bufpos-1]);
  477. } /* event type switch */
  478. break;
  479. case A2232INCTL_CHAR:
  480. /* Receive incoming char */
  481. a2232_receive_char(port, ibuf[bufpos], err);
  482. bufpos++;
  483. break;
  484. default:
  485. printk("A2232: 65EC02 software broken, unknown data type %d occurred.\n",cbuf[bufpos]);
  486. bufpos++;
  487. } /* switch on input data type */
  488. } /* while there's something in the buffer */
  489. status->InTail = bufpos; /* tell 65EC02 what we've read */
  490. } /* if there was something in the buffer */
  491. } /* If input is not disabled */
  492. /* Now check if there's something to output */
  493. obuf = mem->OutBuf[p];
  494. bufpos = status->OutHead;
  495. while ( (port->gs.xmit_cnt > 0) &&
  496. (!port->gs.tty->stopped) &&
  497. (!port->gs.tty->hw_stopped) ){ /* While there are chars to transmit */
  498. if (((bufpos+1) & A2232_IOBUFLENMASK) != status->OutTail) { /* If the A2232 buffer is not full */
  499. ch = port->gs.xmit_buf[port->gs.xmit_tail]; /* get the next char to transmit */
  500. port->gs.xmit_tail = (port->gs.xmit_tail+1) & (SERIAL_XMIT_SIZE-1); /* modulo-addition for the gs.xmit_buf ring-buffer */
  501. obuf[bufpos++] = ch; /* put it into the A2232 buffer */
  502. port->gs.xmit_cnt--;
  503. }
  504. else{ /* If A2232 the buffer is full */
  505. break; /* simply stop filling it. */
  506. }
  507. }
  508. status->OutHead = bufpos;
  509. /* WakeUp if output buffer runs low */
  510. if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.tty) {
  511. tty_wakeup(port->gs.tty);
  512. }
  513. } // if the port is used
  514. } // for every port on the board
  515. /* Now check the CD message queue */
  516. newhead = mem->Common.CDHead;
  517. bufpos = mem->Common.CDTail;
  518. if (newhead != bufpos){ /* There are CD events in queue */
  519. ocd = mem->Common.CDStatus; /* get old status bits */
  520. while (newhead != bufpos){ /* read all events */
  521. ncd = mem->CDBuf[bufpos++]; /* get one event */
  522. ccd = ncd ^ ocd; /* mask of changed lines */
  523. ocd = ncd; /* save new status bits */
  524. for(p=0; p < NUMLINES; p++){ /* for all ports */
  525. if (ccd & 1){ /* this one changed */
  526. struct a2232_port *port = &a2232_ports[n*7+p];
  527. port->cd_status = !(ncd & 1); /* ncd&1 <=> CD is now off */
  528. if (!(port->gs.flags & ASYNC_CHECK_CD))
  529. ; /* Don't report DCD changes */
  530. else if (port->cd_status) { // if DCD on: DCD went UP!
  531. /* Are we blocking in open?*/
  532. wake_up_interruptible(&port->gs.open_wait);
  533. }
  534. else { // if DCD off: DCD went DOWN!
  535. if (port->gs.tty)
  536. tty_hangup (port->gs.tty);
  537. }
  538. } // if CD changed for this port
  539. ccd >>= 1;
  540. ncd >>= 1; /* Shift bits for next line */
  541. } // for every port
  542. } // while CD events in queue
  543. mem->Common.CDStatus = ocd; /* save new status */
  544. mem->Common.CDTail = bufpos; /* remove events */
  545. } // if events in CD queue
  546. } // for every completely initialized A2232 board
  547. return IRQ_HANDLED;
  548. }
  549. static void a2232_init_portstructs(void)
  550. {
  551. struct a2232_port *port;
  552. int i;
  553. for (i = 0; i < MAX_A2232_BOARDS*NUMLINES; i++) {
  554. port = a2232_ports + i;
  555. port->which_a2232 = i/NUMLINES;
  556. port->which_port_on_a2232 = i%NUMLINES;
  557. port->disable_rx = port->throttle_input = port->cd_status = 0;
  558. port->gs.magic = A2232_MAGIC;
  559. port->gs.close_delay = HZ/2;
  560. port->gs.closing_wait = 30 * HZ;
  561. port->gs.rd = &a2232_real_driver;
  562. #ifdef NEW_WRITE_LOCKING
  563. init_MUTEX(&(port->gs.port_write_mutex));
  564. #endif
  565. init_waitqueue_head(&port->gs.open_wait);
  566. init_waitqueue_head(&port->gs.close_wait);
  567. }
  568. }
  569. static const struct tty_operations a2232_ops = {
  570. .open = a2232_open,
  571. .close = gs_close,
  572. .write = gs_write,
  573. .put_char = gs_put_char,
  574. .flush_chars = gs_flush_chars,
  575. .write_room = gs_write_room,
  576. .chars_in_buffer = gs_chars_in_buffer,
  577. .flush_buffer = gs_flush_buffer,
  578. .ioctl = a2232_ioctl,
  579. .throttle = a2232_throttle,
  580. .unthrottle = a2232_unthrottle,
  581. .set_termios = gs_set_termios,
  582. .stop = gs_stop,
  583. .start = gs_start,
  584. .hangup = gs_hangup,
  585. };
  586. static int a2232_init_drivers(void)
  587. {
  588. int error;
  589. a2232_driver = alloc_tty_driver(NUMLINES * nr_a2232);
  590. if (!a2232_driver)
  591. return -ENOMEM;
  592. a2232_driver->owner = THIS_MODULE;
  593. a2232_driver->driver_name = "commodore_a2232";
  594. a2232_driver->name = "ttyY";
  595. a2232_driver->major = A2232_NORMAL_MAJOR;
  596. a2232_driver->type = TTY_DRIVER_TYPE_SERIAL;
  597. a2232_driver->subtype = SERIAL_TYPE_NORMAL;
  598. a2232_driver->init_termios = tty_std_termios;
  599. a2232_driver->init_termios.c_cflag =
  600. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  601. a2232_driver->init_termios.c_ispeed = 9600;
  602. a2232_driver->init_termios.c_ospeed = 9600;
  603. a2232_driver->flags = TTY_DRIVER_REAL_RAW;
  604. tty_set_operations(a2232_driver, &a2232_ops);
  605. if ((error = tty_register_driver(a2232_driver))) {
  606. printk(KERN_ERR "A2232: Couldn't register A2232 driver, error = %d\n",
  607. error);
  608. put_tty_driver(a2232_driver);
  609. return 1;
  610. }
  611. return 0;
  612. }
  613. static int __init a2232board_init(void)
  614. {
  615. struct zorro_dev *z;
  616. unsigned int boardaddr;
  617. int bcount;
  618. short start;
  619. u_char *from;
  620. volatile u_char *to;
  621. volatile struct a2232memory *mem;
  622. #ifdef CONFIG_SMP
  623. return -ENODEV; /* This driver is not SMP aware. Is there an SMP ZorroII-bus-machine? */
  624. #endif
  625. if (!MACH_IS_AMIGA){
  626. return -ENODEV;
  627. }
  628. printk("Commodore A2232 driver initializing.\n"); /* Say that we're alive. */
  629. z = NULL;
  630. nr_a2232 = 0;
  631. while ( (z = zorro_find_device(ZORRO_WILDCARD, z)) ){
  632. if ( (z->id != ZORRO_PROD_CBM_A2232_PROTOTYPE) &&
  633. (z->id != ZORRO_PROD_CBM_A2232) ){
  634. continue; // The board found was no A2232
  635. }
  636. if (!zorro_request_device(z,"A2232 driver"))
  637. continue;
  638. printk("Commodore A2232 found (#%d).\n",nr_a2232);
  639. zd_a2232[nr_a2232] = z;
  640. boardaddr = ZTWO_VADDR( z->resource.start );
  641. printk("Board is located at address 0x%x, size is 0x%x.\n", boardaddr, (unsigned int) ((z->resource.end+1) - (z->resource.start)));
  642. mem = (volatile struct a2232memory *) boardaddr;
  643. (void) mem->Enable6502Reset; /* copy the code across to the board */
  644. to = (u_char *)mem; from = a2232_65EC02code; bcount = sizeof(a2232_65EC02code) - 2;
  645. start = *(short *)from;
  646. from += sizeof(start);
  647. to += start;
  648. while(bcount--) *to++ = *from++;
  649. printk("65EC02 software uploaded to the A2232 memory.\n");
  650. mem->Common.Crystal = A2232_UNKNOWN; /* use automatic speed check */
  651. /* start 6502 running */
  652. (void) mem->ResetBoard;
  653. printk("A2232's 65EC02 CPU up and running.\n");
  654. /* wait until speed detector has finished */
  655. for (bcount = 0; bcount < 2000; bcount++) {
  656. udelay(1000);
  657. if (mem->Common.Crystal)
  658. break;
  659. }
  660. printk((mem->Common.Crystal?"A2232 oscillator crystal detected by 65EC02 software: ":"65EC02 software could not determine A2232 oscillator crystal: "));
  661. switch (mem->Common.Crystal){
  662. case A2232_UNKNOWN:
  663. printk("Unknown crystal.\n");
  664. break;
  665. case A2232_NORMAL:
  666. printk ("Normal crystal.\n");
  667. break;
  668. case A2232_TURBO:
  669. printk ("Turbo crystal.\n");
  670. break;
  671. default:
  672. printk ("0x%x. Huh?\n",mem->Common.Crystal);
  673. }
  674. nr_a2232++;
  675. }
  676. printk("Total: %d A2232 boards initialized.\n", nr_a2232); /* Some status report if no card was found */
  677. a2232_init_portstructs();
  678. /*
  679. a2232_init_drivers also registers the drivers. Must be here because all boards
  680. have to be detected first.
  681. */
  682. if (a2232_init_drivers()) return -ENODEV; // maybe we should use a different -Exxx?
  683. request_irq(IRQ_AMIGA_VERTB, a2232_vbl_inter, 0, "A2232 serial VBL", a2232_driver_ID);
  684. return 0;
  685. }
  686. static void __exit a2232board_exit(void)
  687. {
  688. int i;
  689. for (i = 0; i < nr_a2232; i++) {
  690. zorro_release_device(zd_a2232[i]);
  691. }
  692. tty_unregister_driver(a2232_driver);
  693. put_tty_driver(a2232_driver);
  694. free_irq(IRQ_AMIGA_VERTB, a2232_driver_ID);
  695. }
  696. module_init(a2232board_init);
  697. module_exit(a2232board_exit);
  698. MODULE_AUTHOR("Enver Haase");
  699. MODULE_DESCRIPTION("Amiga A2232 multi-serial board driver");
  700. MODULE_LICENSE("GPL");