con3215.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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/types.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/tty.h>
  14. #include <linux/tty_flip.h>
  15. #include <linux/vt_kern.h>
  16. #include <linux/init.h>
  17. #include <linux/console.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/err.h>
  20. #include <linux/reboot.h>
  21. #include <linux/serial.h> /* ASYNC_* flags */
  22. #include <linux/slab.h>
  23. #include <asm/ccwdev.h>
  24. #include <asm/cio.h>
  25. #include <asm/io.h>
  26. #include <asm/ebcdic.h>
  27. #include <linux/uaccess.h>
  28. #include <asm/delay.h>
  29. #include <asm/cpcmd.h>
  30. #include <asm/setup.h>
  31. #include "ctrlchar.h"
  32. #define NR_3215 1
  33. #define NR_3215_REQ (4*NR_3215)
  34. #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
  35. #define RAW3215_INBUF_SIZE 256 /* input buffer size */
  36. #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
  37. #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
  38. #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
  39. #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
  40. #define RAW3215_NR_CCWS 3
  41. #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
  42. #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
  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. raw->flags &= ~RAW3215_TIMER_RUNS;
  265. if (!tty_port_suspended(&raw->port)) {
  266. raw3215_mk_write_req(raw);
  267. raw3215_start_io(raw);
  268. if ((raw->queued_read || raw->queued_write) &&
  269. !(raw->flags & RAW3215_WORKING) &&
  270. !(raw->flags & RAW3215_TIMER_RUNS)) {
  271. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  272. add_timer(&raw->timer);
  273. raw->flags |= RAW3215_TIMER_RUNS;
  274. }
  275. }
  276. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  277. }
  278. /*
  279. * Function to conditionally start an IO. A read is started immediately,
  280. * a write is only started immediately if the flush flag is on or the
  281. * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
  282. * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
  283. */
  284. static inline void raw3215_try_io(struct raw3215_info *raw)
  285. {
  286. if (!tty_port_initialized(&raw->port) || tty_port_suspended(&raw->port))
  287. return;
  288. if (raw->queued_read != NULL)
  289. raw3215_start_io(raw);
  290. else if (raw->queued_write != NULL) {
  291. if ((raw->queued_write->delayable == 0) ||
  292. (raw->flags & RAW3215_FLUSHING)) {
  293. /* execute write requests bigger than minimum size */
  294. raw3215_start_io(raw);
  295. }
  296. }
  297. if ((raw->queued_read || raw->queued_write) &&
  298. !(raw->flags & RAW3215_WORKING) &&
  299. !(raw->flags & RAW3215_TIMER_RUNS)) {
  300. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  301. add_timer(&raw->timer);
  302. raw->flags |= RAW3215_TIMER_RUNS;
  303. }
  304. }
  305. /*
  306. * Call tty_wakeup from tasklet context
  307. */
  308. static void raw3215_wakeup(unsigned long data)
  309. {
  310. struct raw3215_info *raw = (struct raw3215_info *) data;
  311. struct tty_struct *tty;
  312. tty = tty_port_tty_get(&raw->port);
  313. if (tty) {
  314. tty_wakeup(tty);
  315. tty_kref_put(tty);
  316. }
  317. }
  318. /*
  319. * Try to start the next IO and wake up processes waiting on the tty.
  320. */
  321. static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
  322. {
  323. raw3215_mk_write_req(raw);
  324. raw3215_try_io(raw);
  325. if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
  326. tasklet_schedule(&raw->tlet);
  327. }
  328. /*
  329. * Interrupt routine, called from common io layer
  330. */
  331. static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
  332. struct irb *irb)
  333. {
  334. struct raw3215_info *raw;
  335. struct raw3215_req *req;
  336. struct tty_struct *tty;
  337. int cstat, dstat;
  338. int count;
  339. raw = dev_get_drvdata(&cdev->dev);
  340. req = (struct raw3215_req *) intparm;
  341. tty = tty_port_tty_get(&raw->port);
  342. cstat = irb->scsw.cmd.cstat;
  343. dstat = irb->scsw.cmd.dstat;
  344. if (cstat != 0)
  345. raw3215_next_io(raw, tty);
  346. if (dstat & 0x01) { /* we got a unit exception */
  347. dstat &= ~0x01; /* we can ignore it */
  348. }
  349. switch (dstat) {
  350. case 0x80:
  351. if (cstat != 0)
  352. break;
  353. /* Attention interrupt, someone hit the enter key */
  354. raw3215_mk_read_req(raw);
  355. raw3215_next_io(raw, tty);
  356. break;
  357. case 0x08:
  358. case 0x0C:
  359. /* Channel end interrupt. */
  360. if ((raw = req->info) == NULL)
  361. goto put_tty; /* That shouldn't happen ... */
  362. if (req->type == RAW3215_READ) {
  363. /* store residual count, then wait for device end */
  364. req->residual = irb->scsw.cmd.count;
  365. }
  366. if (dstat == 0x08)
  367. break;
  368. case 0x04:
  369. /* Device end interrupt. */
  370. if ((raw = req->info) == NULL)
  371. goto put_tty; /* That shouldn't happen ... */
  372. if (req->type == RAW3215_READ && tty != NULL) {
  373. unsigned int cchar;
  374. count = 160 - req->residual;
  375. EBCASC(raw->inbuf, count);
  376. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  377. switch (cchar & CTRLCHAR_MASK) {
  378. case CTRLCHAR_SYSRQ:
  379. break;
  380. case CTRLCHAR_CTRL:
  381. tty_insert_flip_char(&raw->port, cchar,
  382. TTY_NORMAL);
  383. tty_flip_buffer_push(&raw->port);
  384. break;
  385. case CTRLCHAR_NONE:
  386. if (count < 2 ||
  387. (strncmp(raw->inbuf+count-2, "\252n", 2) &&
  388. strncmp(raw->inbuf+count-2, "^n", 2)) ) {
  389. /* add the auto \n */
  390. raw->inbuf[count] = '\n';
  391. count++;
  392. } else
  393. count -= 2;
  394. tty_insert_flip_string(&raw->port, raw->inbuf,
  395. count);
  396. tty_flip_buffer_push(&raw->port);
  397. break;
  398. }
  399. } else if (req->type == RAW3215_WRITE) {
  400. raw->count -= req->len;
  401. raw->written -= req->len;
  402. }
  403. raw->flags &= ~RAW3215_WORKING;
  404. raw3215_free_req(req);
  405. /* check for empty wait */
  406. if (waitqueue_active(&raw->empty_wait) &&
  407. raw->queued_write == NULL &&
  408. raw->queued_read == NULL) {
  409. wake_up_interruptible(&raw->empty_wait);
  410. }
  411. raw3215_next_io(raw, tty);
  412. break;
  413. default:
  414. /* Strange interrupt, I'll do my best to clean up */
  415. if (req != NULL && req->type != RAW3215_FREE) {
  416. if (req->type == RAW3215_WRITE) {
  417. raw->count -= req->len;
  418. raw->written -= req->len;
  419. }
  420. raw->flags &= ~RAW3215_WORKING;
  421. raw3215_free_req(req);
  422. }
  423. raw3215_next_io(raw, tty);
  424. }
  425. put_tty:
  426. tty_kref_put(tty);
  427. }
  428. /*
  429. * Drop the oldest line from the output buffer.
  430. */
  431. static void raw3215_drop_line(struct raw3215_info *raw)
  432. {
  433. int ix;
  434. char ch;
  435. BUG_ON(raw->written != 0);
  436. ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
  437. while (raw->count > 0) {
  438. ch = raw->buffer[ix];
  439. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  440. raw->count--;
  441. if (ch == 0x15)
  442. break;
  443. }
  444. raw->head = ix;
  445. }
  446. /*
  447. * Wait until length bytes are available int the output buffer.
  448. * Has to be called with the s390irq lock held. Can be called
  449. * disabled.
  450. */
  451. static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  452. {
  453. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  454. /* While console is frozen for suspend we have no other
  455. * choice but to drop message from the buffer to make
  456. * room for even more messages. */
  457. if (tty_port_suspended(&raw->port)) {
  458. raw3215_drop_line(raw);
  459. continue;
  460. }
  461. /* there might be a request pending */
  462. raw->flags |= RAW3215_FLUSHING;
  463. raw3215_mk_write_req(raw);
  464. raw3215_try_io(raw);
  465. raw->flags &= ~RAW3215_FLUSHING;
  466. #ifdef CONFIG_TN3215_CONSOLE
  467. ccw_device_wait_idle(raw->cdev);
  468. #endif
  469. /* Enough room freed up ? */
  470. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  471. break;
  472. /* there might be another cpu waiting for the lock */
  473. spin_unlock(get_ccwdev_lock(raw->cdev));
  474. udelay(100);
  475. spin_lock(get_ccwdev_lock(raw->cdev));
  476. }
  477. }
  478. /*
  479. * String write routine for 3215 devices
  480. */
  481. static void raw3215_write(struct raw3215_info *raw, const char *str,
  482. unsigned int length)
  483. {
  484. unsigned long flags;
  485. int c, count;
  486. while (length > 0) {
  487. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  488. count = (length > RAW3215_BUFFER_SIZE) ?
  489. RAW3215_BUFFER_SIZE : length;
  490. length -= count;
  491. raw3215_make_room(raw, count);
  492. /* copy string to output buffer and convert it to EBCDIC */
  493. while (1) {
  494. c = min_t(int, count,
  495. min(RAW3215_BUFFER_SIZE - raw->count,
  496. RAW3215_BUFFER_SIZE - raw->head));
  497. if (c <= 0)
  498. break;
  499. memcpy(raw->buffer + raw->head, str, c);
  500. ASCEBC(raw->buffer + raw->head, c);
  501. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  502. raw->count += c;
  503. raw->line_pos += c;
  504. str += c;
  505. count -= c;
  506. }
  507. if (!(raw->flags & RAW3215_WORKING)) {
  508. raw3215_mk_write_req(raw);
  509. /* start or queue request */
  510. raw3215_try_io(raw);
  511. }
  512. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  513. }
  514. }
  515. /*
  516. * Put character routine for 3215 devices
  517. */
  518. static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  519. {
  520. unsigned long flags;
  521. unsigned int length, i;
  522. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  523. if (ch == '\t') {
  524. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  525. raw->line_pos += length;
  526. ch = ' ';
  527. } else if (ch == '\n') {
  528. length = 1;
  529. raw->line_pos = 0;
  530. } else {
  531. length = 1;
  532. raw->line_pos++;
  533. }
  534. raw3215_make_room(raw, length);
  535. for (i = 0; i < length; i++) {
  536. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  537. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  538. raw->count++;
  539. }
  540. if (!(raw->flags & RAW3215_WORKING)) {
  541. raw3215_mk_write_req(raw);
  542. /* start or queue request */
  543. raw3215_try_io(raw);
  544. }
  545. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  546. }
  547. /*
  548. * Flush routine, it simply sets the flush flag and tries to start
  549. * pending IO.
  550. */
  551. static void raw3215_flush_buffer(struct raw3215_info *raw)
  552. {
  553. unsigned long flags;
  554. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  555. if (raw->count > 0) {
  556. raw->flags |= RAW3215_FLUSHING;
  557. raw3215_try_io(raw);
  558. raw->flags &= ~RAW3215_FLUSHING;
  559. }
  560. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  561. }
  562. /*
  563. * Fire up a 3215 device.
  564. */
  565. static int raw3215_startup(struct raw3215_info *raw)
  566. {
  567. unsigned long flags;
  568. if (tty_port_initialized(&raw->port))
  569. return 0;
  570. raw->line_pos = 0;
  571. tty_port_set_initialized(&raw->port, 1);
  572. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  573. raw3215_try_io(raw);
  574. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  575. return 0;
  576. }
  577. /*
  578. * Shutdown a 3215 device.
  579. */
  580. static void raw3215_shutdown(struct raw3215_info *raw)
  581. {
  582. DECLARE_WAITQUEUE(wait, current);
  583. unsigned long flags;
  584. if (!tty_port_initialized(&raw->port) || (raw->flags & RAW3215_FIXED))
  585. return;
  586. /* Wait for outstanding requests, then free irq */
  587. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  588. if ((raw->flags & RAW3215_WORKING) ||
  589. raw->queued_write != NULL ||
  590. raw->queued_read != NULL) {
  591. add_wait_queue(&raw->empty_wait, &wait);
  592. set_current_state(TASK_INTERRUPTIBLE);
  593. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  594. schedule();
  595. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  596. remove_wait_queue(&raw->empty_wait, &wait);
  597. set_current_state(TASK_RUNNING);
  598. tty_port_set_initialized(&raw->port, 1);
  599. }
  600. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  601. }
  602. static struct raw3215_info *raw3215_alloc_info(void)
  603. {
  604. struct raw3215_info *info;
  605. info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
  606. if (!info)
  607. return NULL;
  608. info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  609. info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
  610. if (!info->buffer || !info->inbuf) {
  611. kfree(info->inbuf);
  612. kfree(info->buffer);
  613. kfree(info);
  614. return NULL;
  615. }
  616. setup_timer(&info->timer, raw3215_timeout, (unsigned long)info);
  617. init_waitqueue_head(&info->empty_wait);
  618. tasklet_init(&info->tlet, raw3215_wakeup, (unsigned long)info);
  619. tty_port_init(&info->port);
  620. return info;
  621. }
  622. static void raw3215_free_info(struct raw3215_info *raw)
  623. {
  624. kfree(raw->inbuf);
  625. kfree(raw->buffer);
  626. tty_port_destroy(&raw->port);
  627. kfree(raw);
  628. }
  629. static int raw3215_probe (struct ccw_device *cdev)
  630. {
  631. struct raw3215_info *raw;
  632. int line;
  633. /* Console is special. */
  634. if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
  635. return 0;
  636. raw = raw3215_alloc_info();
  637. if (raw == NULL)
  638. return -ENOMEM;
  639. raw->cdev = cdev;
  640. dev_set_drvdata(&cdev->dev, raw);
  641. cdev->handler = raw3215_irq;
  642. spin_lock(&raw3215_device_lock);
  643. for (line = 0; line < NR_3215; line++) {
  644. if (!raw3215[line]) {
  645. raw3215[line] = raw;
  646. break;
  647. }
  648. }
  649. spin_unlock(&raw3215_device_lock);
  650. if (line == NR_3215) {
  651. raw3215_free_info(raw);
  652. return -ENODEV;
  653. }
  654. return 0;
  655. }
  656. static void raw3215_remove (struct ccw_device *cdev)
  657. {
  658. struct raw3215_info *raw;
  659. unsigned int line;
  660. ccw_device_set_offline(cdev);
  661. raw = dev_get_drvdata(&cdev->dev);
  662. if (raw) {
  663. spin_lock(&raw3215_device_lock);
  664. for (line = 0; line < NR_3215; line++)
  665. if (raw3215[line] == raw)
  666. break;
  667. raw3215[line] = NULL;
  668. spin_unlock(&raw3215_device_lock);
  669. dev_set_drvdata(&cdev->dev, NULL);
  670. raw3215_free_info(raw);
  671. }
  672. }
  673. static int raw3215_set_online (struct ccw_device *cdev)
  674. {
  675. struct raw3215_info *raw;
  676. raw = dev_get_drvdata(&cdev->dev);
  677. if (!raw)
  678. return -ENODEV;
  679. return raw3215_startup(raw);
  680. }
  681. static int raw3215_set_offline (struct ccw_device *cdev)
  682. {
  683. struct raw3215_info *raw;
  684. raw = dev_get_drvdata(&cdev->dev);
  685. if (!raw)
  686. return -ENODEV;
  687. raw3215_shutdown(raw);
  688. return 0;
  689. }
  690. static int raw3215_pm_stop(struct ccw_device *cdev)
  691. {
  692. struct raw3215_info *raw;
  693. unsigned long flags;
  694. /* Empty the output buffer, then prevent new I/O. */
  695. raw = dev_get_drvdata(&cdev->dev);
  696. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  697. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  698. tty_port_set_suspended(&raw->port, 1);
  699. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  700. return 0;
  701. }
  702. static int raw3215_pm_start(struct ccw_device *cdev)
  703. {
  704. struct raw3215_info *raw;
  705. unsigned long flags;
  706. /* Allow I/O again and flush output buffer. */
  707. raw = dev_get_drvdata(&cdev->dev);
  708. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  709. tty_port_set_suspended(&raw->port, 0);
  710. raw->flags |= RAW3215_FLUSHING;
  711. raw3215_try_io(raw);
  712. raw->flags &= ~RAW3215_FLUSHING;
  713. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  714. return 0;
  715. }
  716. static struct ccw_device_id raw3215_id[] = {
  717. { CCW_DEVICE(0x3215, 0) },
  718. { /* end of list */ },
  719. };
  720. static struct ccw_driver raw3215_ccw_driver = {
  721. .driver = {
  722. .name = "3215",
  723. .owner = THIS_MODULE,
  724. },
  725. .ids = raw3215_id,
  726. .probe = &raw3215_probe,
  727. .remove = &raw3215_remove,
  728. .set_online = &raw3215_set_online,
  729. .set_offline = &raw3215_set_offline,
  730. .freeze = &raw3215_pm_stop,
  731. .thaw = &raw3215_pm_start,
  732. .restore = &raw3215_pm_start,
  733. .int_class = IRQIO_C15,
  734. };
  735. #ifdef CONFIG_TN3215_CONSOLE
  736. /*
  737. * Write a string to the 3215 console
  738. */
  739. static void con3215_write(struct console *co, const char *str,
  740. unsigned int count)
  741. {
  742. struct raw3215_info *raw;
  743. int i;
  744. if (count <= 0)
  745. return;
  746. raw = raw3215[0]; /* console 3215 is the first one */
  747. while (count > 0) {
  748. for (i = 0; i < count; i++)
  749. if (str[i] == '\t' || str[i] == '\n')
  750. break;
  751. raw3215_write(raw, str, i);
  752. count -= i;
  753. str += i;
  754. if (count > 0) {
  755. raw3215_putchar(raw, *str);
  756. count--;
  757. str++;
  758. }
  759. }
  760. }
  761. static struct tty_driver *con3215_device(struct console *c, int *index)
  762. {
  763. *index = c->index;
  764. return tty3215_driver;
  765. }
  766. /*
  767. * panic() calls con3215_flush through a panic_notifier
  768. * before the system enters a disabled, endless loop.
  769. */
  770. static void con3215_flush(void)
  771. {
  772. struct raw3215_info *raw;
  773. unsigned long flags;
  774. raw = raw3215[0]; /* console 3215 is the first one */
  775. if (tty_port_suspended(&raw->port))
  776. /* The console is still frozen for suspend. */
  777. if (ccw_device_force_console(raw->cdev))
  778. /* Forcing didn't work, no panic message .. */
  779. return;
  780. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  781. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  782. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  783. }
  784. static int con3215_notify(struct notifier_block *self,
  785. unsigned long event, void *data)
  786. {
  787. con3215_flush();
  788. return NOTIFY_OK;
  789. }
  790. static struct notifier_block on_panic_nb = {
  791. .notifier_call = con3215_notify,
  792. .priority = 0,
  793. };
  794. static struct notifier_block on_reboot_nb = {
  795. .notifier_call = con3215_notify,
  796. .priority = 0,
  797. };
  798. /*
  799. * The console structure for the 3215 console
  800. */
  801. static struct console con3215 = {
  802. .name = "ttyS",
  803. .write = con3215_write,
  804. .device = con3215_device,
  805. .flags = CON_PRINTBUFFER,
  806. };
  807. /*
  808. * 3215 console initialization code called from console_init().
  809. */
  810. static int __init con3215_init(void)
  811. {
  812. struct ccw_device *cdev;
  813. struct raw3215_info *raw;
  814. struct raw3215_req *req;
  815. int i;
  816. /* Check if 3215 is to be the console */
  817. if (!CONSOLE_IS_3215)
  818. return -ENODEV;
  819. /* Set the console mode for VM */
  820. if (MACHINE_IS_VM) {
  821. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  822. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  823. }
  824. /* allocate 3215 request structures */
  825. raw3215_freelist = NULL;
  826. spin_lock_init(&raw3215_freelist_lock);
  827. for (i = 0; i < NR_3215_REQ; i++) {
  828. req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
  829. if (!req)
  830. return -ENOMEM;
  831. req->next = raw3215_freelist;
  832. raw3215_freelist = req;
  833. }
  834. cdev = ccw_device_create_console(&raw3215_ccw_driver);
  835. if (IS_ERR(cdev))
  836. return -ENODEV;
  837. raw3215[0] = raw = raw3215_alloc_info();
  838. raw->cdev = cdev;
  839. dev_set_drvdata(&cdev->dev, raw);
  840. cdev->handler = raw3215_irq;
  841. raw->flags |= RAW3215_FIXED;
  842. if (ccw_device_enable_console(cdev)) {
  843. ccw_device_destroy_console(cdev);
  844. raw3215_free_info(raw);
  845. raw3215[0] = NULL;
  846. return -ENODEV;
  847. }
  848. /* Request the console irq */
  849. if (raw3215_startup(raw) != 0) {
  850. raw3215_free_info(raw);
  851. raw3215[0] = NULL;
  852. return -ENODEV;
  853. }
  854. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  855. register_reboot_notifier(&on_reboot_nb);
  856. register_console(&con3215);
  857. return 0;
  858. }
  859. console_initcall(con3215_init);
  860. #endif
  861. static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
  862. {
  863. struct raw3215_info *raw;
  864. raw = raw3215[tty->index];
  865. if (raw == NULL)
  866. return -ENODEV;
  867. tty->driver_data = raw;
  868. return tty_port_install(&raw->port, driver, tty);
  869. }
  870. /*
  871. * tty3215_open
  872. *
  873. * This routine is called whenever a 3215 tty is opened.
  874. */
  875. static int tty3215_open(struct tty_struct *tty, struct file * filp)
  876. {
  877. struct raw3215_info *raw = tty->driver_data;
  878. int retval;
  879. tty_port_tty_set(&raw->port, tty);
  880. raw->port.low_latency = 0; /* don't use bottom half for pushing chars */
  881. /*
  882. * Start up 3215 device
  883. */
  884. retval = raw3215_startup(raw);
  885. if (retval)
  886. return retval;
  887. return 0;
  888. }
  889. /*
  890. * tty3215_close()
  891. *
  892. * This routine is called when the 3215 tty is closed. We wait
  893. * for the remaining request to be completed. Then we clean up.
  894. */
  895. static void tty3215_close(struct tty_struct *tty, struct file * filp)
  896. {
  897. struct raw3215_info *raw;
  898. raw = (struct raw3215_info *) tty->driver_data;
  899. if (raw == NULL || tty->count > 1)
  900. return;
  901. tty->closing = 1;
  902. /* Shutdown the terminal */
  903. raw3215_shutdown(raw);
  904. tasklet_kill(&raw->tlet);
  905. tty->closing = 0;
  906. tty_port_tty_set(&raw->port, NULL);
  907. }
  908. /*
  909. * Returns the amount of free space in the output buffer.
  910. */
  911. static int tty3215_write_room(struct tty_struct *tty)
  912. {
  913. struct raw3215_info *raw;
  914. raw = (struct raw3215_info *) tty->driver_data;
  915. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  916. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  917. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  918. else
  919. return 0;
  920. }
  921. /*
  922. * String write routine for 3215 ttys
  923. */
  924. static int tty3215_write(struct tty_struct * tty,
  925. const unsigned char *buf, int count)
  926. {
  927. struct raw3215_info *raw;
  928. int i, written;
  929. if (!tty)
  930. return 0;
  931. raw = (struct raw3215_info *) tty->driver_data;
  932. written = count;
  933. while (count > 0) {
  934. for (i = 0; i < count; i++)
  935. if (buf[i] == '\t' || buf[i] == '\n')
  936. break;
  937. raw3215_write(raw, buf, i);
  938. count -= i;
  939. buf += i;
  940. if (count > 0) {
  941. raw3215_putchar(raw, *buf);
  942. count--;
  943. buf++;
  944. }
  945. }
  946. return written;
  947. }
  948. /*
  949. * Put character routine for 3215 ttys
  950. */
  951. static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  952. {
  953. struct raw3215_info *raw;
  954. if (!tty)
  955. return 0;
  956. raw = (struct raw3215_info *) tty->driver_data;
  957. raw3215_putchar(raw, ch);
  958. return 1;
  959. }
  960. static void tty3215_flush_chars(struct tty_struct *tty)
  961. {
  962. }
  963. /*
  964. * Returns the number of characters in the output buffer
  965. */
  966. static int tty3215_chars_in_buffer(struct tty_struct *tty)
  967. {
  968. struct raw3215_info *raw;
  969. raw = (struct raw3215_info *) tty->driver_data;
  970. return raw->count;
  971. }
  972. static void tty3215_flush_buffer(struct tty_struct *tty)
  973. {
  974. struct raw3215_info *raw;
  975. raw = (struct raw3215_info *) tty->driver_data;
  976. raw3215_flush_buffer(raw);
  977. tty_wakeup(tty);
  978. }
  979. /*
  980. * Disable reading from a 3215 tty
  981. */
  982. static void tty3215_throttle(struct tty_struct * tty)
  983. {
  984. struct raw3215_info *raw;
  985. raw = (struct raw3215_info *) tty->driver_data;
  986. raw->flags |= RAW3215_THROTTLED;
  987. }
  988. /*
  989. * Enable reading from a 3215 tty
  990. */
  991. static void tty3215_unthrottle(struct tty_struct * tty)
  992. {
  993. struct raw3215_info *raw;
  994. unsigned long flags;
  995. raw = (struct raw3215_info *) tty->driver_data;
  996. if (raw->flags & RAW3215_THROTTLED) {
  997. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  998. raw->flags &= ~RAW3215_THROTTLED;
  999. raw3215_try_io(raw);
  1000. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  1001. }
  1002. }
  1003. /*
  1004. * Disable writing to a 3215 tty
  1005. */
  1006. static void tty3215_stop(struct tty_struct *tty)
  1007. {
  1008. struct raw3215_info *raw;
  1009. raw = (struct raw3215_info *) tty->driver_data;
  1010. raw->flags |= RAW3215_STOPPED;
  1011. }
  1012. /*
  1013. * Enable writing to a 3215 tty
  1014. */
  1015. static void tty3215_start(struct tty_struct *tty)
  1016. {
  1017. struct raw3215_info *raw;
  1018. unsigned long flags;
  1019. raw = (struct raw3215_info *) tty->driver_data;
  1020. if (raw->flags & RAW3215_STOPPED) {
  1021. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  1022. raw->flags &= ~RAW3215_STOPPED;
  1023. raw3215_try_io(raw);
  1024. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  1025. }
  1026. }
  1027. static const struct tty_operations tty3215_ops = {
  1028. .install = tty3215_install,
  1029. .open = tty3215_open,
  1030. .close = tty3215_close,
  1031. .write = tty3215_write,
  1032. .put_char = tty3215_put_char,
  1033. .flush_chars = tty3215_flush_chars,
  1034. .write_room = tty3215_write_room,
  1035. .chars_in_buffer = tty3215_chars_in_buffer,
  1036. .flush_buffer = tty3215_flush_buffer,
  1037. .throttle = tty3215_throttle,
  1038. .unthrottle = tty3215_unthrottle,
  1039. .stop = tty3215_stop,
  1040. .start = tty3215_start,
  1041. };
  1042. /*
  1043. * 3215 tty registration code called from tty_init().
  1044. * Most kernel services (incl. kmalloc) are available at this poimt.
  1045. */
  1046. static int __init tty3215_init(void)
  1047. {
  1048. struct tty_driver *driver;
  1049. int ret;
  1050. if (!CONSOLE_IS_3215)
  1051. return 0;
  1052. driver = alloc_tty_driver(NR_3215);
  1053. if (!driver)
  1054. return -ENOMEM;
  1055. ret = ccw_driver_register(&raw3215_ccw_driver);
  1056. if (ret) {
  1057. put_tty_driver(driver);
  1058. return ret;
  1059. }
  1060. /*
  1061. * Initialize the tty_driver structure
  1062. * Entries in tty3215_driver that are NOT initialized:
  1063. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1064. */
  1065. driver->driver_name = "tty3215";
  1066. driver->name = "ttyS";
  1067. driver->major = TTY_MAJOR;
  1068. driver->minor_start = 64;
  1069. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1070. driver->subtype = SYSTEM_TYPE_TTY;
  1071. driver->init_termios = tty_std_termios;
  1072. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1073. driver->init_termios.c_oflag = ONLCR;
  1074. driver->init_termios.c_lflag = ISIG;
  1075. driver->flags = TTY_DRIVER_REAL_RAW;
  1076. tty_set_operations(driver, &tty3215_ops);
  1077. ret = tty_register_driver(driver);
  1078. if (ret) {
  1079. put_tty_driver(driver);
  1080. return ret;
  1081. }
  1082. tty3215_driver = driver;
  1083. return 0;
  1084. }
  1085. device_initcall(tty3215_init);