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