con3215.c 29 KB

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