nozomi.c 48 KB

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