raw3270.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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 __init *raw3270_request_alloc_bootmem(size_t size)
  126. {
  127. struct raw3270_request *rq;
  128. rq = alloc_bootmem_low(sizeof(struct raw3270));
  129. if (!rq)
  130. return ERR_PTR(-ENOMEM);
  131. memset(rq, 0, sizeof(struct raw3270_request));
  132. /* alloc output buffer. */
  133. if (size > 0) {
  134. rq->buffer = alloc_bootmem_low(size);
  135. if (!rq->buffer) {
  136. free_bootmem((unsigned long) rq,
  137. sizeof(struct raw3270));
  138. return ERR_PTR(-ENOMEM);
  139. }
  140. }
  141. rq->size = size;
  142. INIT_LIST_HEAD(&rq->list);
  143. /*
  144. * Setup ccw.
  145. */
  146. rq->ccw.cda = __pa(rq->buffer);
  147. rq->ccw.flags = CCW_FLAG_SLI;
  148. return rq;
  149. }
  150. #endif
  151. /*
  152. * Free 3270 ccw request
  153. */
  154. void
  155. raw3270_request_free (struct raw3270_request *rq)
  156. {
  157. kfree(rq->buffer);
  158. kfree(rq);
  159. }
  160. /*
  161. * Reset request to initial state.
  162. */
  163. void
  164. raw3270_request_reset(struct raw3270_request *rq)
  165. {
  166. BUG_ON(!list_empty(&rq->list));
  167. rq->ccw.cmd_code = 0;
  168. rq->ccw.count = 0;
  169. rq->ccw.cda = __pa(rq->buffer);
  170. rq->ccw.flags = CCW_FLAG_SLI;
  171. rq->rescnt = 0;
  172. rq->rc = 0;
  173. }
  174. /*
  175. * Set command code to ccw of a request.
  176. */
  177. void
  178. raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
  179. {
  180. rq->ccw.cmd_code = cmd;
  181. }
  182. /*
  183. * Add data fragment to output buffer.
  184. */
  185. int
  186. raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
  187. {
  188. if (size + rq->ccw.count > rq->size)
  189. return -E2BIG;
  190. memcpy(rq->buffer + rq->ccw.count, data, size);
  191. rq->ccw.count += size;
  192. return 0;
  193. }
  194. /*
  195. * Set address/length pair to ccw of a request.
  196. */
  197. void
  198. raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
  199. {
  200. rq->ccw.cda = __pa(data);
  201. rq->ccw.count = size;
  202. }
  203. /*
  204. * Set idal buffer to ccw of a request.
  205. */
  206. void
  207. raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
  208. {
  209. rq->ccw.cda = __pa(ib->data);
  210. rq->ccw.count = ib->size;
  211. rq->ccw.flags |= CCW_FLAG_IDA;
  212. }
  213. /*
  214. * Stop running ccw.
  215. */
  216. static int
  217. raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
  218. {
  219. int retries;
  220. int rc;
  221. if (raw3270_request_final(rq))
  222. return 0;
  223. /* Check if interrupt has already been processed */
  224. for (retries = 0; retries < 5; retries++) {
  225. if (retries < 2)
  226. rc = ccw_device_halt(rp->cdev, (long) rq);
  227. else
  228. rc = ccw_device_clear(rp->cdev, (long) rq);
  229. if (rc == 0)
  230. break; /* termination successful */
  231. }
  232. return rc;
  233. }
  234. static int
  235. raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
  236. {
  237. unsigned long flags;
  238. int rc;
  239. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  240. rc = raw3270_halt_io_nolock(rp, rq);
  241. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  242. return rc;
  243. }
  244. /*
  245. * Add the request to the request queue, try to start it if the
  246. * 3270 device is idle. Return without waiting for end of i/o.
  247. */
  248. static int
  249. __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
  250. struct raw3270_request *rq)
  251. {
  252. rq->view = view;
  253. raw3270_get_view(view);
  254. if (list_empty(&rp->req_queue) &&
  255. !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  256. /* No other requests are on the queue. Start this one. */
  257. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  258. (unsigned long) rq, 0, 0);
  259. if (rq->rc) {
  260. raw3270_put_view(view);
  261. return rq->rc;
  262. }
  263. }
  264. list_add_tail(&rq->list, &rp->req_queue);
  265. return 0;
  266. }
  267. int
  268. raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
  269. {
  270. unsigned long flags;
  271. struct raw3270 *rp;
  272. int rc;
  273. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  274. rp = view->dev;
  275. if (!rp || rp->view != view)
  276. rc = -EACCES;
  277. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  278. rc = -ENODEV;
  279. else
  280. rc = __raw3270_start(rp, view, rq);
  281. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  282. return rc;
  283. }
  284. int
  285. raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
  286. {
  287. struct raw3270 *rp;
  288. int rc;
  289. rp = view->dev;
  290. if (!rp || rp->view != view)
  291. rc = -EACCES;
  292. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  293. rc = -ENODEV;
  294. else
  295. rc = __raw3270_start(rp, view, rq);
  296. return rc;
  297. }
  298. int
  299. raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
  300. {
  301. struct raw3270 *rp;
  302. rp = view->dev;
  303. rq->view = view;
  304. raw3270_get_view(view);
  305. list_add_tail(&rq->list, &rp->req_queue);
  306. return 0;
  307. }
  308. /*
  309. * 3270 interrupt routine, called from the ccw_device layer
  310. */
  311. static void
  312. raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  313. {
  314. struct raw3270 *rp;
  315. struct raw3270_view *view;
  316. struct raw3270_request *rq;
  317. int rc;
  318. rp = (struct raw3270 *) cdev->dev.driver_data;
  319. if (!rp)
  320. return;
  321. rq = (struct raw3270_request *) intparm;
  322. view = rq ? rq->view : rp->view;
  323. if (IS_ERR(irb))
  324. rc = RAW3270_IO_RETRY;
  325. else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
  326. rq->rc = -EIO;
  327. rc = RAW3270_IO_DONE;
  328. } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
  329. DEV_STAT_UNIT_EXCEP)) {
  330. /* Handle CE-DE-UE and subsequent UDE */
  331. set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  332. rc = RAW3270_IO_BUSY;
  333. } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  334. /* Wait for UDE if busy flag is set. */
  335. if (irb->scsw.dstat & DEV_STAT_DEV_END) {
  336. clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  337. /* Got it, now retry. */
  338. rc = RAW3270_IO_RETRY;
  339. } else
  340. rc = RAW3270_IO_BUSY;
  341. } else if (view)
  342. rc = view->fn->intv(view, rq, irb);
  343. else
  344. rc = RAW3270_IO_DONE;
  345. switch (rc) {
  346. case RAW3270_IO_DONE:
  347. break;
  348. case RAW3270_IO_BUSY:
  349. /*
  350. * Intervention required by the operator. We have to wait
  351. * for unsolicited device end.
  352. */
  353. return;
  354. case RAW3270_IO_RETRY:
  355. if (!rq)
  356. break;
  357. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  358. (unsigned long) rq, 0, 0);
  359. if (rq->rc == 0)
  360. return; /* Sucessfully restarted. */
  361. break;
  362. case RAW3270_IO_STOP:
  363. if (!rq)
  364. break;
  365. raw3270_halt_io_nolock(rp, rq);
  366. rq->rc = -EIO;
  367. break;
  368. default:
  369. BUG();
  370. }
  371. if (rq) {
  372. BUG_ON(list_empty(&rq->list));
  373. /* The request completed, remove from queue and do callback. */
  374. list_del_init(&rq->list);
  375. if (rq->callback)
  376. rq->callback(rq, rq->callback_data);
  377. /* Do put_device for get_device in raw3270_start. */
  378. raw3270_put_view(view);
  379. }
  380. /*
  381. * Try to start each request on request queue until one is
  382. * started successful.
  383. */
  384. while (!list_empty(&rp->req_queue)) {
  385. rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
  386. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  387. (unsigned long) rq, 0, 0);
  388. if (rq->rc == 0)
  389. break;
  390. /* Start failed. Remove request and do callback. */
  391. list_del_init(&rq->list);
  392. if (rq->callback)
  393. rq->callback(rq, rq->callback_data);
  394. /* Do put_device for get_device in raw3270_start. */
  395. raw3270_put_view(view);
  396. }
  397. }
  398. /*
  399. * Size sensing.
  400. */
  401. struct raw3270_ua { /* Query Reply structure for Usable Area */
  402. struct { /* Usable Area Query Reply Base */
  403. short l; /* Length of this structured field */
  404. char sfid; /* 0x81 if Query Reply */
  405. char qcode; /* 0x81 if Usable Area */
  406. char flags0;
  407. char flags1;
  408. short w; /* Width of usable area */
  409. short h; /* Heigth of usavle area */
  410. char units; /* 0x00:in; 0x01:mm */
  411. int xr;
  412. int yr;
  413. char aw;
  414. char ah;
  415. short buffsz; /* Character buffer size, bytes */
  416. char xmin;
  417. char ymin;
  418. char xmax;
  419. char ymax;
  420. } __attribute__ ((packed)) uab;
  421. struct { /* Alternate Usable Area Self-Defining Parameter */
  422. char l; /* Length of this Self-Defining Parm */
  423. char sdpid; /* 0x02 if Alternate Usable Area */
  424. char res;
  425. char auaid; /* 0x01 is Id for the A U A */
  426. short wauai; /* Width of AUAi */
  427. short hauai; /* Height of AUAi */
  428. char auaunits; /* 0x00:in, 0x01:mm */
  429. int auaxr;
  430. int auayr;
  431. char awauai;
  432. char ahauai;
  433. } __attribute__ ((packed)) aua;
  434. } __attribute__ ((packed));
  435. static struct diag210 raw3270_init_diag210;
  436. static DEFINE_MUTEX(raw3270_init_mutex);
  437. static int
  438. raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
  439. struct irb *irb)
  440. {
  441. /*
  442. * Unit-Check Processing:
  443. * Expect Command Reject or Intervention Required.
  444. */
  445. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
  446. /* Request finished abnormally. */
  447. if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
  448. set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
  449. return RAW3270_IO_BUSY;
  450. }
  451. }
  452. if (rq) {
  453. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
  454. if (irb->ecw[0] & SNS0_CMD_REJECT)
  455. rq->rc = -EOPNOTSUPP;
  456. else
  457. rq->rc = -EIO;
  458. } else
  459. /* Request finished normally. Copy residual count. */
  460. rq->rescnt = irb->scsw.count;
  461. }
  462. if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
  463. set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
  464. wake_up(&raw3270_wait_queue);
  465. }
  466. return RAW3270_IO_DONE;
  467. }
  468. static struct raw3270_fn raw3270_init_fn = {
  469. .intv = raw3270_init_irq
  470. };
  471. static struct raw3270_view raw3270_init_view = {
  472. .fn = &raw3270_init_fn
  473. };
  474. /*
  475. * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
  476. * Wait for end of request. The request must have been started
  477. * with raw3270_start, rc = 0. The device lock may NOT have been
  478. * released between calling raw3270_start and raw3270_wait.
  479. */
  480. static void
  481. raw3270_wake_init(struct raw3270_request *rq, void *data)
  482. {
  483. wake_up((wait_queue_head_t *) data);
  484. }
  485. /*
  486. * Special wait function that can cope with console initialization.
  487. */
  488. static int
  489. raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
  490. struct raw3270_request *rq)
  491. {
  492. unsigned long flags;
  493. wait_queue_head_t wq;
  494. int rc;
  495. #ifdef CONFIG_TN3270_CONSOLE
  496. if (raw3270_registered == 0) {
  497. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  498. rq->callback = NULL;
  499. rc = __raw3270_start(rp, view, rq);
  500. if (rc == 0)
  501. while (!raw3270_request_final(rq)) {
  502. wait_cons_dev();
  503. barrier();
  504. }
  505. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  506. return rq->rc;
  507. }
  508. #endif
  509. init_waitqueue_head(&wq);
  510. rq->callback = raw3270_wake_init;
  511. rq->callback_data = &wq;
  512. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  513. rc = __raw3270_start(rp, view, rq);
  514. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  515. if (rc)
  516. return rc;
  517. /* Now wait for the completion. */
  518. rc = wait_event_interruptible(wq, raw3270_request_final(rq));
  519. if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
  520. raw3270_halt_io(view->dev, rq);
  521. /* No wait for the halt to complete. */
  522. wait_event(wq, raw3270_request_final(rq));
  523. return -ERESTARTSYS;
  524. }
  525. return rq->rc;
  526. }
  527. static int
  528. __raw3270_size_device_vm(struct raw3270 *rp)
  529. {
  530. int rc, model;
  531. struct ccw_dev_id dev_id;
  532. ccw_device_get_id(rp->cdev, &dev_id);
  533. raw3270_init_diag210.vrdcdvno = dev_id.devno;
  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. mutex_lock(&raw3270_init_mutex);
  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. mutex_unlock(&raw3270_init_mutex);
  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. mutex_lock(&raw3270_init_mutex);
  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. mutex_unlock(&raw3270_init_mutex);
  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 __init *raw3270_setup_console(struct ccw_device *cdev)
  772. {
  773. struct raw3270 *rp;
  774. char *ascebc;
  775. int rc;
  776. rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
  777. ascebc = (char *) alloc_bootmem(256);
  778. rc = raw3270_setup_device(cdev, rp, ascebc);
  779. if (rc)
  780. return ERR_PTR(rc);
  781. set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
  782. rc = raw3270_reset_device(rp);
  783. if (rc)
  784. return ERR_PTR(rc);
  785. rc = raw3270_size_device(rp);
  786. if (rc)
  787. return ERR_PTR(rc);
  788. rc = raw3270_reset_device(rp);
  789. if (rc)
  790. return ERR_PTR(rc);
  791. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  792. return rp;
  793. }
  794. void
  795. raw3270_wait_cons_dev(struct raw3270 *rp)
  796. {
  797. unsigned long flags;
  798. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  799. wait_cons_dev();
  800. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  801. }
  802. #endif
  803. /*
  804. * Create a 3270 device structure.
  805. */
  806. static struct raw3270 *
  807. raw3270_create_device(struct ccw_device *cdev)
  808. {
  809. struct raw3270 *rp;
  810. char *ascebc;
  811. int rc;
  812. rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  813. if (!rp)
  814. return ERR_PTR(-ENOMEM);
  815. ascebc = kmalloc(256, GFP_KERNEL);
  816. if (!ascebc) {
  817. kfree(rp);
  818. return ERR_PTR(-ENOMEM);
  819. }
  820. rc = raw3270_setup_device(cdev, rp, ascebc);
  821. if (rc) {
  822. kfree(rp->ascebc);
  823. kfree(rp);
  824. rp = ERR_PTR(rc);
  825. }
  826. /* Get reference to ccw_device structure. */
  827. get_device(&cdev->dev);
  828. return rp;
  829. }
  830. /*
  831. * Activate a view.
  832. */
  833. int
  834. raw3270_activate_view(struct raw3270_view *view)
  835. {
  836. struct raw3270 *rp;
  837. struct raw3270_view *oldview, *nv;
  838. unsigned long flags;
  839. int rc;
  840. rp = view->dev;
  841. if (!rp)
  842. return -ENODEV;
  843. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  844. if (rp->view == view)
  845. rc = 0;
  846. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  847. rc = -ENODEV;
  848. else {
  849. oldview = NULL;
  850. if (rp->view) {
  851. oldview = rp->view;
  852. oldview->fn->deactivate(oldview);
  853. }
  854. rp->view = view;
  855. rc = view->fn->activate(view);
  856. if (rc) {
  857. /* Didn't work. Try to reactivate the old view. */
  858. rp->view = oldview;
  859. if (!oldview || oldview->fn->activate(oldview) != 0) {
  860. /* Didn't work as well. Try any other view. */
  861. list_for_each_entry(nv, &rp->view_list, list)
  862. if (nv != view && nv != oldview) {
  863. rp->view = nv;
  864. if (nv->fn->activate(nv) == 0)
  865. break;
  866. rp->view = NULL;
  867. }
  868. }
  869. }
  870. }
  871. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  872. return rc;
  873. }
  874. /*
  875. * Deactivate current view.
  876. */
  877. void
  878. raw3270_deactivate_view(struct raw3270_view *view)
  879. {
  880. unsigned long flags;
  881. struct raw3270 *rp;
  882. rp = view->dev;
  883. if (!rp)
  884. return;
  885. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  886. if (rp->view == view) {
  887. view->fn->deactivate(view);
  888. rp->view = NULL;
  889. /* Move deactivated view to end of list. */
  890. list_del_init(&view->list);
  891. list_add_tail(&view->list, &rp->view_list);
  892. /* Try to activate another view. */
  893. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  894. list_for_each_entry(view, &rp->view_list, list) {
  895. rp->view = view;
  896. if (view->fn->activate(view) == 0)
  897. break;
  898. rp->view = NULL;
  899. }
  900. }
  901. }
  902. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  903. }
  904. /*
  905. * Add view to device with minor "minor".
  906. */
  907. int
  908. raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
  909. {
  910. unsigned long flags;
  911. struct raw3270 *rp;
  912. int rc;
  913. if (minor <= 0)
  914. return -ENODEV;
  915. mutex_lock(&raw3270_mutex);
  916. rc = -ENODEV;
  917. list_for_each_entry(rp, &raw3270_devices, list) {
  918. if (rp->minor != minor)
  919. continue;
  920. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  921. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  922. atomic_set(&view->ref_count, 2);
  923. view->dev = rp;
  924. view->fn = fn;
  925. view->model = rp->model;
  926. view->rows = rp->rows;
  927. view->cols = rp->cols;
  928. view->ascebc = rp->ascebc;
  929. spin_lock_init(&view->lock);
  930. list_add(&view->list, &rp->view_list);
  931. rc = 0;
  932. }
  933. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  934. break;
  935. }
  936. mutex_unlock(&raw3270_mutex);
  937. return rc;
  938. }
  939. /*
  940. * Find specific view of device with minor "minor".
  941. */
  942. struct raw3270_view *
  943. raw3270_find_view(struct raw3270_fn *fn, int minor)
  944. {
  945. struct raw3270 *rp;
  946. struct raw3270_view *view, *tmp;
  947. unsigned long flags;
  948. mutex_lock(&raw3270_mutex);
  949. view = ERR_PTR(-ENODEV);
  950. list_for_each_entry(rp, &raw3270_devices, list) {
  951. if (rp->minor != minor)
  952. continue;
  953. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  954. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  955. view = ERR_PTR(-ENOENT);
  956. list_for_each_entry(tmp, &rp->view_list, list) {
  957. if (tmp->fn == fn) {
  958. raw3270_get_view(tmp);
  959. view = tmp;
  960. break;
  961. }
  962. }
  963. }
  964. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  965. break;
  966. }
  967. mutex_unlock(&raw3270_mutex);
  968. return view;
  969. }
  970. /*
  971. * Remove view from device and free view structure via call to view->fn->free.
  972. */
  973. void
  974. raw3270_del_view(struct raw3270_view *view)
  975. {
  976. unsigned long flags;
  977. struct raw3270 *rp;
  978. struct raw3270_view *nv;
  979. rp = view->dev;
  980. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  981. if (rp->view == view) {
  982. view->fn->deactivate(view);
  983. rp->view = NULL;
  984. }
  985. list_del_init(&view->list);
  986. if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  987. /* Try to activate another view. */
  988. list_for_each_entry(nv, &rp->view_list, list) {
  989. if (nv->fn->activate(nv) == 0) {
  990. rp->view = nv;
  991. break;
  992. }
  993. }
  994. }
  995. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  996. /* Wait for reference counter to drop to zero. */
  997. atomic_dec(&view->ref_count);
  998. wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
  999. if (view->fn->free)
  1000. view->fn->free(view);
  1001. }
  1002. /*
  1003. * Remove a 3270 device structure.
  1004. */
  1005. static void
  1006. raw3270_delete_device(struct raw3270 *rp)
  1007. {
  1008. struct ccw_device *cdev;
  1009. /* Remove from device chain. */
  1010. mutex_lock(&raw3270_mutex);
  1011. if (rp->clttydev && !IS_ERR(rp->clttydev))
  1012. class_device_destroy(class3270,
  1013. MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1014. if (rp->cltubdev && !IS_ERR(rp->cltubdev))
  1015. class_device_destroy(class3270,
  1016. MKDEV(IBM_FS3270_MAJOR, rp->minor));
  1017. list_del_init(&rp->list);
  1018. mutex_unlock(&raw3270_mutex);
  1019. /* Disconnect from ccw_device. */
  1020. cdev = rp->cdev;
  1021. rp->cdev = NULL;
  1022. cdev->dev.driver_data = NULL;
  1023. cdev->handler = NULL;
  1024. /* Put ccw_device structure. */
  1025. put_device(&cdev->dev);
  1026. /* Now free raw3270 structure. */
  1027. kfree(rp->ascebc);
  1028. kfree(rp);
  1029. }
  1030. static int
  1031. raw3270_probe (struct ccw_device *cdev)
  1032. {
  1033. return 0;
  1034. }
  1035. /*
  1036. * Additional attributes for a 3270 device
  1037. */
  1038. static ssize_t
  1039. raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
  1040. {
  1041. return snprintf(buf, PAGE_SIZE, "%i\n",
  1042. ((struct raw3270 *) dev->driver_data)->model);
  1043. }
  1044. static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
  1045. static ssize_t
  1046. raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
  1047. {
  1048. return snprintf(buf, PAGE_SIZE, "%i\n",
  1049. ((struct raw3270 *) dev->driver_data)->rows);
  1050. }
  1051. static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
  1052. static ssize_t
  1053. raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
  1054. {
  1055. return snprintf(buf, PAGE_SIZE, "%i\n",
  1056. ((struct raw3270 *) dev->driver_data)->cols);
  1057. }
  1058. static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
  1059. static struct attribute * raw3270_attrs[] = {
  1060. &dev_attr_model.attr,
  1061. &dev_attr_rows.attr,
  1062. &dev_attr_columns.attr,
  1063. NULL,
  1064. };
  1065. static struct attribute_group raw3270_attr_group = {
  1066. .attrs = raw3270_attrs,
  1067. };
  1068. static int raw3270_create_attributes(struct raw3270 *rp)
  1069. {
  1070. int rc;
  1071. rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1072. if (rc)
  1073. goto out;
  1074. rp->clttydev = class_device_create(class3270, NULL,
  1075. MKDEV(IBM_TTY3270_MAJOR, rp->minor),
  1076. &rp->cdev->dev, "tty%s",
  1077. rp->cdev->dev.bus_id);
  1078. if (IS_ERR(rp->clttydev)) {
  1079. rc = PTR_ERR(rp->clttydev);
  1080. goto out_ttydev;
  1081. }
  1082. rp->cltubdev = class_device_create(class3270, NULL,
  1083. MKDEV(IBM_FS3270_MAJOR, rp->minor),
  1084. &rp->cdev->dev, "tub%s",
  1085. rp->cdev->dev.bus_id);
  1086. if (!IS_ERR(rp->cltubdev))
  1087. goto out;
  1088. rc = PTR_ERR(rp->cltubdev);
  1089. class_device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1090. out_ttydev:
  1091. sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1092. out:
  1093. return rc;
  1094. }
  1095. /*
  1096. * Notifier for device addition/removal
  1097. */
  1098. struct raw3270_notifier {
  1099. struct list_head list;
  1100. void (*notifier)(int, int);
  1101. };
  1102. static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
  1103. int raw3270_register_notifier(void (*notifier)(int, int))
  1104. {
  1105. struct raw3270_notifier *np;
  1106. struct raw3270 *rp;
  1107. np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
  1108. if (!np)
  1109. return -ENOMEM;
  1110. np->notifier = notifier;
  1111. mutex_lock(&raw3270_mutex);
  1112. list_add_tail(&np->list, &raw3270_notifier);
  1113. list_for_each_entry(rp, &raw3270_devices, list) {
  1114. get_device(&rp->cdev->dev);
  1115. notifier(rp->minor, 1);
  1116. }
  1117. mutex_unlock(&raw3270_mutex);
  1118. return 0;
  1119. }
  1120. void raw3270_unregister_notifier(void (*notifier)(int, int))
  1121. {
  1122. struct raw3270_notifier *np;
  1123. mutex_lock(&raw3270_mutex);
  1124. list_for_each_entry(np, &raw3270_notifier, list)
  1125. if (np->notifier == notifier) {
  1126. list_del(&np->list);
  1127. kfree(np);
  1128. break;
  1129. }
  1130. mutex_unlock(&raw3270_mutex);
  1131. }
  1132. /*
  1133. * Set 3270 device online.
  1134. */
  1135. static int
  1136. raw3270_set_online (struct ccw_device *cdev)
  1137. {
  1138. struct raw3270 *rp;
  1139. struct raw3270_notifier *np;
  1140. int rc;
  1141. rp = raw3270_create_device(cdev);
  1142. if (IS_ERR(rp))
  1143. return PTR_ERR(rp);
  1144. rc = raw3270_reset_device(rp);
  1145. if (rc)
  1146. goto failure;
  1147. rc = raw3270_size_device(rp);
  1148. if (rc)
  1149. goto failure;
  1150. rc = raw3270_reset_device(rp);
  1151. if (rc)
  1152. goto failure;
  1153. rc = raw3270_create_attributes(rp);
  1154. if (rc)
  1155. goto failure;
  1156. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  1157. mutex_lock(&raw3270_mutex);
  1158. list_for_each_entry(np, &raw3270_notifier, list)
  1159. np->notifier(rp->minor, 1);
  1160. mutex_unlock(&raw3270_mutex);
  1161. return 0;
  1162. failure:
  1163. raw3270_delete_device(rp);
  1164. return rc;
  1165. }
  1166. /*
  1167. * Remove 3270 device structure.
  1168. */
  1169. static void
  1170. raw3270_remove (struct ccw_device *cdev)
  1171. {
  1172. unsigned long flags;
  1173. struct raw3270 *rp;
  1174. struct raw3270_view *v;
  1175. struct raw3270_notifier *np;
  1176. rp = cdev->dev.driver_data;
  1177. /*
  1178. * _remove is the opposite of _probe; it's probe that
  1179. * should set up rp. raw3270_remove gets entered for
  1180. * devices even if they haven't been varied online.
  1181. * Thus, rp may validly be NULL here.
  1182. */
  1183. if (rp == NULL)
  1184. return;
  1185. clear_bit(RAW3270_FLAGS_READY, &rp->flags);
  1186. sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
  1187. /* Deactivate current view and remove all views. */
  1188. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1189. if (rp->view) {
  1190. rp->view->fn->deactivate(rp->view);
  1191. rp->view = NULL;
  1192. }
  1193. while (!list_empty(&rp->view_list)) {
  1194. v = list_entry(rp->view_list.next, struct raw3270_view, list);
  1195. if (v->fn->release)
  1196. v->fn->release(v);
  1197. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1198. raw3270_del_view(v);
  1199. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1200. }
  1201. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1202. mutex_lock(&raw3270_mutex);
  1203. list_for_each_entry(np, &raw3270_notifier, list)
  1204. np->notifier(rp->minor, 0);
  1205. mutex_unlock(&raw3270_mutex);
  1206. /* Reset 3270 device. */
  1207. raw3270_reset_device(rp);
  1208. /* And finally remove it. */
  1209. raw3270_delete_device(rp);
  1210. }
  1211. /*
  1212. * Set 3270 device offline.
  1213. */
  1214. static int
  1215. raw3270_set_offline (struct ccw_device *cdev)
  1216. {
  1217. struct raw3270 *rp;
  1218. rp = cdev->dev.driver_data;
  1219. if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
  1220. return -EBUSY;
  1221. raw3270_remove(cdev);
  1222. return 0;
  1223. }
  1224. static struct ccw_device_id raw3270_id[] = {
  1225. { CCW_DEVICE(0x3270, 0) },
  1226. { CCW_DEVICE(0x3271, 0) },
  1227. { CCW_DEVICE(0x3272, 0) },
  1228. { CCW_DEVICE(0x3273, 0) },
  1229. { CCW_DEVICE(0x3274, 0) },
  1230. { CCW_DEVICE(0x3275, 0) },
  1231. { CCW_DEVICE(0x3276, 0) },
  1232. { CCW_DEVICE(0x3277, 0) },
  1233. { CCW_DEVICE(0x3278, 0) },
  1234. { CCW_DEVICE(0x3279, 0) },
  1235. { CCW_DEVICE(0x3174, 0) },
  1236. { /* end of list */ },
  1237. };
  1238. static struct ccw_driver raw3270_ccw_driver = {
  1239. .name = "3270",
  1240. .owner = THIS_MODULE,
  1241. .ids = raw3270_id,
  1242. .probe = &raw3270_probe,
  1243. .remove = &raw3270_remove,
  1244. .set_online = &raw3270_set_online,
  1245. .set_offline = &raw3270_set_offline,
  1246. };
  1247. static int
  1248. raw3270_init(void)
  1249. {
  1250. struct raw3270 *rp;
  1251. int rc;
  1252. if (raw3270_registered)
  1253. return 0;
  1254. raw3270_registered = 1;
  1255. rc = ccw_driver_register(&raw3270_ccw_driver);
  1256. if (rc == 0) {
  1257. /* Create attributes for early (= console) device. */
  1258. mutex_lock(&raw3270_mutex);
  1259. class3270 = class_create(THIS_MODULE, "3270");
  1260. list_for_each_entry(rp, &raw3270_devices, list) {
  1261. get_device(&rp->cdev->dev);
  1262. raw3270_create_attributes(rp);
  1263. }
  1264. mutex_unlock(&raw3270_mutex);
  1265. }
  1266. return rc;
  1267. }
  1268. static void
  1269. raw3270_exit(void)
  1270. {
  1271. ccw_driver_unregister(&raw3270_ccw_driver);
  1272. class_destroy(class3270);
  1273. }
  1274. MODULE_LICENSE("GPL");
  1275. module_init(raw3270_init);
  1276. module_exit(raw3270_exit);
  1277. EXPORT_SYMBOL(raw3270_request_alloc);
  1278. EXPORT_SYMBOL(raw3270_request_free);
  1279. EXPORT_SYMBOL(raw3270_request_reset);
  1280. EXPORT_SYMBOL(raw3270_request_set_cmd);
  1281. EXPORT_SYMBOL(raw3270_request_add_data);
  1282. EXPORT_SYMBOL(raw3270_request_set_data);
  1283. EXPORT_SYMBOL(raw3270_request_set_idal);
  1284. EXPORT_SYMBOL(raw3270_buffer_address);
  1285. EXPORT_SYMBOL(raw3270_add_view);
  1286. EXPORT_SYMBOL(raw3270_del_view);
  1287. EXPORT_SYMBOL(raw3270_find_view);
  1288. EXPORT_SYMBOL(raw3270_activate_view);
  1289. EXPORT_SYMBOL(raw3270_deactivate_view);
  1290. EXPORT_SYMBOL(raw3270_start);
  1291. EXPORT_SYMBOL(raw3270_start_locked);
  1292. EXPORT_SYMBOL(raw3270_start_irq);
  1293. EXPORT_SYMBOL(raw3270_reset);
  1294. EXPORT_SYMBOL(raw3270_register_notifier);
  1295. EXPORT_SYMBOL(raw3270_unregister_notifier);
  1296. EXPORT_SYMBOL(raw3270_wait_queue);