con3215.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /*
  2. * 3215 line mode terminal driver.
  3. *
  4. * Copyright IBM Corp. 1999, 2009
  5. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. *
  7. * Updated:
  8. * Aug-2000: Added tab support
  9. * Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/vt_kern.h>
  17. #include <linux/init.h>
  18. #include <linux/console.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/err.h>
  21. #include <linux/reboot.h>
  22. #include <linux/serial.h> /* ASYNC_* flags */
  23. #include <linux/slab.h>
  24. #include <asm/ccwdev.h>
  25. #include <asm/cio.h>
  26. #include <asm/io.h>
  27. #include <asm/ebcdic.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/delay.h>
  30. #include <asm/cpcmd.h>
  31. #include <asm/setup.h>
  32. #include "ctrlchar.h"
  33. #define NR_3215 1
  34. #define NR_3215_REQ (4*NR_3215)
  35. #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
  36. #define RAW3215_INBUF_SIZE 256 /* input buffer size */
  37. #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
  38. #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
  39. #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
  40. #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
  41. #define RAW3215_NR_CCWS 3
  42. #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
  43. #define RAW3215_WORKING 4 /* set if a request is being worked on */
  44. #define RAW3215_THROTTLED 8 /* set if reading is disabled */
  45. #define RAW3215_STOPPED 16 /* set if writing is disabled */
  46. #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
  47. #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
  48. #define TAB_STOP_SIZE 8 /* tab stop size */
  49. /*
  50. * Request types for a 3215 device
  51. */
  52. enum raw3215_type {
  53. RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
  54. };
  55. /*
  56. * Request structure for a 3215 device
  57. */
  58. struct raw3215_req {
  59. enum raw3215_type type; /* type of the request */
  60. int start, len; /* start index & len in output buffer */
  61. int delayable; /* indication to wait for more data */
  62. int residual; /* residual count for read request */
  63. struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
  64. struct raw3215_info *info; /* pointer to main structure */
  65. struct raw3215_req *next; /* pointer to next request */
  66. } __attribute__ ((aligned(8)));
  67. struct raw3215_info {
  68. struct tty_port port;
  69. struct ccw_device *cdev; /* device for tty driver */
  70. spinlock_t *lock; /* pointer to irq lock */
  71. int flags; /* state flags */
  72. char *buffer; /* pointer to output buffer */
  73. char *inbuf; /* pointer to input buffer */
  74. int head; /* first free byte in output buffer */
  75. int count; /* number of bytes in output buffer */
  76. int written; /* number of bytes in write requests */
  77. struct raw3215_req *queued_read; /* pointer to queued read requests */
  78. struct raw3215_req *queued_write;/* pointer to queued write requests */
  79. struct tasklet_struct tlet; /* tasklet to invoke tty_wakeup */
  80. wait_queue_head_t empty_wait; /* wait queue for flushing */
  81. struct timer_list timer; /* timer for delayed output */
  82. int line_pos; /* position on the line (for tabs) */
  83. char ubuffer[80]; /* copy_from_user buffer */
  84. };
  85. /* array of 3215 devices structures */
  86. static struct raw3215_info *raw3215[NR_3215];
  87. /* spinlock to protect the raw3215 array */
  88. static DEFINE_SPINLOCK(raw3215_device_lock);
  89. /* list of free request structures */
  90. static struct raw3215_req *raw3215_freelist;
  91. /* spinlock to protect free list */
  92. static spinlock_t raw3215_freelist_lock;
  93. static struct tty_driver *tty3215_driver;
  94. /*
  95. * Get a request structure from the free list
  96. */
  97. static inline struct raw3215_req *raw3215_alloc_req(void)
  98. {
  99. struct raw3215_req *req;
  100. unsigned long flags;
  101. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  102. req = raw3215_freelist;
  103. raw3215_freelist = req->next;
  104. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  105. return req;
  106. }
  107. /*
  108. * Put a request structure back to the free list
  109. */
  110. static inline void raw3215_free_req(struct raw3215_req *req)
  111. {
  112. unsigned long flags;
  113. if (req->type == RAW3215_FREE)
  114. return; /* don't free a free request */
  115. req->type = RAW3215_FREE;
  116. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  117. req->next = raw3215_freelist;
  118. raw3215_freelist = req;
  119. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  120. }
  121. /*
  122. * Set up a read request that reads up to 160 byte from the 3215 device.
  123. * If there is a queued read request it is used, but that shouldn't happen
  124. * because a 3215 terminal won't accept a new read before the old one is
  125. * completed.
  126. */
  127. static void raw3215_mk_read_req(struct raw3215_info *raw)
  128. {
  129. struct raw3215_req *req;
  130. struct ccw1 *ccw;
  131. /* there can only be ONE read request at a time */
  132. req = raw->queued_read;
  133. if (req == NULL) {
  134. /* no queued read request, use new req structure */
  135. req = raw3215_alloc_req();
  136. req->type = RAW3215_READ;
  137. req->info = raw;
  138. raw->queued_read = req;
  139. }
  140. ccw = req->ccws;
  141. ccw->cmd_code = 0x0A; /* read inquiry */
  142. ccw->flags = 0x20; /* ignore incorrect length */
  143. ccw->count = 160;
  144. ccw->cda = (__u32) __pa(raw->inbuf);
  145. }
  146. /*
  147. * Set up a write request with the information from the main structure.
  148. * A ccw chain is created that writes as much as possible from the output
  149. * buffer to the 3215 device. If a queued write exists it is replaced by
  150. * the new, probably lengthened request.
  151. */
  152. static void raw3215_mk_write_req(struct raw3215_info *raw)
  153. {
  154. struct raw3215_req *req;
  155. struct ccw1 *ccw;
  156. int len, count, ix, lines;
  157. if (raw->count <= raw->written)
  158. return;
  159. /* check if there is a queued write request */
  160. req = raw->queued_write;
  161. if (req == NULL) {
  162. /* no queued write request, use new req structure */
  163. req = raw3215_alloc_req();
  164. req->type = RAW3215_WRITE;
  165. req->info = raw;
  166. raw->queued_write = req;
  167. } else {
  168. raw->written -= req->len;
  169. }
  170. ccw = req->ccws;
  171. req->start = (raw->head - raw->count + raw->written) &
  172. (RAW3215_BUFFER_SIZE - 1);
  173. /*
  174. * now we have to count newlines. We can at max accept
  175. * RAW3215_MAX_NEWLINE newlines in a single ssch due to
  176. * a restriction in VM
  177. */
  178. lines = 0;
  179. ix = req->start;
  180. while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
  181. if (raw->buffer[ix] == 0x15)
  182. lines++;
  183. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  184. }
  185. len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
  186. if (len > RAW3215_MAX_BYTES)
  187. len = RAW3215_MAX_BYTES;
  188. req->len = len;
  189. raw->written += len;
  190. /* set the indication if we should try to enlarge this request */
  191. req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
  192. ix = req->start;
  193. while (len > 0) {
  194. if (ccw > req->ccws)
  195. ccw[-1].flags |= 0x40; /* use command chaining */
  196. ccw->cmd_code = 0x01; /* write, auto carrier return */
  197. ccw->flags = 0x20; /* ignore incorrect length ind. */
  198. ccw->cda =
  199. (__u32) __pa(raw->buffer + ix);
  200. count = len;
  201. if (ix + count > RAW3215_BUFFER_SIZE)
  202. count = RAW3215_BUFFER_SIZE - ix;
  203. ccw->count = count;
  204. len -= count;
  205. ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
  206. ccw++;
  207. }
  208. /*
  209. * Add a NOP to the channel program. 3215 devices are purely
  210. * emulated and its much better to avoid the channel end
  211. * interrupt in this case.
  212. */
  213. if (ccw > req->ccws)
  214. ccw[-1].flags |= 0x40; /* use command chaining */
  215. ccw->cmd_code = 0x03; /* NOP */
  216. ccw->flags = 0;
  217. ccw->cda = 0;
  218. ccw->count = 1;
  219. }
  220. /*
  221. * Start a read or a write request
  222. */
  223. static void raw3215_start_io(struct raw3215_info *raw)
  224. {
  225. struct raw3215_req *req;
  226. int res;
  227. req = raw->queued_read;
  228. if (req != NULL &&
  229. !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
  230. /* dequeue request */
  231. raw->queued_read = NULL;
  232. res = ccw_device_start(raw->cdev, req->ccws,
  233. (unsigned long) req, 0, 0);
  234. if (res != 0) {
  235. /* do_IO failed, put request back to queue */
  236. raw->queued_read = req;
  237. } else {
  238. raw->flags |= RAW3215_WORKING;
  239. }
  240. }
  241. req = raw->queued_write;
  242. if (req != NULL &&
  243. !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
  244. /* dequeue request */
  245. raw->queued_write = NULL;
  246. res = ccw_device_start(raw->cdev, req->ccws,
  247. (unsigned long) req, 0, 0);
  248. if (res != 0) {
  249. /* do_IO failed, put request back to queue */
  250. raw->queued_write = req;
  251. } else {
  252. raw->flags |= RAW3215_WORKING;
  253. }
  254. }
  255. }
  256. /*
  257. * Function to start a delayed output after RAW3215_TIMEOUT seconds
  258. */
  259. static void raw3215_timeout(unsigned long __data)
  260. {
  261. struct raw3215_info *raw = (struct raw3215_info *) __data;
  262. unsigned long flags;
  263. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  264. if (raw->flags & RAW3215_TIMER_RUNS) {
  265. del_timer(&raw->timer);
  266. raw->flags &= ~RAW3215_TIMER_RUNS;
  267. if (!(raw->port.flags & ASYNC_SUSPENDED)) {
  268. raw3215_mk_write_req(raw);
  269. raw3215_start_io(raw);
  270. }
  271. }
  272. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  273. }
  274. /*
  275. * Function to conditionally start an IO. A read is started immediately,
  276. * a write is only started immediately if the flush flag is on or the
  277. * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
  278. * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
  279. */
  280. static inline void raw3215_try_io(struct raw3215_info *raw)
  281. {
  282. if (!(raw->port.flags & ASYNC_INITIALIZED) ||
  283. (raw->port.flags & ASYNC_SUSPENDED))
  284. return;
  285. if (raw->queued_read != NULL)
  286. raw3215_start_io(raw);
  287. else if (raw->queued_write != NULL) {
  288. if ((raw->queued_write->delayable == 0) ||
  289. (raw->flags & RAW3215_FLUSHING)) {
  290. /* execute write requests bigger than minimum size */
  291. raw3215_start_io(raw);
  292. if (raw->flags & RAW3215_TIMER_RUNS) {
  293. del_timer(&raw->timer);
  294. raw->flags &= ~RAW3215_TIMER_RUNS;
  295. }
  296. } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
  297. /* delay small writes */
  298. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  299. add_timer(&raw->timer);
  300. raw->flags |= RAW3215_TIMER_RUNS;
  301. }
  302. }
  303. }
  304. /*
  305. * Call tty_wakeup from tasklet context
  306. */
  307. static void raw3215_wakeup(unsigned long data)
  308. {
  309. struct raw3215_info *raw = (struct raw3215_info *) data;
  310. struct tty_struct *tty;
  311. tty = tty_port_tty_get(&raw->port);
  312. if (tty) {
  313. tty_wakeup(tty);
  314. tty_kref_put(tty);
  315. }
  316. }
  317. /*
  318. * Try to start the next IO and wake up processes waiting on the tty.
  319. */
  320. static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
  321. {
  322. raw3215_mk_write_req(raw);
  323. raw3215_try_io(raw);
  324. if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
  325. tasklet_schedule(&raw->tlet);
  326. }
  327. /*
  328. * Interrupt routine, called from common io layer
  329. */
  330. static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
  331. struct irb *irb)
  332. {
  333. struct raw3215_info *raw;
  334. struct raw3215_req *req;
  335. struct tty_struct *tty;
  336. int cstat, dstat;
  337. int count;
  338. raw = dev_get_drvdata(&cdev->dev);
  339. req = (struct raw3215_req *) intparm;
  340. tty = tty_port_tty_get(&raw->port);
  341. cstat = irb->scsw.cmd.cstat;
  342. dstat = irb->scsw.cmd.dstat;
  343. if (cstat != 0)
  344. raw3215_next_io(raw, tty);
  345. if (dstat & 0x01) { /* we got a unit exception */
  346. dstat &= ~0x01; /* we can ignore it */
  347. }
  348. switch (dstat) {
  349. case 0x80:
  350. if (cstat != 0)
  351. break;
  352. /* Attention interrupt, someone hit the enter key */
  353. raw3215_mk_read_req(raw);
  354. raw3215_next_io(raw, tty);
  355. break;
  356. case 0x08:
  357. case 0x0C:
  358. /* Channel end interrupt. */
  359. if ((raw = req->info) == NULL)
  360. goto put_tty; /* That shouldn't happen ... */
  361. if (req->type == RAW3215_READ) {
  362. /* store residual count, then wait for device end */
  363. req->residual = irb->scsw.cmd.count;
  364. }
  365. if (dstat == 0x08)
  366. break;
  367. case 0x04:
  368. /* Device end interrupt. */
  369. if ((raw = req->info) == NULL)
  370. goto put_tty; /* That shouldn't happen ... */
  371. if (req->type == RAW3215_READ && tty != NULL) {
  372. unsigned int cchar;
  373. count = 160 - req->residual;
  374. EBCASC(raw->inbuf, count);
  375. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  376. switch (cchar & CTRLCHAR_MASK) {
  377. case CTRLCHAR_SYSRQ:
  378. break;
  379. case CTRLCHAR_CTRL:
  380. tty_insert_flip_char(tty, cchar, TTY_NORMAL);
  381. tty_flip_buffer_push(tty);
  382. break;
  383. case CTRLCHAR_NONE:
  384. if (count < 2 ||
  385. (strncmp(raw->inbuf+count-2, "\252n", 2) &&
  386. strncmp(raw->inbuf+count-2, "^n", 2)) ) {
  387. /* add the auto \n */
  388. raw->inbuf[count] = '\n';
  389. count++;
  390. } else
  391. count -= 2;
  392. tty_insert_flip_string(tty, raw->inbuf, count);
  393. tty_flip_buffer_push(tty);
  394. break;
  395. }
  396. } else if (req->type == RAW3215_WRITE) {
  397. raw->count -= req->len;
  398. raw->written -= req->len;
  399. }
  400. raw->flags &= ~RAW3215_WORKING;
  401. raw3215_free_req(req);
  402. /* check for empty wait */
  403. if (waitqueue_active(&raw->empty_wait) &&
  404. raw->queued_write == NULL &&
  405. raw->queued_read == NULL) {
  406. wake_up_interruptible(&raw->empty_wait);
  407. }
  408. raw3215_next_io(raw, tty);
  409. break;
  410. default:
  411. /* Strange interrupt, I'll do my best to clean up */
  412. if (req != NULL && req->type != RAW3215_FREE) {
  413. if (req->type == RAW3215_WRITE) {
  414. raw->count -= req->len;
  415. raw->written -= req->len;
  416. }
  417. raw->flags &= ~RAW3215_WORKING;
  418. raw3215_free_req(req);
  419. }
  420. raw3215_next_io(raw, tty);
  421. }
  422. put_tty:
  423. tty_kref_put(tty);
  424. }
  425. /*
  426. * Drop the oldest line from the output buffer.
  427. */
  428. static void raw3215_drop_line(struct raw3215_info *raw)
  429. {
  430. int ix;
  431. char ch;
  432. BUG_ON(raw->written != 0);
  433. ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
  434. while (raw->count > 0) {
  435. ch = raw->buffer[ix];
  436. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  437. raw->count--;
  438. if (ch == 0x15)
  439. break;
  440. }
  441. raw->head = ix;
  442. }
  443. /*
  444. * Wait until length bytes are available int the output buffer.
  445. * Has to be called with the s390irq lock held. Can be called
  446. * disabled.
  447. */
  448. static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  449. {
  450. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  451. /* While console is frozen for suspend we have no other
  452. * choice but to drop message from the buffer to make
  453. * room for even more messages. */
  454. if (raw->port.flags & ASYNC_SUSPENDED) {
  455. raw3215_drop_line(raw);
  456. continue;
  457. }
  458. /* there might be a request pending */
  459. raw->flags |= RAW3215_FLUSHING;
  460. raw3215_mk_write_req(raw);
  461. raw3215_try_io(raw);
  462. raw->flags &= ~RAW3215_FLUSHING;
  463. #ifdef CONFIG_TN3215_CONSOLE
  464. wait_cons_dev();
  465. #endif
  466. /* Enough room freed up ? */
  467. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  468. break;
  469. /* there might be another cpu waiting for the lock */
  470. spin_unlock(get_ccwdev_lock(raw->cdev));
  471. udelay(100);
  472. spin_lock(get_ccwdev_lock(raw->cdev));
  473. }
  474. }
  475. /*
  476. * String write routine for 3215 devices
  477. */
  478. static void raw3215_write(struct raw3215_info *raw, const char *str,
  479. unsigned int length)
  480. {
  481. unsigned long flags;
  482. int c, count;
  483. while (length > 0) {
  484. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  485. count = (length > RAW3215_BUFFER_SIZE) ?
  486. RAW3215_BUFFER_SIZE : length;
  487. length -= count;
  488. raw3215_make_room(raw, count);
  489. /* copy string to output buffer and convert it to EBCDIC */
  490. while (1) {
  491. c = min_t(int, count,
  492. min(RAW3215_BUFFER_SIZE - raw->count,
  493. RAW3215_BUFFER_SIZE - raw->head));
  494. if (c <= 0)
  495. break;
  496. memcpy(raw->buffer + raw->head, str, c);
  497. ASCEBC(raw->buffer + raw->head, c);
  498. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  499. raw->count += c;
  500. raw->line_pos += c;
  501. str += c;
  502. count -= c;
  503. }
  504. if (!(raw->flags & RAW3215_WORKING)) {
  505. raw3215_mk_write_req(raw);
  506. /* start or queue request */
  507. raw3215_try_io(raw);
  508. }
  509. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  510. }
  511. }
  512. /*
  513. * Put character routine for 3215 devices
  514. */
  515. static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  516. {
  517. unsigned long flags;
  518. unsigned int length, i;
  519. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  520. if (ch == '\t') {
  521. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  522. raw->line_pos += length;
  523. ch = ' ';
  524. } else if (ch == '\n') {
  525. length = 1;
  526. raw->line_pos = 0;
  527. } else {
  528. length = 1;
  529. raw->line_pos++;
  530. }
  531. raw3215_make_room(raw, length);
  532. for (i = 0; i < length; i++) {
  533. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  534. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  535. raw->count++;
  536. }
  537. if (!(raw->flags & RAW3215_WORKING)) {
  538. raw3215_mk_write_req(raw);
  539. /* start or queue request */
  540. raw3215_try_io(raw);
  541. }
  542. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  543. }
  544. /*
  545. * Flush routine, it simply sets the flush flag and tries to start
  546. * pending IO.
  547. */
  548. static void raw3215_flush_buffer(struct raw3215_info *raw)
  549. {
  550. unsigned long flags;
  551. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  552. if (raw->count > 0) {
  553. raw->flags |= RAW3215_FLUSHING;
  554. raw3215_try_io(raw);
  555. raw->flags &= ~RAW3215_FLUSHING;
  556. }
  557. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  558. }
  559. /*
  560. * Fire up a 3215 device.
  561. */
  562. static int raw3215_startup(struct raw3215_info *raw)
  563. {
  564. unsigned long flags;
  565. if (raw->port.flags & ASYNC_INITIALIZED)
  566. return 0;
  567. raw->line_pos = 0;
  568. raw->port.flags |= ASYNC_INITIALIZED;
  569. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  570. raw3215_try_io(raw);
  571. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  572. return 0;
  573. }
  574. /*
  575. * Shutdown a 3215 device.
  576. */
  577. static void raw3215_shutdown(struct raw3215_info *raw)
  578. {
  579. DECLARE_WAITQUEUE(wait, current);
  580. unsigned long flags;
  581. if (!(raw->port.flags & ASYNC_INITIALIZED))
  582. return;
  583. /* Wait for outstanding requests, then free irq */
  584. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  585. if ((raw->flags & RAW3215_WORKING) ||
  586. raw->queued_write != NULL ||
  587. raw->queued_read != NULL) {
  588. raw->port.flags |= ASYNC_CLOSING;
  589. add_wait_queue(&raw->empty_wait, &wait);
  590. set_current_state(TASK_INTERRUPTIBLE);
  591. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  592. schedule();
  593. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  594. remove_wait_queue(&raw->empty_wait, &wait);
  595. set_current_state(TASK_RUNNING);
  596. raw->port.flags &= ~(ASYNC_INITIALIZED | ASYNC_CLOSING);
  597. }
  598. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  599. }
  600. static struct raw3215_info *raw3215_alloc_info(void)
  601. {
  602. struct raw3215_info *info;
  603. info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
  604. if (!info)
  605. return NULL;
  606. info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  607. info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
  608. if (!info->buffer || !info->inbuf) {
  609. kfree(info);
  610. return NULL;
  611. }
  612. setup_timer(&info->timer, raw3215_timeout, (unsigned long)info);
  613. init_waitqueue_head(&info->empty_wait);
  614. tasklet_init(&info->tlet, raw3215_wakeup, (unsigned long)info);
  615. tty_port_init(&info->port);
  616. return info;
  617. }
  618. static void raw3215_free_info(struct raw3215_info *raw)
  619. {
  620. kfree(raw->inbuf);
  621. kfree(raw->buffer);
  622. kfree(raw);
  623. }
  624. static int raw3215_probe (struct ccw_device *cdev)
  625. {
  626. struct raw3215_info *raw;
  627. int line;
  628. /* Console is special. */
  629. if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
  630. return 0;
  631. raw = raw3215_alloc_info();
  632. if (raw == NULL)
  633. return -ENOMEM;
  634. raw->cdev = cdev;
  635. dev_set_drvdata(&cdev->dev, raw);
  636. cdev->handler = raw3215_irq;
  637. spin_lock(&raw3215_device_lock);
  638. for (line = 0; line < NR_3215; line++) {
  639. if (!raw3215[line]) {
  640. raw3215[line] = raw;
  641. break;
  642. }
  643. }
  644. spin_unlock(&raw3215_device_lock);
  645. if (line == NR_3215) {
  646. raw3215_free_info(raw);
  647. return -ENODEV;
  648. }
  649. return 0;
  650. }
  651. static void raw3215_remove (struct ccw_device *cdev)
  652. {
  653. struct raw3215_info *raw;
  654. unsigned int line;
  655. ccw_device_set_offline(cdev);
  656. raw = dev_get_drvdata(&cdev->dev);
  657. if (raw) {
  658. spin_lock(&raw3215_device_lock);
  659. for (line = 0; line < NR_3215; line++)
  660. if (raw3215[line] == raw)
  661. break;
  662. raw3215[line] = NULL;
  663. spin_unlock(&raw3215_device_lock);
  664. dev_set_drvdata(&cdev->dev, NULL);
  665. raw3215_free_info(raw);
  666. }
  667. }
  668. static int raw3215_set_online (struct ccw_device *cdev)
  669. {
  670. struct raw3215_info *raw;
  671. raw = dev_get_drvdata(&cdev->dev);
  672. if (!raw)
  673. return -ENODEV;
  674. return raw3215_startup(raw);
  675. }
  676. static int raw3215_set_offline (struct ccw_device *cdev)
  677. {
  678. struct raw3215_info *raw;
  679. raw = dev_get_drvdata(&cdev->dev);
  680. if (!raw)
  681. return -ENODEV;
  682. raw3215_shutdown(raw);
  683. return 0;
  684. }
  685. static int raw3215_pm_stop(struct ccw_device *cdev)
  686. {
  687. struct raw3215_info *raw;
  688. unsigned long flags;
  689. /* Empty the output buffer, then prevent new I/O. */
  690. raw = dev_get_drvdata(&cdev->dev);
  691. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  692. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  693. raw->port.flags |= ASYNC_SUSPENDED;
  694. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  695. return 0;
  696. }
  697. static int raw3215_pm_start(struct ccw_device *cdev)
  698. {
  699. struct raw3215_info *raw;
  700. unsigned long flags;
  701. /* Allow I/O again and flush output buffer. */
  702. raw = dev_get_drvdata(&cdev->dev);
  703. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  704. raw->port.flags &= ~ASYNC_SUSPENDED;
  705. raw->flags |= RAW3215_FLUSHING;
  706. raw3215_try_io(raw);
  707. raw->flags &= ~RAW3215_FLUSHING;
  708. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  709. return 0;
  710. }
  711. static struct ccw_device_id raw3215_id[] = {
  712. { CCW_DEVICE(0x3215, 0) },
  713. { /* end of list */ },
  714. };
  715. static struct ccw_driver raw3215_ccw_driver = {
  716. .driver = {
  717. .name = "3215",
  718. .owner = THIS_MODULE,
  719. },
  720. .ids = raw3215_id,
  721. .probe = &raw3215_probe,
  722. .remove = &raw3215_remove,
  723. .set_online = &raw3215_set_online,
  724. .set_offline = &raw3215_set_offline,
  725. .freeze = &raw3215_pm_stop,
  726. .thaw = &raw3215_pm_start,
  727. .restore = &raw3215_pm_start,
  728. .int_class = IOINT_C15,
  729. };
  730. #ifdef CONFIG_TN3215_CONSOLE
  731. /*
  732. * Write a string to the 3215 console
  733. */
  734. static void con3215_write(struct console *co, const char *str,
  735. unsigned int count)
  736. {
  737. struct raw3215_info *raw;
  738. int i;
  739. if (count <= 0)
  740. return;
  741. raw = raw3215[0]; /* console 3215 is the first one */
  742. while (count > 0) {
  743. for (i = 0; i < count; i++)
  744. if (str[i] == '\t' || str[i] == '\n')
  745. break;
  746. raw3215_write(raw, str, i);
  747. count -= i;
  748. str += i;
  749. if (count > 0) {
  750. raw3215_putchar(raw, *str);
  751. count--;
  752. str++;
  753. }
  754. }
  755. }
  756. static struct tty_driver *con3215_device(struct console *c, int *index)
  757. {
  758. *index = c->index;
  759. return tty3215_driver;
  760. }
  761. /*
  762. * panic() calls con3215_flush through a panic_notifier
  763. * before the system enters a disabled, endless loop.
  764. */
  765. static void con3215_flush(void)
  766. {
  767. struct raw3215_info *raw;
  768. unsigned long flags;
  769. raw = raw3215[0]; /* console 3215 is the first one */
  770. if (raw->port.flags & ASYNC_SUSPENDED)
  771. /* The console is still frozen for suspend. */
  772. if (ccw_device_force_console())
  773. /* Forcing didn't work, no panic message .. */
  774. return;
  775. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  776. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  777. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  778. }
  779. static int con3215_notify(struct notifier_block *self,
  780. unsigned long event, void *data)
  781. {
  782. con3215_flush();
  783. return NOTIFY_OK;
  784. }
  785. static struct notifier_block on_panic_nb = {
  786. .notifier_call = con3215_notify,
  787. .priority = 0,
  788. };
  789. static struct notifier_block on_reboot_nb = {
  790. .notifier_call = con3215_notify,
  791. .priority = 0,
  792. };
  793. /*
  794. * The console structure for the 3215 console
  795. */
  796. static struct console con3215 = {
  797. .name = "ttyS",
  798. .write = con3215_write,
  799. .device = con3215_device,
  800. .flags = CON_PRINTBUFFER,
  801. };
  802. /*
  803. * 3215 console initialization code called from console_init().
  804. */
  805. static int __init con3215_init(void)
  806. {
  807. struct ccw_device *cdev;
  808. struct raw3215_info *raw;
  809. struct raw3215_req *req;
  810. int i;
  811. /* Check if 3215 is to be the console */
  812. if (!CONSOLE_IS_3215)
  813. return -ENODEV;
  814. /* Set the console mode for VM */
  815. if (MACHINE_IS_VM) {
  816. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  817. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  818. }
  819. /* allocate 3215 request structures */
  820. raw3215_freelist = NULL;
  821. spin_lock_init(&raw3215_freelist_lock);
  822. for (i = 0; i < NR_3215_REQ; i++) {
  823. req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
  824. req->next = raw3215_freelist;
  825. raw3215_freelist = req;
  826. }
  827. cdev = ccw_device_probe_console();
  828. if (IS_ERR(cdev))
  829. return -ENODEV;
  830. raw3215[0] = raw = raw3215_alloc_info();
  831. raw->cdev = cdev;
  832. dev_set_drvdata(&cdev->dev, raw);
  833. cdev->handler = raw3215_irq;
  834. /* Request the console irq */
  835. if (raw3215_startup(raw) != 0) {
  836. raw3215_free_info(raw);
  837. raw3215[0] = NULL;
  838. return -ENODEV;
  839. }
  840. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  841. register_reboot_notifier(&on_reboot_nb);
  842. register_console(&con3215);
  843. return 0;
  844. }
  845. console_initcall(con3215_init);
  846. #endif
  847. static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
  848. {
  849. struct raw3215_info *raw;
  850. raw = raw3215[tty->index];
  851. if (raw == NULL)
  852. return -ENODEV;
  853. tty->driver_data = raw;
  854. return tty_port_install(&raw->port, driver, tty);
  855. }
  856. /*
  857. * tty3215_open
  858. *
  859. * This routine is called whenever a 3215 tty is opened.
  860. */
  861. static int tty3215_open(struct tty_struct *tty, struct file * filp)
  862. {
  863. struct raw3215_info *raw = tty->driver_data;
  864. int retval;
  865. tty_port_tty_set(&raw->port, tty);
  866. tty->low_latency = 0; /* don't use bottom half for pushing chars */
  867. /*
  868. * Start up 3215 device
  869. */
  870. retval = raw3215_startup(raw);
  871. if (retval)
  872. return retval;
  873. return 0;
  874. }
  875. /*
  876. * tty3215_close()
  877. *
  878. * This routine is called when the 3215 tty is closed. We wait
  879. * for the remaining request to be completed. Then we clean up.
  880. */
  881. static void tty3215_close(struct tty_struct *tty, struct file * filp)
  882. {
  883. struct raw3215_info *raw;
  884. raw = (struct raw3215_info *) tty->driver_data;
  885. if (raw == NULL || tty->count > 1)
  886. return;
  887. tty->closing = 1;
  888. /* Shutdown the terminal */
  889. raw3215_shutdown(raw);
  890. tasklet_kill(&raw->tlet);
  891. tty->closing = 0;
  892. tty_port_tty_set(&raw->port, NULL);
  893. }
  894. /*
  895. * Returns the amount of free space in the output buffer.
  896. */
  897. static int tty3215_write_room(struct tty_struct *tty)
  898. {
  899. struct raw3215_info *raw;
  900. raw = (struct raw3215_info *) tty->driver_data;
  901. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  902. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  903. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  904. else
  905. return 0;
  906. }
  907. /*
  908. * String write routine for 3215 ttys
  909. */
  910. static int tty3215_write(struct tty_struct * tty,
  911. const unsigned char *buf, int count)
  912. {
  913. struct raw3215_info *raw;
  914. if (!tty)
  915. return 0;
  916. raw = (struct raw3215_info *) tty->driver_data;
  917. raw3215_write(raw, buf, count);
  918. return count;
  919. }
  920. /*
  921. * Put character routine for 3215 ttys
  922. */
  923. static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  924. {
  925. struct raw3215_info *raw;
  926. if (!tty)
  927. return 0;
  928. raw = (struct raw3215_info *) tty->driver_data;
  929. raw3215_putchar(raw, ch);
  930. return 1;
  931. }
  932. static void tty3215_flush_chars(struct tty_struct *tty)
  933. {
  934. }
  935. /*
  936. * Returns the number of characters in the output buffer
  937. */
  938. static int tty3215_chars_in_buffer(struct tty_struct *tty)
  939. {
  940. struct raw3215_info *raw;
  941. raw = (struct raw3215_info *) tty->driver_data;
  942. return raw->count;
  943. }
  944. static void tty3215_flush_buffer(struct tty_struct *tty)
  945. {
  946. struct raw3215_info *raw;
  947. raw = (struct raw3215_info *) tty->driver_data;
  948. raw3215_flush_buffer(raw);
  949. tty_wakeup(tty);
  950. }
  951. /*
  952. * Disable reading from a 3215 tty
  953. */
  954. static void tty3215_throttle(struct tty_struct * tty)
  955. {
  956. struct raw3215_info *raw;
  957. raw = (struct raw3215_info *) tty->driver_data;
  958. raw->flags |= RAW3215_THROTTLED;
  959. }
  960. /*
  961. * Enable reading from a 3215 tty
  962. */
  963. static void tty3215_unthrottle(struct tty_struct * tty)
  964. {
  965. struct raw3215_info *raw;
  966. unsigned long flags;
  967. raw = (struct raw3215_info *) tty->driver_data;
  968. if (raw->flags & RAW3215_THROTTLED) {
  969. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  970. raw->flags &= ~RAW3215_THROTTLED;
  971. raw3215_try_io(raw);
  972. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  973. }
  974. }
  975. /*
  976. * Disable writing to a 3215 tty
  977. */
  978. static void tty3215_stop(struct tty_struct *tty)
  979. {
  980. struct raw3215_info *raw;
  981. raw = (struct raw3215_info *) tty->driver_data;
  982. raw->flags |= RAW3215_STOPPED;
  983. }
  984. /*
  985. * Enable writing to a 3215 tty
  986. */
  987. static void tty3215_start(struct tty_struct *tty)
  988. {
  989. struct raw3215_info *raw;
  990. unsigned long flags;
  991. raw = (struct raw3215_info *) tty->driver_data;
  992. if (raw->flags & RAW3215_STOPPED) {
  993. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  994. raw->flags &= ~RAW3215_STOPPED;
  995. raw3215_try_io(raw);
  996. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  997. }
  998. }
  999. static const struct tty_operations tty3215_ops = {
  1000. .install = tty3215_install,
  1001. .open = tty3215_open,
  1002. .close = tty3215_close,
  1003. .write = tty3215_write,
  1004. .put_char = tty3215_put_char,
  1005. .flush_chars = tty3215_flush_chars,
  1006. .write_room = tty3215_write_room,
  1007. .chars_in_buffer = tty3215_chars_in_buffer,
  1008. .flush_buffer = tty3215_flush_buffer,
  1009. .throttle = tty3215_throttle,
  1010. .unthrottle = tty3215_unthrottle,
  1011. .stop = tty3215_stop,
  1012. .start = tty3215_start,
  1013. };
  1014. /*
  1015. * 3215 tty registration code called from tty_init().
  1016. * Most kernel services (incl. kmalloc) are available at this poimt.
  1017. */
  1018. static int __init tty3215_init(void)
  1019. {
  1020. struct tty_driver *driver;
  1021. int ret;
  1022. if (!CONSOLE_IS_3215)
  1023. return 0;
  1024. driver = alloc_tty_driver(NR_3215);
  1025. if (!driver)
  1026. return -ENOMEM;
  1027. ret = ccw_driver_register(&raw3215_ccw_driver);
  1028. if (ret) {
  1029. put_tty_driver(driver);
  1030. return ret;
  1031. }
  1032. /*
  1033. * Initialize the tty_driver structure
  1034. * Entries in tty3215_driver that are NOT initialized:
  1035. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1036. */
  1037. driver->driver_name = "tty3215";
  1038. driver->name = "ttyS";
  1039. driver->major = TTY_MAJOR;
  1040. driver->minor_start = 64;
  1041. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1042. driver->subtype = SYSTEM_TYPE_TTY;
  1043. driver->init_termios = tty_std_termios;
  1044. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1045. driver->init_termios.c_oflag = ONLCR | XTABS;
  1046. driver->init_termios.c_lflag = ISIG;
  1047. driver->flags = TTY_DRIVER_REAL_RAW;
  1048. tty_set_operations(driver, &tty3215_ops);
  1049. ret = tty_register_driver(driver);
  1050. if (ret) {
  1051. put_tty_driver(driver);
  1052. return ret;
  1053. }
  1054. tty3215_driver = driver;
  1055. return 0;
  1056. }
  1057. static void __exit tty3215_exit(void)
  1058. {
  1059. tty_unregister_driver(tty3215_driver);
  1060. put_tty_driver(tty3215_driver);
  1061. ccw_driver_unregister(&raw3215_ccw_driver);
  1062. }
  1063. module_init(tty3215_init);
  1064. module_exit(tty3215_exit);