raw3270.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. /*
  2. * drivers/s390/char/raw3270.c
  3. * IBM/3270 Driver - core functions.
  4. *
  5. * Author(s):
  6. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  7. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. * -- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  9. */
  10. #include <linux/bootmem.h>
  11. #include <linux/module.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/wait.h>
  19. #include <asm/ccwdev.h>
  20. #include <asm/cio.h>
  21. #include <asm/ebcdic.h>
  22. #include "raw3270.h"
  23. #include <linux/major.h>
  24. #include <linux/kdev_t.h>
  25. #include <linux/device.h>
  26. #include <linux/mutex.h>
  27. static struct class *class3270;
  28. /* The main 3270 data structure. */
  29. struct raw3270 {
  30. struct list_head list;
  31. struct ccw_device *cdev;
  32. int minor;
  33. short model, rows, cols;
  34. unsigned long flags;
  35. struct list_head req_queue; /* Request queue. */
  36. struct list_head view_list; /* List of available views. */
  37. struct raw3270_view *view; /* Active view. */
  38. struct timer_list timer; /* Device timer. */
  39. unsigned char *ascebc; /* ascii -> ebcdic table */
  40. struct class_device *clttydev; /* 3270-class tty device ptr */
  41. struct class_device *cltubdev; /* 3270-class tub device ptr */
  42. struct raw3270_request init_request;
  43. unsigned char init_data[256];
  44. };
  45. /* raw3270->flags */
  46. #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
  47. #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
  48. #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
  49. #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
  50. #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
  51. /* Semaphore to protect global data of raw3270 (devices, views, etc). */
  52. static DEFINE_MUTEX(raw3270_mutex);
  53. /* List of 3270 devices. */
  54. static struct list_head raw3270_devices = LIST_HEAD_INIT(raw3270_devices);
  55. /*
  56. * Flag to indicate if the driver has been registered. Some operations
  57. * like waiting for the end of i/o need to be done differently as long
  58. * as the kernel is still starting up (console support).
  59. */
  60. static int raw3270_registered;
  61. /* Module parameters */
  62. static int tubxcorrect = 0;
  63. module_param(tubxcorrect, bool, 0);
  64. /*
  65. * Wait queue for device init/delete, view delete.
  66. */
  67. DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
  68. /*
  69. * Encode array for 12 bit 3270 addresses.
  70. */
  71. static unsigned char raw3270_ebcgraf[64] = {
  72. 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  73. 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  74. 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  75. 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  76. 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  77. 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  78. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  79. 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
  80. };
  81. void
  82. raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
  83. {
  84. if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
  85. cp[0] = (addr >> 8) & 0x3f;
  86. cp[1] = addr & 0xff;
  87. } else {
  88. cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
  89. cp[1] = raw3270_ebcgraf[addr & 0x3f];
  90. }
  91. }
  92. /*
  93. * Allocate a new 3270 ccw request
  94. */
  95. struct raw3270_request *
  96. raw3270_request_alloc(size_t size)
  97. {
  98. struct raw3270_request *rq;
  99. /* Allocate request structure */
  100. rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
  101. if (!rq)
  102. return ERR_PTR(-ENOMEM);
  103. /* alloc output buffer. */
  104. if (size > 0) {
  105. rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
  106. if (!rq->buffer) {
  107. kfree(rq);
  108. return ERR_PTR(-ENOMEM);
  109. }
  110. }
  111. rq->size = size;
  112. INIT_LIST_HEAD(&rq->list);
  113. /*
  114. * Setup ccw.
  115. */
  116. rq->ccw.cda = __pa(rq->buffer);
  117. rq->ccw.flags = CCW_FLAG_SLI;
  118. return rq;
  119. }
  120. #ifdef CONFIG_TN3270_CONSOLE
  121. /*
  122. * Allocate a new 3270 ccw request from bootmem. Only works very
  123. * early in the boot process. Only con3270.c should be using this.
  124. */
  125. struct raw3270_request *
  126. raw3270_request_alloc_bootmem(size_t size)
  127. {
  128. struct raw3270_request *rq;
  129. rq = alloc_bootmem_low(sizeof(struct raw3270));
  130. if (!rq)
  131. return ERR_PTR(-ENOMEM);
  132. memset(rq, 0, sizeof(struct raw3270_request));
  133. /* alloc output buffer. */
  134. if (size > 0) {
  135. rq->buffer = alloc_bootmem_low(size);
  136. if (!rq->buffer) {
  137. free_bootmem((unsigned long) rq,
  138. sizeof(struct raw3270));
  139. return ERR_PTR(-ENOMEM);
  140. }
  141. }
  142. rq->size = size;
  143. INIT_LIST_HEAD(&rq->list);
  144. /*
  145. * Setup ccw.
  146. */
  147. rq->ccw.cda = __pa(rq->buffer);
  148. rq->ccw.flags = CCW_FLAG_SLI;
  149. return rq;
  150. }
  151. #endif
  152. /*
  153. * Free 3270 ccw request
  154. */
  155. void
  156. raw3270_request_free (struct raw3270_request *rq)
  157. {
  158. kfree(rq->buffer);
  159. kfree(rq);
  160. }
  161. /*
  162. * Reset request to initial state.
  163. */
  164. void
  165. raw3270_request_reset(struct raw3270_request *rq)
  166. {
  167. BUG_ON(!list_empty(&rq->list));
  168. rq->ccw.cmd_code = 0;
  169. rq->ccw.count = 0;
  170. rq->ccw.cda = __pa(rq->buffer);
  171. rq->ccw.flags = CCW_FLAG_SLI;
  172. rq->rescnt = 0;
  173. rq->rc = 0;
  174. }
  175. /*
  176. * Set command code to ccw of a request.
  177. */
  178. void
  179. raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
  180. {
  181. rq->ccw.cmd_code = cmd;
  182. }
  183. /*
  184. * Add data fragment to output buffer.
  185. */
  186. int
  187. raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
  188. {
  189. if (size + rq->ccw.count > rq->size)
  190. return -E2BIG;
  191. memcpy(rq->buffer + rq->ccw.count, data, size);
  192. rq->ccw.count += size;
  193. return 0;
  194. }
  195. /*
  196. * Set address/length pair to ccw of a request.
  197. */
  198. void
  199. raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
  200. {
  201. rq->ccw.cda = __pa(data);
  202. rq->ccw.count = size;
  203. }
  204. /*
  205. * Set idal buffer to ccw of a request.
  206. */
  207. void
  208. raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
  209. {
  210. rq->ccw.cda = __pa(ib->data);
  211. rq->ccw.count = ib->size;
  212. rq->ccw.flags |= CCW_FLAG_IDA;
  213. }
  214. /*
  215. * Stop running ccw.
  216. */
  217. static int
  218. raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
  219. {
  220. int retries;
  221. int rc;
  222. if (raw3270_request_final(rq))
  223. return 0;
  224. /* Check if interrupt has already been processed */
  225. for (retries = 0; retries < 5; retries++) {
  226. if (retries < 2)
  227. rc = ccw_device_halt(rp->cdev, (long) rq);
  228. else
  229. rc = ccw_device_clear(rp->cdev, (long) rq);
  230. if (rc == 0)
  231. break; /* termination successful */
  232. }
  233. return rc;
  234. }
  235. static int
  236. raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
  237. {
  238. unsigned long flags;
  239. int rc;
  240. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  241. rc = raw3270_halt_io_nolock(rp, rq);
  242. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  243. return rc;
  244. }
  245. /*
  246. * Add the request to the request queue, try to start it if the
  247. * 3270 device is idle. Return without waiting for end of i/o.
  248. */
  249. static int
  250. __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
  251. struct raw3270_request *rq)
  252. {
  253. rq->view = view;
  254. raw3270_get_view(view);
  255. if (list_empty(&rp->req_queue) &&
  256. !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  257. /* No other requests are on the queue. Start this one. */
  258. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  259. (unsigned long) rq, 0, 0);
  260. if (rq->rc) {
  261. raw3270_put_view(view);
  262. return rq->rc;
  263. }
  264. }
  265. list_add_tail(&rq->list, &rp->req_queue);
  266. return 0;
  267. }
  268. int
  269. raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
  270. {
  271. unsigned long flags;
  272. struct raw3270 *rp;
  273. int rc;
  274. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  275. rp = view->dev;
  276. if (!rp || rp->view != view)
  277. rc = -EACCES;
  278. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  279. rc = -ENODEV;
  280. else
  281. rc = __raw3270_start(rp, view, rq);
  282. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  283. return rc;
  284. }
  285. int
  286. raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
  287. {
  288. struct raw3270 *rp;
  289. int rc;
  290. rp = view->dev;
  291. if (!rp || rp->view != view)
  292. rc = -EACCES;
  293. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  294. rc = -ENODEV;
  295. else
  296. rc = __raw3270_start(rp, view, rq);
  297. return rc;
  298. }
  299. int
  300. raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
  301. {
  302. struct raw3270 *rp;
  303. rp = view->dev;
  304. rq->view = view;
  305. raw3270_get_view(view);
  306. list_add_tail(&rq->list, &rp->req_queue);
  307. return 0;
  308. }
  309. /*
  310. * 3270 interrupt routine, called from the ccw_device layer
  311. */
  312. static void
  313. raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  314. {
  315. struct raw3270 *rp;
  316. struct raw3270_view *view;
  317. struct raw3270_request *rq;
  318. int rc;
  319. rp = (struct raw3270 *) cdev->dev.driver_data;
  320. if (!rp)
  321. return;
  322. rq = (struct raw3270_request *) intparm;
  323. view = rq ? rq->view : rp->view;
  324. if (IS_ERR(irb))
  325. rc = RAW3270_IO_RETRY;
  326. else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
  327. rq->rc = -EIO;
  328. rc = RAW3270_IO_DONE;
  329. } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
  330. DEV_STAT_UNIT_EXCEP)) {
  331. /* Handle CE-DE-UE and subsequent UDE */
  332. set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  333. rc = RAW3270_IO_BUSY;
  334. } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  335. /* Wait for UDE if busy flag is set. */
  336. if (irb->scsw.dstat & DEV_STAT_DEV_END) {
  337. clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  338. /* Got it, now retry. */
  339. rc = RAW3270_IO_RETRY;
  340. } else
  341. rc = RAW3270_IO_BUSY;
  342. } else if (view)
  343. rc = view->fn->intv(view, rq, irb);
  344. else
  345. rc = RAW3270_IO_DONE;
  346. switch (rc) {
  347. case RAW3270_IO_DONE:
  348. break;
  349. case RAW3270_IO_BUSY:
  350. /*
  351. * Intervention required by the operator. We have to wait
  352. * for unsolicited device end.
  353. */
  354. return;
  355. case RAW3270_IO_RETRY:
  356. if (!rq)
  357. break;
  358. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  359. (unsigned long) rq, 0, 0);
  360. if (rq->rc == 0)
  361. return; /* Sucessfully restarted. */
  362. break;
  363. case RAW3270_IO_STOP:
  364. if (!rq)
  365. break;
  366. raw3270_halt_io_nolock(rp, rq);
  367. rq->rc = -EIO;
  368. break;
  369. default:
  370. BUG();
  371. }
  372. if (rq) {
  373. BUG_ON(list_empty(&rq->list));
  374. /* The request completed, remove from queue and do callback. */
  375. list_del_init(&rq->list);
  376. if (rq->callback)
  377. rq->callback(rq, rq->callback_data);
  378. /* Do put_device for get_device in raw3270_start. */
  379. raw3270_put_view(view);
  380. }
  381. /*
  382. * Try to start each request on request queue until one is
  383. * started successful.
  384. */
  385. while (!list_empty(&rp->req_queue)) {
  386. rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
  387. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  388. (unsigned long) rq, 0, 0);
  389. if (rq->rc == 0)
  390. break;
  391. /* Start failed. Remove request and do callback. */
  392. list_del_init(&rq->list);
  393. if (rq->callback)
  394. rq->callback(rq, rq->callback_data);
  395. /* Do put_device for get_device in raw3270_start. */
  396. raw3270_put_view(view);
  397. }
  398. }
  399. /*
  400. * Size sensing.
  401. */
  402. struct raw3270_ua { /* Query Reply structure for Usable Area */
  403. struct { /* Usable Area Query Reply Base */
  404. short l; /* Length of this structured field */
  405. char sfid; /* 0x81 if Query Reply */
  406. char qcode; /* 0x81 if Usable Area */
  407. char flags0;
  408. char flags1;
  409. short w; /* Width of usable area */
  410. short h; /* Heigth of usavle area */
  411. char units; /* 0x00:in; 0x01:mm */
  412. int xr;
  413. int yr;
  414. char aw;
  415. char ah;
  416. short buffsz; /* Character buffer size, bytes */
  417. char xmin;
  418. char ymin;
  419. char xmax;
  420. char ymax;
  421. } __attribute__ ((packed)) uab;
  422. struct { /* Alternate Usable Area Self-Defining Parameter */
  423. char l; /* Length of this Self-Defining Parm */
  424. char sdpid; /* 0x02 if Alternate Usable Area */
  425. char res;
  426. char auaid; /* 0x01 is Id for the A U A */
  427. short wauai; /* Width of AUAi */
  428. short hauai; /* Height of AUAi */
  429. char auaunits; /* 0x00:in, 0x01:mm */
  430. int auaxr;
  431. int auayr;
  432. char awauai;
  433. char ahauai;
  434. } __attribute__ ((packed)) aua;
  435. } __attribute__ ((packed));
  436. static struct diag210 raw3270_init_diag210;
  437. static DECLARE_MUTEX(raw3270_init_sem);
  438. static int
  439. raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
  440. struct irb *irb)
  441. {
  442. /*
  443. * Unit-Check Processing:
  444. * Expect Command Reject or Intervention Required.
  445. */
  446. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
  447. /* Request finished abnormally. */
  448. if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
  449. set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
  450. return RAW3270_IO_BUSY;
  451. }
  452. }
  453. if (rq) {
  454. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
  455. if (irb->ecw[0] & SNS0_CMD_REJECT)
  456. rq->rc = -EOPNOTSUPP;
  457. else
  458. rq->rc = -EIO;
  459. } else
  460. /* Request finished normally. Copy residual count. */
  461. rq->rescnt = irb->scsw.count;
  462. }
  463. if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
  464. set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
  465. wake_up(&raw3270_wait_queue);
  466. }
  467. return RAW3270_IO_DONE;
  468. }
  469. static struct raw3270_fn raw3270_init_fn = {
  470. .intv = raw3270_init_irq
  471. };
  472. static struct raw3270_view raw3270_init_view = {
  473. .fn = &raw3270_init_fn
  474. };
  475. /*
  476. * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
  477. * Wait for end of request. The request must have been started
  478. * with raw3270_start, rc = 0. The device lock may NOT have been
  479. * released between calling raw3270_start and raw3270_wait.
  480. */
  481. static void
  482. raw3270_wake_init(struct raw3270_request *rq, void *data)
  483. {
  484. wake_up((wait_queue_head_t *) data);
  485. }
  486. /*
  487. * Special wait function that can cope with console initialization.
  488. */
  489. static int
  490. raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
  491. struct raw3270_request *rq)
  492. {
  493. unsigned long flags;
  494. wait_queue_head_t wq;
  495. int rc;
  496. #ifdef CONFIG_TN3270_CONSOLE
  497. if (raw3270_registered == 0) {
  498. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  499. rq->callback = NULL;
  500. rc = __raw3270_start(rp, view, rq);
  501. if (rc == 0)
  502. while (!raw3270_request_final(rq)) {
  503. wait_cons_dev();
  504. barrier();
  505. }
  506. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  507. return rq->rc;
  508. }
  509. #endif
  510. init_waitqueue_head(&wq);
  511. rq->callback = raw3270_wake_init;
  512. rq->callback_data = &wq;
  513. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  514. rc = __raw3270_start(rp, view, rq);
  515. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  516. if (rc)
  517. return rc;
  518. /* Now wait for the completion. */
  519. rc = wait_event_interruptible(wq, raw3270_request_final(rq));
  520. if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
  521. raw3270_halt_io(view->dev, rq);
  522. /* No wait for the halt to complete. */
  523. wait_event(wq, raw3270_request_final(rq));
  524. return -ERESTARTSYS;
  525. }
  526. return rq->rc;
  527. }
  528. static int
  529. __raw3270_size_device_vm(struct raw3270 *rp)
  530. {
  531. int rc, model;
  532. raw3270_init_diag210.vrdcdvno =
  533. _ccw_device_get_device_number(rp->cdev);
  534. raw3270_init_diag210.vrdclen = sizeof(struct diag210);
  535. rc = diag210(&raw3270_init_diag210);
  536. if (rc)
  537. return rc;
  538. model = raw3270_init_diag210.vrdccrmd;
  539. switch (model) {
  540. case 2:
  541. rp->model = model;
  542. rp->rows = 24;
  543. rp->cols = 80;
  544. break;
  545. case 3:
  546. rp->model = model;
  547. rp->rows = 32;
  548. rp->cols = 80;
  549. break;
  550. case 4:
  551. rp->model = model;
  552. rp->rows = 43;
  553. rp->cols = 80;
  554. break;
  555. case 5:
  556. rp->model = model;
  557. rp->rows = 27;
  558. rp->cols = 132;
  559. break;
  560. default:
  561. printk(KERN_WARNING "vrdccrmd is 0x%.8x\n", model);
  562. rc = -EOPNOTSUPP;
  563. break;
  564. }
  565. return rc;
  566. }
  567. static int
  568. __raw3270_size_device(struct raw3270 *rp)
  569. {
  570. static const unsigned char wbuf[] =
  571. { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
  572. struct raw3270_ua *uap;
  573. unsigned short count;
  574. int rc;
  575. /*
  576. * To determine the size of the 3270 device we need to do:
  577. * 1) send a 'read partition' data stream to the device
  578. * 2) wait for the attn interrupt that preceeds the query reply
  579. * 3) do a read modified to get the query reply
  580. * To make things worse we have to cope with intervention
  581. * required (3270 device switched to 'stand-by') and command
  582. * rejects (old devices that can't do 'read partition').
  583. */
  584. memset(&rp->init_request, 0, sizeof(rp->init_request));
  585. memset(&rp->init_data, 0, 256);
  586. /* Store 'read partition' data stream to init_data */
  587. memcpy(&rp->init_data, wbuf, sizeof(wbuf));
  588. INIT_LIST_HEAD(&rp->init_request.list);
  589. rp->init_request.ccw.cmd_code = TC_WRITESF;
  590. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  591. rp->init_request.ccw.count = sizeof(wbuf);
  592. rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
  593. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  594. if (rc)
  595. /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
  596. return rc;
  597. /* Wait for attention interrupt. */
  598. #ifdef CONFIG_TN3270_CONSOLE
  599. if (raw3270_registered == 0) {
  600. unsigned long flags;
  601. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  602. while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
  603. wait_cons_dev();
  604. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  605. } else
  606. #endif
  607. rc = wait_event_interruptible(raw3270_wait_queue,
  608. test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
  609. if (rc)
  610. return rc;
  611. /*
  612. * The device accepted the 'read partition' command. Now
  613. * set up a read ccw and issue it.
  614. */
  615. rp->init_request.ccw.cmd_code = TC_READMOD;
  616. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  617. rp->init_request.ccw.count = sizeof(rp->init_data);
  618. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  619. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  620. if (rc)
  621. return rc;
  622. /* Got a Query Reply */
  623. count = sizeof(rp->init_data) - rp->init_request.rescnt;
  624. uap = (struct raw3270_ua *) (rp->init_data + 1);
  625. /* Paranoia check. */
  626. if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
  627. return -EOPNOTSUPP;
  628. /* Copy rows/columns of default Usable Area */
  629. rp->rows = uap->uab.h;
  630. rp->cols = uap->uab.w;
  631. /* Check for 14 bit addressing */
  632. if ((uap->uab.flags0 & 0x0d) == 0x01)
  633. set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
  634. /* Check for Alternate Usable Area */
  635. if (uap->uab.l == sizeof(struct raw3270_ua) &&
  636. uap->aua.sdpid == 0x02) {
  637. rp->rows = uap->aua.hauai;
  638. rp->cols = uap->aua.wauai;
  639. }
  640. return 0;
  641. }
  642. static int
  643. raw3270_size_device(struct raw3270 *rp)
  644. {
  645. int rc;
  646. down(&raw3270_init_sem);
  647. rp->view = &raw3270_init_view;
  648. raw3270_init_view.dev = rp;
  649. if (MACHINE_IS_VM)
  650. rc = __raw3270_size_device_vm(rp);
  651. else
  652. rc = __raw3270_size_device(rp);
  653. raw3270_init_view.dev = NULL;
  654. rp->view = NULL;
  655. up(&raw3270_init_sem);
  656. if (rc == 0) { /* Found something. */
  657. /* Try to find a model. */
  658. rp->model = 0;
  659. if (rp->rows == 24 && rp->cols == 80)
  660. rp->model = 2;
  661. if (rp->rows == 32 && rp->cols == 80)
  662. rp->model = 3;
  663. if (rp->rows == 43 && rp->cols == 80)
  664. rp->model = 4;
  665. if (rp->rows == 27 && rp->cols == 132)
  666. rp->model = 5;
  667. } else {
  668. /* Couldn't detect size. Use default model 2. */
  669. rp->model = 2;
  670. rp->rows = 24;
  671. rp->cols = 80;
  672. return 0;
  673. }
  674. return rc;
  675. }
  676. static int
  677. raw3270_reset_device(struct raw3270 *rp)
  678. {
  679. int rc;
  680. down(&raw3270_init_sem);
  681. memset(&rp->init_request, 0, sizeof(rp->init_request));
  682. memset(&rp->init_data, 0, sizeof(rp->init_data));
  683. /* Store reset data stream to init_data/init_request */
  684. rp->init_data[0] = TW_KR;
  685. INIT_LIST_HEAD(&rp->init_request.list);
  686. rp->init_request.ccw.cmd_code = TC_EWRITEA;
  687. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  688. rp->init_request.ccw.count = 1;
  689. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  690. rp->view = &raw3270_init_view;
  691. raw3270_init_view.dev = rp;
  692. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  693. raw3270_init_view.dev = NULL;
  694. rp->view = NULL;
  695. up(&raw3270_init_sem);
  696. return rc;
  697. }
  698. int
  699. raw3270_reset(struct raw3270_view *view)
  700. {
  701. struct raw3270 *rp;
  702. int rc;
  703. rp = view->dev;
  704. if (!rp || rp->view != view)
  705. rc = -EACCES;
  706. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  707. rc = -ENODEV;
  708. else
  709. rc = raw3270_reset_device(view->dev);
  710. return rc;
  711. }
  712. /*
  713. * Setup new 3270 device.
  714. */
  715. static int
  716. raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
  717. {
  718. struct list_head *l;
  719. struct raw3270 *tmp;
  720. int minor;
  721. memset(rp, 0, sizeof(struct raw3270));
  722. /* Copy ebcdic -> ascii translation table. */
  723. memcpy(ascebc, _ascebc, 256);
  724. if (tubxcorrect) {
  725. /* correct brackets and circumflex */
  726. ascebc['['] = 0xad;
  727. ascebc[']'] = 0xbd;
  728. ascebc['^'] = 0xb0;
  729. }
  730. rp->ascebc = ascebc;
  731. /* Set defaults. */
  732. rp->rows = 24;
  733. rp->cols = 80;
  734. INIT_LIST_HEAD(&rp->req_queue);
  735. INIT_LIST_HEAD(&rp->view_list);
  736. /*
  737. * Add device to list and find the smallest unused minor
  738. * number for it. Note: there is no device with minor 0,
  739. * see special case for fs3270.c:fs3270_open().
  740. */
  741. mutex_lock(&raw3270_mutex);
  742. /* Keep the list sorted. */
  743. minor = RAW3270_FIRSTMINOR;
  744. rp->minor = -1;
  745. list_for_each(l, &raw3270_devices) {
  746. tmp = list_entry(l, struct raw3270, list);
  747. if (tmp->minor > minor) {
  748. rp->minor = minor;
  749. __list_add(&rp->list, l->prev, l);
  750. break;
  751. }
  752. minor++;
  753. }
  754. if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
  755. rp->minor = minor;
  756. list_add_tail(&rp->list, &raw3270_devices);
  757. }
  758. mutex_unlock(&raw3270_mutex);
  759. /* No free minor number? Then give up. */
  760. if (rp->minor == -1)
  761. return -EUSERS;
  762. rp->cdev = cdev;
  763. cdev->dev.driver_data = rp;
  764. cdev->handler = raw3270_irq;
  765. return 0;
  766. }
  767. #ifdef CONFIG_TN3270_CONSOLE
  768. /*
  769. * Setup 3270 device configured as console.
  770. */
  771. struct raw3270 *
  772. raw3270_setup_console(struct ccw_device *cdev)
  773. {
  774. struct raw3270 *rp;
  775. char *ascebc;
  776. int rc;
  777. rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
  778. ascebc = (char *) alloc_bootmem(256);
  779. rc = raw3270_setup_device(cdev, rp, ascebc);
  780. if (rc)
  781. return ERR_PTR(rc);
  782. set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
  783. rc = raw3270_reset_device(rp);
  784. if (rc)
  785. return ERR_PTR(rc);
  786. rc = raw3270_size_device(rp);
  787. if (rc)
  788. return ERR_PTR(rc);
  789. rc = raw3270_reset_device(rp);
  790. if (rc)
  791. return ERR_PTR(rc);
  792. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  793. return rp;
  794. }
  795. void
  796. raw3270_wait_cons_dev(struct raw3270 *rp)
  797. {
  798. unsigned long flags;
  799. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  800. wait_cons_dev();
  801. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  802. }
  803. #endif
  804. /*
  805. * Create a 3270 device structure.
  806. */
  807. static struct raw3270 *
  808. raw3270_create_device(struct ccw_device *cdev)
  809. {
  810. struct raw3270 *rp;
  811. char *ascebc;
  812. int rc;
  813. rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  814. if (!rp)
  815. return ERR_PTR(-ENOMEM);
  816. ascebc = kmalloc(256, GFP_KERNEL);
  817. if (!ascebc) {
  818. kfree(rp);
  819. return ERR_PTR(-ENOMEM);
  820. }
  821. rc = raw3270_setup_device(cdev, rp, ascebc);
  822. if (rc) {
  823. kfree(rp->ascebc);
  824. kfree(rp);
  825. rp = ERR_PTR(rc);
  826. }
  827. /* Get reference to ccw_device structure. */
  828. get_device(&cdev->dev);
  829. return rp;
  830. }
  831. /*
  832. * Activate a view.
  833. */
  834. int
  835. raw3270_activate_view(struct raw3270_view *view)
  836. {
  837. struct raw3270 *rp;
  838. struct raw3270_view *oldview, *nv;
  839. unsigned long flags;
  840. int rc;
  841. rp = view->dev;
  842. if (!rp)
  843. return -ENODEV;
  844. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  845. if (rp->view == view)
  846. rc = 0;
  847. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  848. rc = -ENODEV;
  849. else {
  850. oldview = NULL;
  851. if (rp->view) {
  852. oldview = rp->view;
  853. oldview->fn->deactivate(oldview);
  854. }
  855. rp->view = view;
  856. rc = view->fn->activate(view);
  857. if (rc) {
  858. /* Didn't work. Try to reactivate the old view. */
  859. rp->view = oldview;
  860. if (!oldview || oldview->fn->activate(oldview) != 0) {
  861. /* Didn't work as well. Try any other view. */
  862. list_for_each_entry(nv, &rp->view_list, list)
  863. if (nv != view && nv != oldview) {
  864. rp->view = nv;
  865. if (nv->fn->activate(nv) == 0)
  866. break;
  867. rp->view = NULL;
  868. }
  869. }
  870. }
  871. }
  872. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  873. return rc;
  874. }
  875. /*
  876. * Deactivate current view.
  877. */
  878. void
  879. raw3270_deactivate_view(struct raw3270_view *view)
  880. {
  881. unsigned long flags;
  882. struct raw3270 *rp;
  883. rp = view->dev;
  884. if (!rp)
  885. return;
  886. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  887. if (rp->view == view) {
  888. view->fn->deactivate(view);
  889. rp->view = NULL;
  890. /* Move deactivated view to end of list. */
  891. list_del_init(&view->list);
  892. list_add_tail(&view->list, &rp->view_list);
  893. /* Try to activate another view. */
  894. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  895. list_for_each_entry(view, &rp->view_list, list) {
  896. rp->view = view;
  897. if (view->fn->activate(view) == 0)
  898. break;
  899. rp->view = NULL;
  900. }
  901. }
  902. }
  903. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  904. }
  905. /*
  906. * Add view to device with minor "minor".
  907. */
  908. int
  909. raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
  910. {
  911. unsigned long flags;
  912. struct raw3270 *rp;
  913. int rc;
  914. if (minor <= 0)
  915. return -ENODEV;
  916. mutex_lock(&raw3270_mutex);
  917. rc = -ENODEV;
  918. list_for_each_entry(rp, &raw3270_devices, list) {
  919. if (rp->minor != minor)
  920. continue;
  921. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  922. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  923. atomic_set(&view->ref_count, 2);
  924. view->dev = rp;
  925. view->fn = fn;
  926. view->model = rp->model;
  927. view->rows = rp->rows;
  928. view->cols = rp->cols;
  929. view->ascebc = rp->ascebc;
  930. spin_lock_init(&view->lock);
  931. list_add(&view->list, &rp->view_list);
  932. rc = 0;
  933. }
  934. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  935. break;
  936. }
  937. mutex_unlock(&raw3270_mutex);
  938. return rc;
  939. }
  940. /*
  941. * Find specific view of device with minor "minor".
  942. */
  943. struct raw3270_view *
  944. raw3270_find_view(struct raw3270_fn *fn, int minor)
  945. {
  946. struct raw3270 *rp;
  947. struct raw3270_view *view, *tmp;
  948. unsigned long flags;
  949. mutex_lock(&raw3270_mutex);
  950. view = ERR_PTR(-ENODEV);
  951. list_for_each_entry(rp, &raw3270_devices, list) {
  952. if (rp->minor != minor)
  953. continue;
  954. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  955. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  956. view = ERR_PTR(-ENOENT);
  957. list_for_each_entry(tmp, &rp->view_list, list) {
  958. if (tmp->fn == fn) {
  959. raw3270_get_view(tmp);
  960. view = tmp;
  961. break;
  962. }
  963. }
  964. }
  965. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  966. break;
  967. }
  968. mutex_unlock(&raw3270_mutex);
  969. return view;
  970. }
  971. /*
  972. * Remove view from device and free view structure via call to view->fn->free.
  973. */
  974. void
  975. raw3270_del_view(struct raw3270_view *view)
  976. {
  977. unsigned long flags;
  978. struct raw3270 *rp;
  979. struct raw3270_view *nv;
  980. rp = view->dev;
  981. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  982. if (rp->view == view) {
  983. view->fn->deactivate(view);
  984. rp->view = NULL;
  985. }
  986. list_del_init(&view->list);
  987. if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  988. /* Try to activate another view. */
  989. list_for_each_entry(nv, &rp->view_list, list) {
  990. if (nv->fn->activate(nv) == 0) {
  991. rp->view = nv;
  992. break;
  993. }
  994. }
  995. }
  996. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  997. /* Wait for reference counter to drop to zero. */
  998. atomic_dec(&view->ref_count);
  999. wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
  1000. if (view->fn->free)
  1001. view->fn->free(view);
  1002. }
  1003. /*
  1004. * Remove a 3270 device structure.
  1005. */
  1006. static void
  1007. raw3270_delete_device(struct raw3270 *rp)
  1008. {
  1009. struct ccw_device *cdev;
  1010. /* Remove from device chain. */
  1011. mutex_lock(&raw3270_mutex);
  1012. if (rp->clttydev && !IS_ERR(rp->clttydev))
  1013. class_device_destroy(class3270,
  1014. MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1015. if (rp->cltubdev && !IS_ERR(rp->cltubdev))
  1016. class_device_destroy(class3270,
  1017. MKDEV(IBM_FS3270_MAJOR, rp->minor));
  1018. list_del_init(&rp->list);
  1019. mutex_unlock(&raw3270_mutex);
  1020. /* Disconnect from ccw_device. */
  1021. cdev = rp->cdev;
  1022. rp->cdev = NULL;
  1023. cdev->dev.driver_data = NULL;
  1024. cdev->handler = NULL;
  1025. /* Put ccw_device structure. */
  1026. put_device(&cdev->dev);
  1027. /* Now free raw3270 structure. */
  1028. kfree(rp->ascebc);
  1029. kfree(rp);
  1030. }
  1031. static int
  1032. raw3270_probe (struct ccw_device *cdev)
  1033. {
  1034. return 0;
  1035. }
  1036. /*
  1037. * Additional attributes for a 3270 device
  1038. */
  1039. static ssize_t
  1040. raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
  1041. {
  1042. return snprintf(buf, PAGE_SIZE, "%i\n",
  1043. ((struct raw3270 *) dev->driver_data)->model);
  1044. }
  1045. static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
  1046. static ssize_t
  1047. raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
  1048. {
  1049. return snprintf(buf, PAGE_SIZE, "%i\n",
  1050. ((struct raw3270 *) dev->driver_data)->rows);
  1051. }
  1052. static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
  1053. static ssize_t
  1054. raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
  1055. {
  1056. return snprintf(buf, PAGE_SIZE, "%i\n",
  1057. ((struct raw3270 *) dev->driver_data)->cols);
  1058. }
  1059. static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
  1060. static struct attribute * raw3270_attrs[] = {
  1061. &dev_attr_model.attr,
  1062. &dev_attr_rows.attr,
  1063. &dev_attr_columns.attr,
  1064. NULL,
  1065. };
  1066. static struct attribute_group raw3270_attr_group = {
  1067. .attrs = raw3270_attrs,
  1068. };
  1069. static int raw3270_create_attributes(struct raw3270 *rp)
  1070. {
  1071. int rc;
  1072. rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1073. if (rc)
  1074. goto out;
  1075. rp->clttydev = class_device_create(class3270, NULL,
  1076. MKDEV(IBM_TTY3270_MAJOR, rp->minor),
  1077. &rp->cdev->dev, "tty%s",
  1078. rp->cdev->dev.bus_id);
  1079. if (IS_ERR(rp->clttydev)) {
  1080. rc = PTR_ERR(rp->clttydev);
  1081. goto out_ttydev;
  1082. }
  1083. rp->cltubdev = class_device_create(class3270, NULL,
  1084. MKDEV(IBM_FS3270_MAJOR, rp->minor),
  1085. &rp->cdev->dev, "tub%s",
  1086. rp->cdev->dev.bus_id);
  1087. if (!IS_ERR(rp->cltubdev))
  1088. goto out;
  1089. rc = PTR_ERR(rp->cltubdev);
  1090. class_device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1091. out_ttydev:
  1092. sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1093. out:
  1094. return rc;
  1095. }
  1096. /*
  1097. * Notifier for device addition/removal
  1098. */
  1099. struct raw3270_notifier {
  1100. struct list_head list;
  1101. void (*notifier)(int, int);
  1102. };
  1103. static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
  1104. int raw3270_register_notifier(void (*notifier)(int, int))
  1105. {
  1106. struct raw3270_notifier *np;
  1107. struct raw3270 *rp;
  1108. np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
  1109. if (!np)
  1110. return -ENOMEM;
  1111. np->notifier = notifier;
  1112. mutex_lock(&raw3270_mutex);
  1113. list_add_tail(&np->list, &raw3270_notifier);
  1114. list_for_each_entry(rp, &raw3270_devices, list) {
  1115. get_device(&rp->cdev->dev);
  1116. notifier(rp->minor, 1);
  1117. }
  1118. mutex_unlock(&raw3270_mutex);
  1119. return 0;
  1120. }
  1121. void raw3270_unregister_notifier(void (*notifier)(int, int))
  1122. {
  1123. struct raw3270_notifier *np;
  1124. mutex_lock(&raw3270_mutex);
  1125. list_for_each_entry(np, &raw3270_notifier, list)
  1126. if (np->notifier == notifier) {
  1127. list_del(&np->list);
  1128. kfree(np);
  1129. break;
  1130. }
  1131. mutex_unlock(&raw3270_mutex);
  1132. }
  1133. /*
  1134. * Set 3270 device online.
  1135. */
  1136. static int
  1137. raw3270_set_online (struct ccw_device *cdev)
  1138. {
  1139. struct raw3270 *rp;
  1140. struct raw3270_notifier *np;
  1141. int rc;
  1142. rp = raw3270_create_device(cdev);
  1143. if (IS_ERR(rp))
  1144. return PTR_ERR(rp);
  1145. rc = raw3270_reset_device(rp);
  1146. if (rc)
  1147. goto failure;
  1148. rc = raw3270_size_device(rp);
  1149. if (rc)
  1150. goto failure;
  1151. rc = raw3270_reset_device(rp);
  1152. if (rc)
  1153. goto failure;
  1154. rc = raw3270_create_attributes(rp);
  1155. if (rc)
  1156. goto failure;
  1157. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  1158. mutex_lock(&raw3270_mutex);
  1159. list_for_each_entry(np, &raw3270_notifier, list)
  1160. np->notifier(rp->minor, 1);
  1161. mutex_unlock(&raw3270_mutex);
  1162. return 0;
  1163. failure:
  1164. raw3270_delete_device(rp);
  1165. return rc;
  1166. }
  1167. /*
  1168. * Remove 3270 device structure.
  1169. */
  1170. static void
  1171. raw3270_remove (struct ccw_device *cdev)
  1172. {
  1173. unsigned long flags;
  1174. struct raw3270 *rp;
  1175. struct raw3270_view *v;
  1176. struct raw3270_notifier *np;
  1177. rp = cdev->dev.driver_data;
  1178. /*
  1179. * _remove is the opposite of _probe; it's probe that
  1180. * should set up rp. raw3270_remove gets entered for
  1181. * devices even if they haven't been varied online.
  1182. * Thus, rp may validly be NULL here.
  1183. */
  1184. if (rp == NULL)
  1185. return;
  1186. clear_bit(RAW3270_FLAGS_READY, &rp->flags);
  1187. sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
  1188. /* Deactivate current view and remove all views. */
  1189. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1190. if (rp->view) {
  1191. rp->view->fn->deactivate(rp->view);
  1192. rp->view = NULL;
  1193. }
  1194. while (!list_empty(&rp->view_list)) {
  1195. v = list_entry(rp->view_list.next, struct raw3270_view, list);
  1196. if (v->fn->release)
  1197. v->fn->release(v);
  1198. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1199. raw3270_del_view(v);
  1200. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1201. }
  1202. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1203. mutex_lock(&raw3270_mutex);
  1204. list_for_each_entry(np, &raw3270_notifier, list)
  1205. np->notifier(rp->minor, 0);
  1206. mutex_unlock(&raw3270_mutex);
  1207. /* Reset 3270 device. */
  1208. raw3270_reset_device(rp);
  1209. /* And finally remove it. */
  1210. raw3270_delete_device(rp);
  1211. }
  1212. /*
  1213. * Set 3270 device offline.
  1214. */
  1215. static int
  1216. raw3270_set_offline (struct ccw_device *cdev)
  1217. {
  1218. struct raw3270 *rp;
  1219. rp = cdev->dev.driver_data;
  1220. if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
  1221. return -EBUSY;
  1222. raw3270_remove(cdev);
  1223. return 0;
  1224. }
  1225. static struct ccw_device_id raw3270_id[] = {
  1226. { CCW_DEVICE(0x3270, 0) },
  1227. { CCW_DEVICE(0x3271, 0) },
  1228. { CCW_DEVICE(0x3272, 0) },
  1229. { CCW_DEVICE(0x3273, 0) },
  1230. { CCW_DEVICE(0x3274, 0) },
  1231. { CCW_DEVICE(0x3275, 0) },
  1232. { CCW_DEVICE(0x3276, 0) },
  1233. { CCW_DEVICE(0x3277, 0) },
  1234. { CCW_DEVICE(0x3278, 0) },
  1235. { CCW_DEVICE(0x3279, 0) },
  1236. { CCW_DEVICE(0x3174, 0) },
  1237. { /* end of list */ },
  1238. };
  1239. static struct ccw_driver raw3270_ccw_driver = {
  1240. .name = "3270",
  1241. .owner = THIS_MODULE,
  1242. .ids = raw3270_id,
  1243. .probe = &raw3270_probe,
  1244. .remove = &raw3270_remove,
  1245. .set_online = &raw3270_set_online,
  1246. .set_offline = &raw3270_set_offline,
  1247. };
  1248. static int
  1249. raw3270_init(void)
  1250. {
  1251. struct raw3270 *rp;
  1252. int rc;
  1253. if (raw3270_registered)
  1254. return 0;
  1255. raw3270_registered = 1;
  1256. rc = ccw_driver_register(&raw3270_ccw_driver);
  1257. if (rc == 0) {
  1258. /* Create attributes for early (= console) device. */
  1259. mutex_lock(&raw3270_mutex);
  1260. class3270 = class_create(THIS_MODULE, "3270");
  1261. list_for_each_entry(rp, &raw3270_devices, list) {
  1262. get_device(&rp->cdev->dev);
  1263. raw3270_create_attributes(rp);
  1264. }
  1265. mutex_unlock(&raw3270_mutex);
  1266. }
  1267. return rc;
  1268. }
  1269. static void
  1270. raw3270_exit(void)
  1271. {
  1272. ccw_driver_unregister(&raw3270_ccw_driver);
  1273. class_destroy(class3270);
  1274. }
  1275. MODULE_LICENSE("GPL");
  1276. module_init(raw3270_init);
  1277. module_exit(raw3270_exit);
  1278. EXPORT_SYMBOL(raw3270_request_alloc);
  1279. EXPORT_SYMBOL(raw3270_request_free);
  1280. EXPORT_SYMBOL(raw3270_request_reset);
  1281. EXPORT_SYMBOL(raw3270_request_set_cmd);
  1282. EXPORT_SYMBOL(raw3270_request_add_data);
  1283. EXPORT_SYMBOL(raw3270_request_set_data);
  1284. EXPORT_SYMBOL(raw3270_request_set_idal);
  1285. EXPORT_SYMBOL(raw3270_buffer_address);
  1286. EXPORT_SYMBOL(raw3270_add_view);
  1287. EXPORT_SYMBOL(raw3270_del_view);
  1288. EXPORT_SYMBOL(raw3270_find_view);
  1289. EXPORT_SYMBOL(raw3270_activate_view);
  1290. EXPORT_SYMBOL(raw3270_deactivate_view);
  1291. EXPORT_SYMBOL(raw3270_start);
  1292. EXPORT_SYMBOL(raw3270_start_locked);
  1293. EXPORT_SYMBOL(raw3270_start_irq);
  1294. EXPORT_SYMBOL(raw3270_reset);
  1295. EXPORT_SYMBOL(raw3270_register_notifier);
  1296. EXPORT_SYMBOL(raw3270_unregister_notifier);
  1297. EXPORT_SYMBOL(raw3270_wait_queue);