nozomi.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
  4. *
  5. * Written by: Ulf Jakobsson,
  6. * Jan Åkerfeldt,
  7. * Stefan Thomasson,
  8. *
  9. * Maintained by: Paul Hardwick ([email protected])
  10. *
  11. * Patches:
  12. * Locking code changes for Vodafone by Sphere Systems Ltd,
  13. * Andrew Bird ([email protected] )
  14. * & Phil Sanderson
  15. *
  16. * Source has been ported from an implementation made by Filip Aben @ Option
  17. *
  18. * --------------------------------------------------------------------------
  19. *
  20. * Copyright (c) 2005,2006 Option Wireless Sweden AB
  21. * Copyright (c) 2006 Sphere Systems Ltd
  22. * Copyright (c) 2006 Option Wireless n/v
  23. * All rights Reserved.
  24. *
  25. * --------------------------------------------------------------------------
  26. */
  27. /* Enable this to have a lot of debug printouts */
  28. #define DEBUG
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/pci.h>
  32. #include <linux/ioport.h>
  33. #include <linux/tty.h>
  34. #include <linux/tty_driver.h>
  35. #include <linux/tty_flip.h>
  36. #include <linux/sched.h>
  37. #include <linux/serial.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/kmod.h>
  40. #include <linux/init.h>
  41. #include <linux/kfifo.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/slab.h>
  44. #include <asm/byteorder.h>
  45. #include <linux/delay.h>
  46. /* Default debug printout level */
  47. #define NOZOMI_DEBUG_LEVEL 0x00
  48. static int debug = NOZOMI_DEBUG_LEVEL;
  49. module_param(debug, int, S_IRUGO | S_IWUSR);
  50. /* Macros definitions */
  51. #define DBG_(lvl, fmt, args...) \
  52. do { \
  53. if (lvl & debug) \
  54. pr_debug("[%d] %s(): " fmt "\n", \
  55. __LINE__, __func__, ##args); \
  56. } while (0)
  57. #define DBG1(args...) DBG_(0x01, ##args)
  58. #define DBG2(args...) DBG_(0x02, ##args)
  59. #define DBG3(args...) DBG_(0x04, ##args)
  60. #define DBG4(args...) DBG_(0x08, ##args)
  61. /* TODO: rewrite to optimize macros... */
  62. #define TMP_BUF_MAX 256
  63. #define DUMP(buf__, len__) \
  64. do { \
  65. char tbuf[TMP_BUF_MAX] = {0}; \
  66. if (len__ > 1) { \
  67. u32 data_len = min_t(u32, len__, TMP_BUF_MAX); \
  68. strscpy(tbuf, buf__, data_len); \
  69. if (tbuf[data_len - 2] == '\r') \
  70. tbuf[data_len - 2] = 'r'; \
  71. DBG1("SENDING: '%s' (%d+n)", tbuf, len__); \
  72. } else { \
  73. DBG1("SENDING: '%s' (%d)", tbuf, len__); \
  74. } \
  75. } while (0)
  76. /* Defines */
  77. #define NOZOMI_NAME "nozomi"
  78. #define NOZOMI_NAME_TTY "nozomi_tty"
  79. #define NTTY_TTY_MAXMINORS 256
  80. #define NTTY_FIFO_BUFFER_SIZE 8192
  81. /* Must be power of 2 */
  82. #define FIFO_BUFFER_SIZE_UL 8192
  83. /* Size of tmp send buffer to card */
  84. #define SEND_BUF_MAX 1024
  85. #define RECEIVE_BUF_MAX 4
  86. #define R_IIR 0x0000 /* Interrupt Identity Register */
  87. #define R_FCR 0x0000 /* Flow Control Register */
  88. #define R_IER 0x0004 /* Interrupt Enable Register */
  89. #define NOZOMI_CONFIG_MAGIC 0xEFEFFEFE
  90. #define TOGGLE_VALID 0x0000
  91. /* Definition of interrupt tokens */
  92. #define MDM_DL1 0x0001
  93. #define MDM_UL1 0x0002
  94. #define MDM_DL2 0x0004
  95. #define MDM_UL2 0x0008
  96. #define DIAG_DL1 0x0010
  97. #define DIAG_DL2 0x0020
  98. #define DIAG_UL 0x0040
  99. #define APP1_DL 0x0080
  100. #define APP1_UL 0x0100
  101. #define APP2_DL 0x0200
  102. #define APP2_UL 0x0400
  103. #define CTRL_DL 0x0800
  104. #define CTRL_UL 0x1000
  105. #define RESET 0x8000
  106. #define MDM_DL (MDM_DL1 | MDM_DL2)
  107. #define MDM_UL (MDM_UL1 | MDM_UL2)
  108. #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
  109. /* modem signal definition */
  110. #define CTRL_DSR 0x0001
  111. #define CTRL_DCD 0x0002
  112. #define CTRL_RI 0x0004
  113. #define CTRL_CTS 0x0008
  114. #define CTRL_DTR 0x0001
  115. #define CTRL_RTS 0x0002
  116. #define MAX_PORT 4
  117. #define NOZOMI_MAX_PORTS 5
  118. #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
  119. /* Type definitions */
  120. /*
  121. * There are two types of nozomi cards,
  122. * one with 2048 memory and with 8192 memory
  123. */
  124. enum card_type {
  125. F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
  126. F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
  127. };
  128. /* Initialization states a card can be in */
  129. enum card_state {
  130. NOZOMI_STATE_UNKNOWN = 0,
  131. NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
  132. NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
  133. NOZOMI_STATE_READY = 3, /* flowcontrols received */
  134. };
  135. /* Two different toggle channels exist */
  136. enum channel_type {
  137. CH_A = 0,
  138. CH_B = 1,
  139. };
  140. /* Port definition for the card regarding flow control */
  141. enum ctrl_port_type {
  142. CTRL_CMD = 0,
  143. CTRL_MDM = 1,
  144. CTRL_DIAG = 2,
  145. CTRL_APP1 = 3,
  146. CTRL_APP2 = 4,
  147. CTRL_ERROR = -1,
  148. };
  149. /* Ports that the nozomi has */
  150. enum port_type {
  151. PORT_MDM = 0,
  152. PORT_DIAG = 1,
  153. PORT_APP1 = 2,
  154. PORT_APP2 = 3,
  155. PORT_CTRL = 4,
  156. PORT_ERROR = -1,
  157. };
  158. #ifdef __BIG_ENDIAN
  159. /* Big endian */
  160. struct toggles {
  161. unsigned int enabled:5; /*
  162. * Toggle fields are valid if enabled is 0,
  163. * else A-channels must always be used.
  164. */
  165. unsigned int diag_dl:1;
  166. unsigned int mdm_dl:1;
  167. unsigned int mdm_ul:1;
  168. } __attribute__ ((packed));
  169. /* Configuration table to read at startup of card */
  170. /* Is for now only needed during initialization phase */
  171. struct config_table {
  172. u32 signature;
  173. u16 product_information;
  174. u16 version;
  175. u8 pad3[3];
  176. struct toggles toggle;
  177. u8 pad1[4];
  178. u16 dl_mdm_len1; /*
  179. * If this is 64, it can hold
  180. * 60 bytes + 4 that is length field
  181. */
  182. u16 dl_start;
  183. u16 dl_diag_len1;
  184. u16 dl_mdm_len2; /*
  185. * If this is 64, it can hold
  186. * 60 bytes + 4 that is length field
  187. */
  188. u16 dl_app1_len;
  189. u16 dl_diag_len2;
  190. u16 dl_ctrl_len;
  191. u16 dl_app2_len;
  192. u8 pad2[16];
  193. u16 ul_mdm_len1;
  194. u16 ul_start;
  195. u16 ul_diag_len;
  196. u16 ul_mdm_len2;
  197. u16 ul_app1_len;
  198. u16 ul_app2_len;
  199. u16 ul_ctrl_len;
  200. } __attribute__ ((packed));
  201. /* This stores all control downlink flags */
  202. struct ctrl_dl {
  203. u8 port;
  204. unsigned int reserved:4;
  205. unsigned int CTS:1;
  206. unsigned int RI:1;
  207. unsigned int DCD:1;
  208. unsigned int DSR:1;
  209. } __attribute__ ((packed));
  210. /* This stores all control uplink flags */
  211. struct ctrl_ul {
  212. u8 port;
  213. unsigned int reserved:6;
  214. unsigned int RTS:1;
  215. unsigned int DTR:1;
  216. } __attribute__ ((packed));
  217. #else
  218. /* Little endian */
  219. /* This represents the toggle information */
  220. struct toggles {
  221. unsigned int mdm_ul:1;
  222. unsigned int mdm_dl:1;
  223. unsigned int diag_dl:1;
  224. unsigned int enabled:5; /*
  225. * Toggle fields are valid if enabled is 0,
  226. * else A-channels must always be used.
  227. */
  228. } __attribute__ ((packed));
  229. /* Configuration table to read at startup of card */
  230. struct config_table {
  231. u32 signature;
  232. u16 version;
  233. u16 product_information;
  234. struct toggles toggle;
  235. u8 pad1[7];
  236. u16 dl_start;
  237. u16 dl_mdm_len1; /*
  238. * If this is 64, it can hold
  239. * 60 bytes + 4 that is length field
  240. */
  241. u16 dl_mdm_len2;
  242. u16 dl_diag_len1;
  243. u16 dl_diag_len2;
  244. u16 dl_app1_len;
  245. u16 dl_app2_len;
  246. u16 dl_ctrl_len;
  247. u8 pad2[16];
  248. u16 ul_start;
  249. u16 ul_mdm_len2;
  250. u16 ul_mdm_len1;
  251. u16 ul_diag_len;
  252. u16 ul_app1_len;
  253. u16 ul_app2_len;
  254. u16 ul_ctrl_len;
  255. } __attribute__ ((packed));
  256. /* This stores all control downlink flags */
  257. struct ctrl_dl {
  258. unsigned int DSR:1;
  259. unsigned int DCD:1;
  260. unsigned int RI:1;
  261. unsigned int CTS:1;
  262. unsigned int reserved:4;
  263. u8 port;
  264. } __attribute__ ((packed));
  265. /* This stores all control uplink flags */
  266. struct ctrl_ul {
  267. unsigned int DTR:1;
  268. unsigned int RTS:1;
  269. unsigned int reserved:6;
  270. u8 port;
  271. } __attribute__ ((packed));
  272. #endif
  273. /* This holds all information that is needed regarding a port */
  274. struct port {
  275. struct tty_port port;
  276. u8 update_flow_control;
  277. struct ctrl_ul ctrl_ul;
  278. struct ctrl_dl ctrl_dl;
  279. struct kfifo fifo_ul;
  280. void __iomem *dl_addr[2];
  281. u32 dl_size[2];
  282. u8 toggle_dl;
  283. void __iomem *ul_addr[2];
  284. u32 ul_size[2];
  285. u8 toggle_ul;
  286. u16 token_dl;
  287. wait_queue_head_t tty_wait;
  288. struct async_icount tty_icount;
  289. struct nozomi *dc;
  290. };
  291. /* Private data one for each card in the system */
  292. struct nozomi {
  293. void __iomem *base_addr;
  294. unsigned long flip;
  295. /* Pointers to registers */
  296. void __iomem *reg_iir;
  297. void __iomem *reg_fcr;
  298. void __iomem *reg_ier;
  299. u16 last_ier;
  300. enum card_type card_type;
  301. struct config_table config_table; /* Configuration table */
  302. struct pci_dev *pdev;
  303. struct port port[NOZOMI_MAX_PORTS];
  304. u8 *send_buf;
  305. spinlock_t spin_mutex; /* secures access to registers and tty */
  306. unsigned int index_start;
  307. enum card_state state;
  308. u32 open_ttys;
  309. };
  310. /* Global variables */
  311. static const struct pci_device_id nozomi_pci_tbl[] = {
  312. {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
  313. {},
  314. };
  315. MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
  316. static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
  317. static struct tty_driver *ntty_driver;
  318. static const struct tty_port_operations noz_tty_port_ops;
  319. /*
  320. * find card by tty_index
  321. */
  322. static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
  323. {
  324. return tty ? ndevs[tty->index / MAX_PORT] : NULL;
  325. }
  326. static inline struct port *get_port_by_tty(const struct tty_struct *tty)
  327. {
  328. struct nozomi *ndev = get_dc_by_tty(tty);
  329. return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
  330. }
  331. /*
  332. * TODO:
  333. * -Optimize
  334. * -Rewrite cleaner
  335. */
  336. static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
  337. u32 size_bytes)
  338. {
  339. u32 i = 0;
  340. const u32 __iomem *ptr = mem_addr_start;
  341. u16 *buf16;
  342. if (unlikely(!ptr || !buf))
  343. goto out;
  344. /* shortcut for extremely often used cases */
  345. switch (size_bytes) {
  346. case 2: /* 2 bytes */
  347. buf16 = (u16 *) buf;
  348. *buf16 = __le16_to_cpu(readw(ptr));
  349. goto out;
  350. case 4: /* 4 bytes */
  351. *(buf) = __le32_to_cpu(readl(ptr));
  352. goto out;
  353. }
  354. while (i < size_bytes) {
  355. if (size_bytes - i == 2) {
  356. /* Handle 2 bytes in the end */
  357. buf16 = (u16 *) buf;
  358. *(buf16) = __le16_to_cpu(readw(ptr));
  359. i += 2;
  360. } else {
  361. /* Read 4 bytes */
  362. *(buf) = __le32_to_cpu(readl(ptr));
  363. i += 4;
  364. }
  365. buf++;
  366. ptr++;
  367. }
  368. out:
  369. return;
  370. }
  371. /*
  372. * TODO:
  373. * -Optimize
  374. * -Rewrite cleaner
  375. */
  376. static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
  377. u32 size_bytes)
  378. {
  379. u32 i = 0;
  380. u32 __iomem *ptr = mem_addr_start;
  381. const u16 *buf16;
  382. if (unlikely(!ptr || !buf))
  383. return 0;
  384. /* shortcut for extremely often used cases */
  385. switch (size_bytes) {
  386. case 2: /* 2 bytes */
  387. buf16 = (const u16 *)buf;
  388. writew(__cpu_to_le16(*buf16), ptr);
  389. return 2;
  390. case 1: /*
  391. * also needs to write 4 bytes in this case
  392. * so falling through..
  393. */
  394. fallthrough;
  395. case 4: /* 4 bytes */
  396. writel(__cpu_to_le32(*buf), ptr);
  397. return 4;
  398. }
  399. while (i < size_bytes) {
  400. if (size_bytes - i == 2) {
  401. /* 2 bytes */
  402. buf16 = (const u16 *)buf;
  403. writew(__cpu_to_le16(*buf16), ptr);
  404. i += 2;
  405. } else {
  406. /* 4 bytes */
  407. writel(__cpu_to_le32(*buf), ptr);
  408. i += 4;
  409. }
  410. buf++;
  411. ptr++;
  412. }
  413. return i;
  414. }
  415. /* Setup pointers to different channels and also setup buffer sizes. */
  416. static void nozomi_setup_memory(struct nozomi *dc)
  417. {
  418. void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
  419. /* The length reported is including the length field of 4 bytes,
  420. * hence subtract with 4.
  421. */
  422. const u16 buff_offset = 4;
  423. /* Modem port dl configuration */
  424. dc->port[PORT_MDM].dl_addr[CH_A] = offset;
  425. dc->port[PORT_MDM].dl_addr[CH_B] =
  426. (offset += dc->config_table.dl_mdm_len1);
  427. dc->port[PORT_MDM].dl_size[CH_A] =
  428. dc->config_table.dl_mdm_len1 - buff_offset;
  429. dc->port[PORT_MDM].dl_size[CH_B] =
  430. dc->config_table.dl_mdm_len2 - buff_offset;
  431. /* Diag port dl configuration */
  432. dc->port[PORT_DIAG].dl_addr[CH_A] =
  433. (offset += dc->config_table.dl_mdm_len2);
  434. dc->port[PORT_DIAG].dl_size[CH_A] =
  435. dc->config_table.dl_diag_len1 - buff_offset;
  436. dc->port[PORT_DIAG].dl_addr[CH_B] =
  437. (offset += dc->config_table.dl_diag_len1);
  438. dc->port[PORT_DIAG].dl_size[CH_B] =
  439. dc->config_table.dl_diag_len2 - buff_offset;
  440. /* App1 port dl configuration */
  441. dc->port[PORT_APP1].dl_addr[CH_A] =
  442. (offset += dc->config_table.dl_diag_len2);
  443. dc->port[PORT_APP1].dl_size[CH_A] =
  444. dc->config_table.dl_app1_len - buff_offset;
  445. /* App2 port dl configuration */
  446. dc->port[PORT_APP2].dl_addr[CH_A] =
  447. (offset += dc->config_table.dl_app1_len);
  448. dc->port[PORT_APP2].dl_size[CH_A] =
  449. dc->config_table.dl_app2_len - buff_offset;
  450. /* Ctrl dl configuration */
  451. dc->port[PORT_CTRL].dl_addr[CH_A] =
  452. (offset += dc->config_table.dl_app2_len);
  453. dc->port[PORT_CTRL].dl_size[CH_A] =
  454. dc->config_table.dl_ctrl_len - buff_offset;
  455. offset = dc->base_addr + dc->config_table.ul_start;
  456. /* Modem Port ul configuration */
  457. dc->port[PORT_MDM].ul_addr[CH_A] = offset;
  458. dc->port[PORT_MDM].ul_size[CH_A] =
  459. dc->config_table.ul_mdm_len1 - buff_offset;
  460. dc->port[PORT_MDM].ul_addr[CH_B] =
  461. (offset += dc->config_table.ul_mdm_len1);
  462. dc->port[PORT_MDM].ul_size[CH_B] =
  463. dc->config_table.ul_mdm_len2 - buff_offset;
  464. /* Diag port ul configuration */
  465. dc->port[PORT_DIAG].ul_addr[CH_A] =
  466. (offset += dc->config_table.ul_mdm_len2);
  467. dc->port[PORT_DIAG].ul_size[CH_A] =
  468. dc->config_table.ul_diag_len - buff_offset;
  469. /* App1 port ul configuration */
  470. dc->port[PORT_APP1].ul_addr[CH_A] =
  471. (offset += dc->config_table.ul_diag_len);
  472. dc->port[PORT_APP1].ul_size[CH_A] =
  473. dc->config_table.ul_app1_len - buff_offset;
  474. /* App2 port ul configuration */
  475. dc->port[PORT_APP2].ul_addr[CH_A] =
  476. (offset += dc->config_table.ul_app1_len);
  477. dc->port[PORT_APP2].ul_size[CH_A] =
  478. dc->config_table.ul_app2_len - buff_offset;
  479. /* Ctrl ul configuration */
  480. dc->port[PORT_CTRL].ul_addr[CH_A] =
  481. (offset += dc->config_table.ul_app2_len);
  482. dc->port[PORT_CTRL].ul_size[CH_A] =
  483. dc->config_table.ul_ctrl_len - buff_offset;
  484. }
  485. /* Dump config table under initalization phase */
  486. #ifdef DEBUG
  487. static void dump_table(const struct nozomi *dc)
  488. {
  489. DBG3("signature: 0x%08X", dc->config_table.signature);
  490. DBG3("version: 0x%04X", dc->config_table.version);
  491. DBG3("product_information: 0x%04X", \
  492. dc->config_table.product_information);
  493. DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
  494. DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
  495. DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
  496. DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
  497. DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
  498. DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
  499. dc->config_table.dl_mdm_len1);
  500. DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
  501. dc->config_table.dl_mdm_len2);
  502. DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
  503. dc->config_table.dl_diag_len1);
  504. DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
  505. dc->config_table.dl_diag_len2);
  506. DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
  507. dc->config_table.dl_app1_len);
  508. DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
  509. dc->config_table.dl_app2_len);
  510. DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
  511. dc->config_table.dl_ctrl_len);
  512. DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
  513. dc->config_table.ul_start);
  514. DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
  515. dc->config_table.ul_mdm_len1);
  516. DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
  517. dc->config_table.ul_mdm_len2);
  518. DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
  519. dc->config_table.ul_diag_len);
  520. DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
  521. dc->config_table.ul_app1_len);
  522. DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
  523. dc->config_table.ul_app2_len);
  524. DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
  525. dc->config_table.ul_ctrl_len);
  526. }
  527. #else
  528. static inline void dump_table(const struct nozomi *dc) { }
  529. #endif
  530. /*
  531. * Read configuration table from card under intalization phase
  532. * Returns 1 if ok, else 0
  533. */
  534. static int nozomi_read_config_table(struct nozomi *dc)
  535. {
  536. read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
  537. sizeof(struct config_table));
  538. if (dc->config_table.signature != NOZOMI_CONFIG_MAGIC) {
  539. dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
  540. dc->config_table.signature, NOZOMI_CONFIG_MAGIC);
  541. return 0;
  542. }
  543. if ((dc->config_table.version == 0)
  544. || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
  545. int i;
  546. DBG1("Second phase, configuring card");
  547. nozomi_setup_memory(dc);
  548. dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
  549. dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
  550. dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
  551. DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
  552. dc->port[PORT_MDM].toggle_ul,
  553. dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
  554. dump_table(dc);
  555. for (i = PORT_MDM; i < MAX_PORT; i++) {
  556. memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
  557. memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
  558. }
  559. /* Enable control channel */
  560. dc->last_ier = dc->last_ier | CTRL_DL;
  561. writew(dc->last_ier, dc->reg_ier);
  562. dc->state = NOZOMI_STATE_ALLOCATED;
  563. dev_info(&dc->pdev->dev, "Initialization OK!\n");
  564. return 1;
  565. }
  566. if ((dc->config_table.version > 0)
  567. && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
  568. u32 offset = 0;
  569. DBG1("First phase: pushing upload buffers, clearing download");
  570. dev_info(&dc->pdev->dev, "Version of card: %d\n",
  571. dc->config_table.version);
  572. /* Here we should disable all I/O over F32. */
  573. nozomi_setup_memory(dc);
  574. /*
  575. * We should send ALL channel pair tokens back along
  576. * with reset token
  577. */
  578. /* push upload modem buffers */
  579. write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
  580. (u32 *) &offset, 4);
  581. write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
  582. (u32 *) &offset, 4);
  583. writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
  584. DBG1("First phase done");
  585. }
  586. return 1;
  587. }
  588. /* Enable uplink interrupts */
  589. static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
  590. {
  591. static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
  592. if (port < NOZOMI_MAX_PORTS) {
  593. dc->last_ier |= mask[port];
  594. writew(dc->last_ier, dc->reg_ier);
  595. } else {
  596. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  597. }
  598. }
  599. /* Disable uplink interrupts */
  600. static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
  601. {
  602. static const u16 mask[] =
  603. {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
  604. if (port < NOZOMI_MAX_PORTS) {
  605. dc->last_ier &= mask[port];
  606. writew(dc->last_ier, dc->reg_ier);
  607. } else {
  608. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  609. }
  610. }
  611. /* Enable downlink interrupts */
  612. static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
  613. {
  614. static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
  615. if (port < NOZOMI_MAX_PORTS) {
  616. dc->last_ier |= mask[port];
  617. writew(dc->last_ier, dc->reg_ier);
  618. } else {
  619. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  620. }
  621. }
  622. /* Disable downlink interrupts */
  623. static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
  624. {
  625. static const u16 mask[] =
  626. {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
  627. if (port < NOZOMI_MAX_PORTS) {
  628. dc->last_ier &= mask[port];
  629. writew(dc->last_ier, dc->reg_ier);
  630. } else {
  631. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  632. }
  633. }
  634. /*
  635. * Return 1 - send buffer to card and ack.
  636. * Return 0 - don't ack, don't send buffer to card.
  637. */
  638. static int send_data(enum port_type index, struct nozomi *dc)
  639. {
  640. u32 size = 0;
  641. struct port *port = &dc->port[index];
  642. const u8 toggle = port->toggle_ul;
  643. void __iomem *addr = port->ul_addr[toggle];
  644. const u32 ul_size = port->ul_size[toggle];
  645. /* Get data from tty and place in buf for now */
  646. size = kfifo_out(&port->fifo_ul, dc->send_buf,
  647. ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
  648. if (size == 0) {
  649. DBG4("No more data to send, disable link:");
  650. return 0;
  651. }
  652. /* DUMP(buf, size); */
  653. /* Write length + data */
  654. write_mem32(addr, (u32 *) &size, 4);
  655. write_mem32(addr + 4, (u32 *) dc->send_buf, size);
  656. tty_port_tty_wakeup(&port->port);
  657. return 1;
  658. }
  659. /* If all data has been read, return 1, else 0 */
  660. static int receive_data(enum port_type index, struct nozomi *dc)
  661. {
  662. u8 buf[RECEIVE_BUF_MAX] = { 0 };
  663. int size;
  664. u32 offset = 4;
  665. struct port *port = &dc->port[index];
  666. void __iomem *addr = port->dl_addr[port->toggle_dl];
  667. struct tty_struct *tty = tty_port_tty_get(&port->port);
  668. int i, ret;
  669. size = __le32_to_cpu(readl(addr));
  670. if (tty && tty_throttled(tty)) {
  671. DBG1("No room in tty, don't read data, don't ack interrupt, "
  672. "disable interrupt");
  673. /* disable interrupt in downlink... */
  674. disable_transmit_dl(index, dc);
  675. ret = 0;
  676. goto put;
  677. }
  678. if (unlikely(size == 0)) {
  679. dev_err(&dc->pdev->dev, "size == 0?\n");
  680. ret = 1;
  681. goto put;
  682. }
  683. while (size > 0) {
  684. read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
  685. if (size == 1) {
  686. tty_insert_flip_char(&port->port, buf[0], TTY_NORMAL);
  687. size = 0;
  688. } else if (size < RECEIVE_BUF_MAX) {
  689. size -= tty_insert_flip_string(&port->port,
  690. (char *)buf, size);
  691. } else {
  692. i = tty_insert_flip_string(&port->port,
  693. (char *)buf, RECEIVE_BUF_MAX);
  694. size -= i;
  695. offset += i;
  696. }
  697. }
  698. set_bit(index, &dc->flip);
  699. ret = 1;
  700. put:
  701. tty_kref_put(tty);
  702. return ret;
  703. }
  704. /* Debug for interrupts */
  705. #ifdef DEBUG
  706. static char *interrupt2str(u16 interrupt)
  707. {
  708. static char buf[TMP_BUF_MAX];
  709. char *p = buf;
  710. if (interrupt & MDM_DL1)
  711. p += scnprintf(p, TMP_BUF_MAX, "MDM_DL1 ");
  712. if (interrupt & MDM_DL2)
  713. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "MDM_DL2 ");
  714. if (interrupt & MDM_UL1)
  715. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "MDM_UL1 ");
  716. if (interrupt & MDM_UL2)
  717. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "MDM_UL2 ");
  718. if (interrupt & DIAG_DL1)
  719. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "DIAG_DL1 ");
  720. if (interrupt & DIAG_DL2)
  721. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "DIAG_DL2 ");
  722. if (interrupt & DIAG_UL)
  723. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "DIAG_UL ");
  724. if (interrupt & APP1_DL)
  725. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "APP1_DL ");
  726. if (interrupt & APP2_DL)
  727. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "APP2_DL ");
  728. if (interrupt & APP1_UL)
  729. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "APP1_UL ");
  730. if (interrupt & APP2_UL)
  731. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "APP2_UL ");
  732. if (interrupt & CTRL_DL)
  733. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "CTRL_DL ");
  734. if (interrupt & CTRL_UL)
  735. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "CTRL_UL ");
  736. if (interrupt & RESET)
  737. p += scnprintf(p, TMP_BUF_MAX - (p - buf), "RESET ");
  738. return buf;
  739. }
  740. #endif
  741. /*
  742. * Receive flow control
  743. * Return 1 - If ok, else 0
  744. */
  745. static int receive_flow_control(struct nozomi *dc)
  746. {
  747. enum port_type port = PORT_MDM;
  748. struct ctrl_dl ctrl_dl;
  749. struct ctrl_dl old_ctrl;
  750. u16 enable_ier = 0;
  751. read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
  752. switch (ctrl_dl.port) {
  753. case CTRL_CMD:
  754. DBG1("The Base Band sends this value as a response to a "
  755. "request for IMSI detach sent over the control "
  756. "channel uplink (see section 7.6.1).");
  757. break;
  758. case CTRL_MDM:
  759. port = PORT_MDM;
  760. enable_ier = MDM_DL;
  761. break;
  762. case CTRL_DIAG:
  763. port = PORT_DIAG;
  764. enable_ier = DIAG_DL;
  765. break;
  766. case CTRL_APP1:
  767. port = PORT_APP1;
  768. enable_ier = APP1_DL;
  769. break;
  770. case CTRL_APP2:
  771. port = PORT_APP2;
  772. enable_ier = APP2_DL;
  773. if (dc->state == NOZOMI_STATE_ALLOCATED) {
  774. /*
  775. * After card initialization the flow control
  776. * received for APP2 is always the last
  777. */
  778. dc->state = NOZOMI_STATE_READY;
  779. dev_info(&dc->pdev->dev, "Device READY!\n");
  780. }
  781. break;
  782. default:
  783. dev_err(&dc->pdev->dev,
  784. "ERROR: flow control received for non-existing port\n");
  785. return 0;
  786. }
  787. DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
  788. *((u16 *)&ctrl_dl));
  789. old_ctrl = dc->port[port].ctrl_dl;
  790. dc->port[port].ctrl_dl = ctrl_dl;
  791. if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
  792. DBG1("Disable interrupt (0x%04X) on port: %d",
  793. enable_ier, port);
  794. disable_transmit_ul(port, dc);
  795. } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
  796. if (kfifo_len(&dc->port[port].fifo_ul)) {
  797. DBG1("Enable interrupt (0x%04X) on port: %d",
  798. enable_ier, port);
  799. DBG1("Data in buffer [%d], enable transmit! ",
  800. kfifo_len(&dc->port[port].fifo_ul));
  801. enable_transmit_ul(port, dc);
  802. } else {
  803. DBG1("No data in buffer...");
  804. }
  805. }
  806. if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
  807. DBG1(" No change in mctrl");
  808. return 1;
  809. }
  810. /* Update statistics */
  811. if (old_ctrl.CTS != ctrl_dl.CTS)
  812. dc->port[port].tty_icount.cts++;
  813. if (old_ctrl.DSR != ctrl_dl.DSR)
  814. dc->port[port].tty_icount.dsr++;
  815. if (old_ctrl.RI != ctrl_dl.RI)
  816. dc->port[port].tty_icount.rng++;
  817. if (old_ctrl.DCD != ctrl_dl.DCD)
  818. dc->port[port].tty_icount.dcd++;
  819. wake_up_interruptible(&dc->port[port].tty_wait);
  820. DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
  821. port,
  822. dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
  823. dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
  824. return 1;
  825. }
  826. static enum ctrl_port_type port2ctrl(enum port_type port,
  827. const struct nozomi *dc)
  828. {
  829. switch (port) {
  830. case PORT_MDM:
  831. return CTRL_MDM;
  832. case PORT_DIAG:
  833. return CTRL_DIAG;
  834. case PORT_APP1:
  835. return CTRL_APP1;
  836. case PORT_APP2:
  837. return CTRL_APP2;
  838. default:
  839. dev_err(&dc->pdev->dev,
  840. "ERROR: send flow control " \
  841. "received for non-existing port\n");
  842. }
  843. return CTRL_ERROR;
  844. }
  845. /*
  846. * Send flow control, can only update one channel at a time
  847. * Return 0 - If we have updated all flow control
  848. * Return 1 - If we need to update more flow control, ack current enable more
  849. */
  850. static int send_flow_control(struct nozomi *dc)
  851. {
  852. u32 i, more_flow_control_to_be_updated = 0;
  853. u16 *ctrl;
  854. for (i = PORT_MDM; i < MAX_PORT; i++) {
  855. if (dc->port[i].update_flow_control) {
  856. if (more_flow_control_to_be_updated) {
  857. /* We have more flow control to be updated */
  858. return 1;
  859. }
  860. dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
  861. ctrl = (u16 *)&dc->port[i].ctrl_ul;
  862. write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
  863. (u32 *) ctrl, 2);
  864. dc->port[i].update_flow_control = 0;
  865. more_flow_control_to_be_updated = 1;
  866. }
  867. }
  868. return 0;
  869. }
  870. /*
  871. * Handle downlink data, ports that are handled are modem and diagnostics
  872. * Return 1 - ok
  873. * Return 0 - toggle fields are out of sync
  874. */
  875. static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
  876. u16 read_iir, u16 mask1, u16 mask2)
  877. {
  878. if (*toggle == 0 && read_iir & mask1) {
  879. if (receive_data(port, dc)) {
  880. writew(mask1, dc->reg_fcr);
  881. *toggle = !(*toggle);
  882. }
  883. if (read_iir & mask2) {
  884. if (receive_data(port, dc)) {
  885. writew(mask2, dc->reg_fcr);
  886. *toggle = !(*toggle);
  887. }
  888. }
  889. } else if (*toggle == 1 && read_iir & mask2) {
  890. if (receive_data(port, dc)) {
  891. writew(mask2, dc->reg_fcr);
  892. *toggle = !(*toggle);
  893. }
  894. if (read_iir & mask1) {
  895. if (receive_data(port, dc)) {
  896. writew(mask1, dc->reg_fcr);
  897. *toggle = !(*toggle);
  898. }
  899. }
  900. } else {
  901. dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
  902. *toggle);
  903. return 0;
  904. }
  905. return 1;
  906. }
  907. /*
  908. * Handle uplink data, this is currently for the modem port
  909. * Return 1 - ok
  910. * Return 0 - toggle field are out of sync
  911. */
  912. static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
  913. {
  914. u8 *toggle = &(dc->port[port].toggle_ul);
  915. if (*toggle == 0 && read_iir & MDM_UL1) {
  916. dc->last_ier &= ~MDM_UL;
  917. writew(dc->last_ier, dc->reg_ier);
  918. if (send_data(port, dc)) {
  919. writew(MDM_UL1, dc->reg_fcr);
  920. dc->last_ier = dc->last_ier | MDM_UL;
  921. writew(dc->last_ier, dc->reg_ier);
  922. *toggle = !*toggle;
  923. }
  924. if (read_iir & MDM_UL2) {
  925. dc->last_ier &= ~MDM_UL;
  926. writew(dc->last_ier, dc->reg_ier);
  927. if (send_data(port, dc)) {
  928. writew(MDM_UL2, dc->reg_fcr);
  929. dc->last_ier = dc->last_ier | MDM_UL;
  930. writew(dc->last_ier, dc->reg_ier);
  931. *toggle = !*toggle;
  932. }
  933. }
  934. } else if (*toggle == 1 && read_iir & MDM_UL2) {
  935. dc->last_ier &= ~MDM_UL;
  936. writew(dc->last_ier, dc->reg_ier);
  937. if (send_data(port, dc)) {
  938. writew(MDM_UL2, dc->reg_fcr);
  939. dc->last_ier = dc->last_ier | MDM_UL;
  940. writew(dc->last_ier, dc->reg_ier);
  941. *toggle = !*toggle;
  942. }
  943. if (read_iir & MDM_UL1) {
  944. dc->last_ier &= ~MDM_UL;
  945. writew(dc->last_ier, dc->reg_ier);
  946. if (send_data(port, dc)) {
  947. writew(MDM_UL1, dc->reg_fcr);
  948. dc->last_ier = dc->last_ier | MDM_UL;
  949. writew(dc->last_ier, dc->reg_ier);
  950. *toggle = !*toggle;
  951. }
  952. }
  953. } else {
  954. writew(read_iir & MDM_UL, dc->reg_fcr);
  955. dev_err(&dc->pdev->dev, "port out of sync!\n");
  956. return 0;
  957. }
  958. return 1;
  959. }
  960. static irqreturn_t interrupt_handler(int irq, void *dev_id)
  961. {
  962. struct nozomi *dc = dev_id;
  963. unsigned int a;
  964. u16 read_iir;
  965. if (!dc)
  966. return IRQ_NONE;
  967. spin_lock(&dc->spin_mutex);
  968. read_iir = readw(dc->reg_iir);
  969. /* Card removed */
  970. if (read_iir == (u16)-1)
  971. goto none;
  972. /*
  973. * Just handle interrupt enabled in IER
  974. * (by masking with dc->last_ier)
  975. */
  976. read_iir &= dc->last_ier;
  977. if (read_iir == 0)
  978. goto none;
  979. DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
  980. dc->last_ier);
  981. if (read_iir & RESET) {
  982. if (unlikely(!nozomi_read_config_table(dc))) {
  983. dc->last_ier = 0x0;
  984. writew(dc->last_ier, dc->reg_ier);
  985. dev_err(&dc->pdev->dev, "Could not read status from "
  986. "card, we should disable interface\n");
  987. } else {
  988. writew(RESET, dc->reg_fcr);
  989. }
  990. /* No more useful info if this was the reset interrupt. */
  991. goto exit_handler;
  992. }
  993. if (read_iir & CTRL_UL) {
  994. DBG1("CTRL_UL");
  995. dc->last_ier &= ~CTRL_UL;
  996. writew(dc->last_ier, dc->reg_ier);
  997. if (send_flow_control(dc)) {
  998. writew(CTRL_UL, dc->reg_fcr);
  999. dc->last_ier = dc->last_ier | CTRL_UL;
  1000. writew(dc->last_ier, dc->reg_ier);
  1001. }
  1002. }
  1003. if (read_iir & CTRL_DL) {
  1004. receive_flow_control(dc);
  1005. writew(CTRL_DL, dc->reg_fcr);
  1006. }
  1007. if (read_iir & MDM_DL) {
  1008. if (!handle_data_dl(dc, PORT_MDM,
  1009. &(dc->port[PORT_MDM].toggle_dl), read_iir,
  1010. MDM_DL1, MDM_DL2)) {
  1011. dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
  1012. goto exit_handler;
  1013. }
  1014. }
  1015. if (read_iir & MDM_UL) {
  1016. if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
  1017. dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
  1018. goto exit_handler;
  1019. }
  1020. }
  1021. if (read_iir & DIAG_DL) {
  1022. if (!handle_data_dl(dc, PORT_DIAG,
  1023. &(dc->port[PORT_DIAG].toggle_dl), read_iir,
  1024. DIAG_DL1, DIAG_DL2)) {
  1025. dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
  1026. goto exit_handler;
  1027. }
  1028. }
  1029. if (read_iir & DIAG_UL) {
  1030. dc->last_ier &= ~DIAG_UL;
  1031. writew(dc->last_ier, dc->reg_ier);
  1032. if (send_data(PORT_DIAG, dc)) {
  1033. writew(DIAG_UL, dc->reg_fcr);
  1034. dc->last_ier = dc->last_ier | DIAG_UL;
  1035. writew(dc->last_ier, dc->reg_ier);
  1036. }
  1037. }
  1038. if (read_iir & APP1_DL) {
  1039. if (receive_data(PORT_APP1, dc))
  1040. writew(APP1_DL, dc->reg_fcr);
  1041. }
  1042. if (read_iir & APP1_UL) {
  1043. dc->last_ier &= ~APP1_UL;
  1044. writew(dc->last_ier, dc->reg_ier);
  1045. if (send_data(PORT_APP1, dc)) {
  1046. writew(APP1_UL, dc->reg_fcr);
  1047. dc->last_ier = dc->last_ier | APP1_UL;
  1048. writew(dc->last_ier, dc->reg_ier);
  1049. }
  1050. }
  1051. if (read_iir & APP2_DL) {
  1052. if (receive_data(PORT_APP2, dc))
  1053. writew(APP2_DL, dc->reg_fcr);
  1054. }
  1055. if (read_iir & APP2_UL) {
  1056. dc->last_ier &= ~APP2_UL;
  1057. writew(dc->last_ier, dc->reg_ier);
  1058. if (send_data(PORT_APP2, dc)) {
  1059. writew(APP2_UL, dc->reg_fcr);
  1060. dc->last_ier = dc->last_ier | APP2_UL;
  1061. writew(dc->last_ier, dc->reg_ier);
  1062. }
  1063. }
  1064. exit_handler:
  1065. spin_unlock(&dc->spin_mutex);
  1066. for (a = 0; a < NOZOMI_MAX_PORTS; a++)
  1067. if (test_and_clear_bit(a, &dc->flip))
  1068. tty_flip_buffer_push(&dc->port[a].port);
  1069. return IRQ_HANDLED;
  1070. none:
  1071. spin_unlock(&dc->spin_mutex);
  1072. return IRQ_NONE;
  1073. }
  1074. static void nozomi_get_card_type(struct nozomi *dc)
  1075. {
  1076. int i;
  1077. u32 size = 0;
  1078. for (i = 0; i < 6; i++)
  1079. size += pci_resource_len(dc->pdev, i);
  1080. /* Assume card type F32_8 if no match */
  1081. dc->card_type = size == 2048 ? F32_2 : F32_8;
  1082. dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
  1083. }
  1084. static void nozomi_setup_private_data(struct nozomi *dc)
  1085. {
  1086. void __iomem *offset = dc->base_addr + dc->card_type / 2;
  1087. unsigned int i;
  1088. dc->reg_fcr = (void __iomem *)(offset + R_FCR);
  1089. dc->reg_iir = (void __iomem *)(offset + R_IIR);
  1090. dc->reg_ier = (void __iomem *)(offset + R_IER);
  1091. dc->last_ier = 0;
  1092. dc->flip = 0;
  1093. dc->port[PORT_MDM].token_dl = MDM_DL;
  1094. dc->port[PORT_DIAG].token_dl = DIAG_DL;
  1095. dc->port[PORT_APP1].token_dl = APP1_DL;
  1096. dc->port[PORT_APP2].token_dl = APP2_DL;
  1097. for (i = 0; i < MAX_PORT; i++)
  1098. init_waitqueue_head(&dc->port[i].tty_wait);
  1099. }
  1100. static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
  1101. char *buf)
  1102. {
  1103. const struct nozomi *dc = dev_get_drvdata(dev);
  1104. return sprintf(buf, "%d\n", dc->card_type);
  1105. }
  1106. static DEVICE_ATTR_RO(card_type);
  1107. static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
  1108. char *buf)
  1109. {
  1110. const struct nozomi *dc = dev_get_drvdata(dev);
  1111. return sprintf(buf, "%u\n", dc->open_ttys);
  1112. }
  1113. static DEVICE_ATTR_RO(open_ttys);
  1114. static void make_sysfs_files(struct nozomi *dc)
  1115. {
  1116. if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
  1117. dev_err(&dc->pdev->dev,
  1118. "Could not create sysfs file for card_type\n");
  1119. if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
  1120. dev_err(&dc->pdev->dev,
  1121. "Could not create sysfs file for open_ttys\n");
  1122. }
  1123. static void remove_sysfs_files(struct nozomi *dc)
  1124. {
  1125. device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
  1126. device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
  1127. }
  1128. /* Allocate memory for one device */
  1129. static int nozomi_card_init(struct pci_dev *pdev,
  1130. const struct pci_device_id *ent)
  1131. {
  1132. int ret;
  1133. struct nozomi *dc = NULL;
  1134. int ndev_idx;
  1135. int i;
  1136. for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
  1137. if (!ndevs[ndev_idx])
  1138. break;
  1139. if (ndev_idx >= ARRAY_SIZE(ndevs)) {
  1140. dev_err(&pdev->dev, "no free tty range for this card left\n");
  1141. ret = -EIO;
  1142. goto err;
  1143. }
  1144. dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
  1145. if (unlikely(!dc)) {
  1146. dev_err(&pdev->dev, "Could not allocate memory\n");
  1147. ret = -ENOMEM;
  1148. goto err_free;
  1149. }
  1150. dc->pdev = pdev;
  1151. ret = pci_enable_device(dc->pdev);
  1152. if (ret) {
  1153. dev_err(&pdev->dev, "Failed to enable PCI Device\n");
  1154. goto err_free;
  1155. }
  1156. ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
  1157. if (ret) {
  1158. dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
  1159. (int) /* nozomi_private.io_addr */ 0);
  1160. goto err_disable_device;
  1161. }
  1162. /* Find out what card type it is */
  1163. nozomi_get_card_type(dc);
  1164. dc->base_addr = pci_iomap(dc->pdev, 0, dc->card_type);
  1165. if (!dc->base_addr) {
  1166. dev_err(&pdev->dev, "Unable to map card MMIO\n");
  1167. ret = -ENODEV;
  1168. goto err_rel_regs;
  1169. }
  1170. dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
  1171. if (!dc->send_buf) {
  1172. dev_err(&pdev->dev, "Could not allocate send buffer?\n");
  1173. ret = -ENOMEM;
  1174. goto err_free_sbuf;
  1175. }
  1176. for (i = PORT_MDM; i < MAX_PORT; i++) {
  1177. if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
  1178. GFP_KERNEL)) {
  1179. dev_err(&pdev->dev,
  1180. "Could not allocate kfifo buffer\n");
  1181. ret = -ENOMEM;
  1182. goto err_free_kfifo;
  1183. }
  1184. }
  1185. spin_lock_init(&dc->spin_mutex);
  1186. nozomi_setup_private_data(dc);
  1187. /* Disable all interrupts */
  1188. dc->last_ier = 0;
  1189. writew(dc->last_ier, dc->reg_ier);
  1190. ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
  1191. NOZOMI_NAME, dc);
  1192. if (unlikely(ret)) {
  1193. dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
  1194. goto err_free_all_kfifo;
  1195. }
  1196. DBG1("base_addr: %p", dc->base_addr);
  1197. make_sysfs_files(dc);
  1198. dc->index_start = ndev_idx * MAX_PORT;
  1199. ndevs[ndev_idx] = dc;
  1200. pci_set_drvdata(pdev, dc);
  1201. /* Enable RESET interrupt */
  1202. dc->last_ier = RESET;
  1203. iowrite16(dc->last_ier, dc->reg_ier);
  1204. dc->state = NOZOMI_STATE_ENABLED;
  1205. for (i = 0; i < MAX_PORT; i++) {
  1206. struct device *tty_dev;
  1207. struct port *port = &dc->port[i];
  1208. port->dc = dc;
  1209. tty_port_init(&port->port);
  1210. port->port.ops = &noz_tty_port_ops;
  1211. tty_dev = tty_port_register_device(&port->port, ntty_driver,
  1212. dc->index_start + i, &pdev->dev);
  1213. if (IS_ERR(tty_dev)) {
  1214. ret = PTR_ERR(tty_dev);
  1215. dev_err(&pdev->dev, "Could not allocate tty?\n");
  1216. tty_port_destroy(&port->port);
  1217. goto err_free_tty;
  1218. }
  1219. }
  1220. return 0;
  1221. err_free_tty:
  1222. for (i--; i >= 0; i--) {
  1223. tty_unregister_device(ntty_driver, dc->index_start + i);
  1224. tty_port_destroy(&dc->port[i].port);
  1225. }
  1226. free_irq(pdev->irq, dc);
  1227. err_free_all_kfifo:
  1228. i = MAX_PORT;
  1229. err_free_kfifo:
  1230. for (i--; i >= PORT_MDM; i--)
  1231. kfifo_free(&dc->port[i].fifo_ul);
  1232. err_free_sbuf:
  1233. kfree(dc->send_buf);
  1234. iounmap(dc->base_addr);
  1235. err_rel_regs:
  1236. pci_release_regions(pdev);
  1237. err_disable_device:
  1238. pci_disable_device(pdev);
  1239. err_free:
  1240. kfree(dc);
  1241. err:
  1242. return ret;
  1243. }
  1244. static void tty_exit(struct nozomi *dc)
  1245. {
  1246. unsigned int i;
  1247. for (i = 0; i < MAX_PORT; ++i)
  1248. tty_port_tty_hangup(&dc->port[i].port, false);
  1249. /* Racy below - surely should wait for scheduled work to be done or
  1250. complete off a hangup method ? */
  1251. while (dc->open_ttys)
  1252. msleep(1);
  1253. for (i = 0; i < MAX_PORT; ++i) {
  1254. tty_unregister_device(ntty_driver, dc->index_start + i);
  1255. tty_port_destroy(&dc->port[i].port);
  1256. }
  1257. }
  1258. /* Deallocate memory for one device */
  1259. static void nozomi_card_exit(struct pci_dev *pdev)
  1260. {
  1261. int i;
  1262. struct ctrl_ul ctrl;
  1263. struct nozomi *dc = pci_get_drvdata(pdev);
  1264. /* Disable all interrupts */
  1265. dc->last_ier = 0;
  1266. writew(dc->last_ier, dc->reg_ier);
  1267. tty_exit(dc);
  1268. /* Send 0x0001, command card to resend the reset token. */
  1269. /* This is to get the reset when the module is reloaded. */
  1270. ctrl.port = 0x00;
  1271. ctrl.reserved = 0;
  1272. ctrl.RTS = 0;
  1273. ctrl.DTR = 1;
  1274. DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
  1275. /* Setup dc->reg addresses to we can use defines here */
  1276. write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
  1277. writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
  1278. remove_sysfs_files(dc);
  1279. free_irq(pdev->irq, dc);
  1280. for (i = 0; i < MAX_PORT; i++)
  1281. kfifo_free(&dc->port[i].fifo_ul);
  1282. kfree(dc->send_buf);
  1283. iounmap(dc->base_addr);
  1284. pci_release_regions(pdev);
  1285. pci_disable_device(pdev);
  1286. ndevs[dc->index_start / MAX_PORT] = NULL;
  1287. kfree(dc);
  1288. }
  1289. static void set_rts(const struct tty_struct *tty, int rts)
  1290. {
  1291. struct port *port = get_port_by_tty(tty);
  1292. port->ctrl_ul.RTS = rts;
  1293. port->update_flow_control = 1;
  1294. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1295. }
  1296. static void set_dtr(const struct tty_struct *tty, int dtr)
  1297. {
  1298. struct port *port = get_port_by_tty(tty);
  1299. DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
  1300. port->ctrl_ul.DTR = dtr;
  1301. port->update_flow_control = 1;
  1302. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1303. }
  1304. /*
  1305. * ----------------------------------------------------------------------------
  1306. * TTY code
  1307. * ----------------------------------------------------------------------------
  1308. */
  1309. static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
  1310. {
  1311. struct port *port = get_port_by_tty(tty);
  1312. struct nozomi *dc = get_dc_by_tty(tty);
  1313. int ret;
  1314. if (!port || !dc || dc->state != NOZOMI_STATE_READY)
  1315. return -ENODEV;
  1316. ret = tty_standard_install(driver, tty);
  1317. if (ret == 0)
  1318. tty->driver_data = port;
  1319. return ret;
  1320. }
  1321. static void ntty_cleanup(struct tty_struct *tty)
  1322. {
  1323. tty->driver_data = NULL;
  1324. }
  1325. static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
  1326. {
  1327. struct port *port = container_of(tport, struct port, port);
  1328. struct nozomi *dc = port->dc;
  1329. unsigned long flags;
  1330. DBG1("open: %d", port->token_dl);
  1331. spin_lock_irqsave(&dc->spin_mutex, flags);
  1332. dc->last_ier = dc->last_ier | port->token_dl;
  1333. writew(dc->last_ier, dc->reg_ier);
  1334. dc->open_ttys++;
  1335. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1336. printk("noz: activated %d: %p\n", tty->index, tport);
  1337. return 0;
  1338. }
  1339. static int ntty_open(struct tty_struct *tty, struct file *filp)
  1340. {
  1341. struct port *port = tty->driver_data;
  1342. return tty_port_open(&port->port, tty, filp);
  1343. }
  1344. static void ntty_shutdown(struct tty_port *tport)
  1345. {
  1346. struct port *port = container_of(tport, struct port, port);
  1347. struct nozomi *dc = port->dc;
  1348. unsigned long flags;
  1349. DBG1("close: %d", port->token_dl);
  1350. spin_lock_irqsave(&dc->spin_mutex, flags);
  1351. dc->last_ier &= ~(port->token_dl);
  1352. writew(dc->last_ier, dc->reg_ier);
  1353. dc->open_ttys--;
  1354. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1355. printk("noz: shutdown %p\n", tport);
  1356. }
  1357. static void ntty_close(struct tty_struct *tty, struct file *filp)
  1358. {
  1359. struct port *port = tty->driver_data;
  1360. if (port)
  1361. tty_port_close(&port->port, tty, filp);
  1362. }
  1363. static void ntty_hangup(struct tty_struct *tty)
  1364. {
  1365. struct port *port = tty->driver_data;
  1366. tty_port_hangup(&port->port);
  1367. }
  1368. /*
  1369. * called when the userspace process writes to the tty (/dev/noz*).
  1370. * Data is inserted into a fifo, which is then read and transferred to the modem.
  1371. */
  1372. static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
  1373. int count)
  1374. {
  1375. int rval = -EINVAL;
  1376. struct nozomi *dc = get_dc_by_tty(tty);
  1377. struct port *port = tty->driver_data;
  1378. unsigned long flags;
  1379. if (!dc || !port)
  1380. return -ENODEV;
  1381. rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
  1382. spin_lock_irqsave(&dc->spin_mutex, flags);
  1383. /* CTS is only valid on the modem channel */
  1384. if (port == &(dc->port[PORT_MDM])) {
  1385. if (port->ctrl_dl.CTS) {
  1386. DBG4("Enable interrupt");
  1387. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1388. } else {
  1389. dev_err(&dc->pdev->dev,
  1390. "CTS not active on modem port?\n");
  1391. }
  1392. } else {
  1393. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1394. }
  1395. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1396. return rval;
  1397. }
  1398. /*
  1399. * Calculate how much is left in device
  1400. * This method is called by the upper tty layer.
  1401. * #according to sources N_TTY.c it expects a value >= 0 and
  1402. * does not check for negative values.
  1403. *
  1404. * If the port is unplugged report lots of room and let the bits
  1405. * dribble away so we don't block anything.
  1406. */
  1407. static unsigned int ntty_write_room(struct tty_struct *tty)
  1408. {
  1409. struct port *port = tty->driver_data;
  1410. unsigned int room = 4096;
  1411. const struct nozomi *dc = get_dc_by_tty(tty);
  1412. if (dc)
  1413. room = kfifo_avail(&port->fifo_ul);
  1414. return room;
  1415. }
  1416. /* Gets io control parameters */
  1417. static int ntty_tiocmget(struct tty_struct *tty)
  1418. {
  1419. const struct port *port = tty->driver_data;
  1420. const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
  1421. const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
  1422. /* Note: these could change under us but it is not clear this
  1423. matters if so */
  1424. return (ctrl_ul->RTS ? TIOCM_RTS : 0)
  1425. | (ctrl_ul->DTR ? TIOCM_DTR : 0)
  1426. | (ctrl_dl->DCD ? TIOCM_CAR : 0)
  1427. | (ctrl_dl->RI ? TIOCM_RNG : 0)
  1428. | (ctrl_dl->DSR ? TIOCM_DSR : 0)
  1429. | (ctrl_dl->CTS ? TIOCM_CTS : 0);
  1430. }
  1431. /* Sets io controls parameters */
  1432. static int ntty_tiocmset(struct tty_struct *tty,
  1433. unsigned int set, unsigned int clear)
  1434. {
  1435. struct nozomi *dc = get_dc_by_tty(tty);
  1436. unsigned long flags;
  1437. spin_lock_irqsave(&dc->spin_mutex, flags);
  1438. if (set & TIOCM_RTS)
  1439. set_rts(tty, 1);
  1440. else if (clear & TIOCM_RTS)
  1441. set_rts(tty, 0);
  1442. if (set & TIOCM_DTR)
  1443. set_dtr(tty, 1);
  1444. else if (clear & TIOCM_DTR)
  1445. set_dtr(tty, 0);
  1446. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1447. return 0;
  1448. }
  1449. static int ntty_cflags_changed(struct port *port, unsigned long flags,
  1450. struct async_icount *cprev)
  1451. {
  1452. const struct async_icount cnow = port->tty_icount;
  1453. int ret;
  1454. ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng))
  1455. || ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr))
  1456. || ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd))
  1457. || ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
  1458. *cprev = cnow;
  1459. return ret;
  1460. }
  1461. static int ntty_tiocgicount(struct tty_struct *tty,
  1462. struct serial_icounter_struct *icount)
  1463. {
  1464. struct port *port = tty->driver_data;
  1465. const struct async_icount cnow = port->tty_icount;
  1466. icount->cts = cnow.cts;
  1467. icount->dsr = cnow.dsr;
  1468. icount->rng = cnow.rng;
  1469. icount->dcd = cnow.dcd;
  1470. icount->rx = cnow.rx;
  1471. icount->tx = cnow.tx;
  1472. icount->frame = cnow.frame;
  1473. icount->overrun = cnow.overrun;
  1474. icount->parity = cnow.parity;
  1475. icount->brk = cnow.brk;
  1476. icount->buf_overrun = cnow.buf_overrun;
  1477. return 0;
  1478. }
  1479. static int ntty_ioctl(struct tty_struct *tty,
  1480. unsigned int cmd, unsigned long arg)
  1481. {
  1482. struct port *port = tty->driver_data;
  1483. int rval = -ENOIOCTLCMD;
  1484. switch (cmd) {
  1485. case TIOCMIWAIT: {
  1486. struct async_icount cprev = port->tty_icount;
  1487. rval = wait_event_interruptible(port->tty_wait,
  1488. ntty_cflags_changed(port, arg, &cprev));
  1489. break;
  1490. }
  1491. default:
  1492. DBG1("ERR: 0x%08X, %d", cmd, cmd);
  1493. break;
  1494. }
  1495. return rval;
  1496. }
  1497. /*
  1498. * Called by the upper tty layer when tty buffers are ready
  1499. * to receive data again after a call to throttle.
  1500. */
  1501. static void ntty_unthrottle(struct tty_struct *tty)
  1502. {
  1503. struct nozomi *dc = get_dc_by_tty(tty);
  1504. unsigned long flags;
  1505. spin_lock_irqsave(&dc->spin_mutex, flags);
  1506. enable_transmit_dl(tty->index % MAX_PORT, dc);
  1507. set_rts(tty, 1);
  1508. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1509. }
  1510. /*
  1511. * Called by the upper tty layer when the tty buffers are almost full.
  1512. * The driver should stop send more data.
  1513. */
  1514. static void ntty_throttle(struct tty_struct *tty)
  1515. {
  1516. struct nozomi *dc = get_dc_by_tty(tty);
  1517. unsigned long flags;
  1518. spin_lock_irqsave(&dc->spin_mutex, flags);
  1519. set_rts(tty, 0);
  1520. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1521. }
  1522. /* Returns number of chars in buffer, called by tty layer */
  1523. static unsigned int ntty_chars_in_buffer(struct tty_struct *tty)
  1524. {
  1525. struct port *port = tty->driver_data;
  1526. struct nozomi *dc = get_dc_by_tty(tty);
  1527. if (unlikely(!dc || !port))
  1528. return 0;
  1529. return kfifo_len(&port->fifo_ul);
  1530. }
  1531. static const struct tty_port_operations noz_tty_port_ops = {
  1532. .activate = ntty_activate,
  1533. .shutdown = ntty_shutdown,
  1534. };
  1535. static const struct tty_operations tty_ops = {
  1536. .ioctl = ntty_ioctl,
  1537. .open = ntty_open,
  1538. .close = ntty_close,
  1539. .hangup = ntty_hangup,
  1540. .write = ntty_write,
  1541. .write_room = ntty_write_room,
  1542. .unthrottle = ntty_unthrottle,
  1543. .throttle = ntty_throttle,
  1544. .chars_in_buffer = ntty_chars_in_buffer,
  1545. .tiocmget = ntty_tiocmget,
  1546. .tiocmset = ntty_tiocmset,
  1547. .get_icount = ntty_tiocgicount,
  1548. .install = ntty_install,
  1549. .cleanup = ntty_cleanup,
  1550. };
  1551. /* Module initialization */
  1552. static struct pci_driver nozomi_driver = {
  1553. .name = NOZOMI_NAME,
  1554. .id_table = nozomi_pci_tbl,
  1555. .probe = nozomi_card_init,
  1556. .remove = nozomi_card_exit,
  1557. };
  1558. static __init int nozomi_init(void)
  1559. {
  1560. int ret;
  1561. ntty_driver = tty_alloc_driver(NTTY_TTY_MAXMINORS, TTY_DRIVER_REAL_RAW |
  1562. TTY_DRIVER_DYNAMIC_DEV);
  1563. if (IS_ERR(ntty_driver))
  1564. return PTR_ERR(ntty_driver);
  1565. ntty_driver->driver_name = NOZOMI_NAME_TTY;
  1566. ntty_driver->name = "noz";
  1567. ntty_driver->major = 0;
  1568. ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1569. ntty_driver->subtype = SERIAL_TYPE_NORMAL;
  1570. ntty_driver->init_termios = tty_std_termios;
  1571. ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
  1572. HUPCL | CLOCAL;
  1573. ntty_driver->init_termios.c_ispeed = 115200;
  1574. ntty_driver->init_termios.c_ospeed = 115200;
  1575. tty_set_operations(ntty_driver, &tty_ops);
  1576. ret = tty_register_driver(ntty_driver);
  1577. if (ret) {
  1578. printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
  1579. goto free_tty;
  1580. }
  1581. ret = pci_register_driver(&nozomi_driver);
  1582. if (ret) {
  1583. printk(KERN_ERR "Nozomi: can't register pci driver\n");
  1584. goto unr_tty;
  1585. }
  1586. return 0;
  1587. unr_tty:
  1588. tty_unregister_driver(ntty_driver);
  1589. free_tty:
  1590. tty_driver_kref_put(ntty_driver);
  1591. return ret;
  1592. }
  1593. static __exit void nozomi_exit(void)
  1594. {
  1595. pci_unregister_driver(&nozomi_driver);
  1596. tty_unregister_driver(ntty_driver);
  1597. tty_driver_kref_put(ntty_driver);
  1598. }
  1599. module_init(nozomi_init);
  1600. module_exit(nozomi_exit);
  1601. MODULE_LICENSE("Dual BSD/GPL");
  1602. MODULE_DESCRIPTION("Nozomi driver");