nozomi.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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 R_IIR 0x0000 /* Interrupt Identity Register */
  118. #define R_FCR 0x0000 /* Flow Control Register */
  119. #define R_IER 0x0004 /* Interrupt Enable Register */
  120. #define CONFIG_MAGIC 0xEFEFFEFE
  121. #define TOGGLE_VALID 0x0000
  122. /* Definition of interrupt tokens */
  123. #define MDM_DL1 0x0001
  124. #define MDM_UL1 0x0002
  125. #define MDM_DL2 0x0004
  126. #define MDM_UL2 0x0008
  127. #define DIAG_DL1 0x0010
  128. #define DIAG_DL2 0x0020
  129. #define DIAG_UL 0x0040
  130. #define APP1_DL 0x0080
  131. #define APP1_UL 0x0100
  132. #define APP2_DL 0x0200
  133. #define APP2_UL 0x0400
  134. #define CTRL_DL 0x0800
  135. #define CTRL_UL 0x1000
  136. #define RESET 0x8000
  137. #define MDM_DL (MDM_DL1 | MDM_DL2)
  138. #define MDM_UL (MDM_UL1 | MDM_UL2)
  139. #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
  140. /* modem signal definition */
  141. #define CTRL_DSR 0x0001
  142. #define CTRL_DCD 0x0002
  143. #define CTRL_RI 0x0004
  144. #define CTRL_CTS 0x0008
  145. #define CTRL_DTR 0x0001
  146. #define CTRL_RTS 0x0002
  147. #define MAX_PORT 4
  148. #define NOZOMI_MAX_PORTS 5
  149. #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
  150. /* Type definitions */
  151. /*
  152. * There are two types of nozomi cards,
  153. * one with 2048 memory and with 8192 memory
  154. */
  155. enum card_type {
  156. F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
  157. F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
  158. };
  159. /* Initialization states a card can be in */
  160. enum card_state {
  161. NOZOMI_STATE_UKNOWN = 0,
  162. NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
  163. NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
  164. NOZOMI_STATE_READY = 3, /* flowcontrols received */
  165. };
  166. /* Two different toggle channels exist */
  167. enum channel_type {
  168. CH_A = 0,
  169. CH_B = 1,
  170. };
  171. /* Port definition for the card regarding flow control */
  172. enum ctrl_port_type {
  173. CTRL_CMD = 0,
  174. CTRL_MDM = 1,
  175. CTRL_DIAG = 2,
  176. CTRL_APP1 = 3,
  177. CTRL_APP2 = 4,
  178. CTRL_ERROR = -1,
  179. };
  180. /* Ports that the nozomi has */
  181. enum port_type {
  182. PORT_MDM = 0,
  183. PORT_DIAG = 1,
  184. PORT_APP1 = 2,
  185. PORT_APP2 = 3,
  186. PORT_CTRL = 4,
  187. PORT_ERROR = -1,
  188. };
  189. #ifdef __BIG_ENDIAN
  190. /* Big endian */
  191. struct toggles {
  192. unsigned int enabled:5; /*
  193. * Toggle fields are valid if enabled is 0,
  194. * else A-channels must always be used.
  195. */
  196. unsigned int diag_dl:1;
  197. unsigned int mdm_dl:1;
  198. unsigned int mdm_ul:1;
  199. } __attribute__ ((packed));
  200. /* Configuration table to read at startup of card */
  201. /* Is for now only needed during initialization phase */
  202. struct config_table {
  203. u32 signature;
  204. u16 product_information;
  205. u16 version;
  206. u8 pad3[3];
  207. struct toggles toggle;
  208. u8 pad1[4];
  209. u16 dl_mdm_len1; /*
  210. * If this is 64, it can hold
  211. * 60 bytes + 4 that is length field
  212. */
  213. u16 dl_start;
  214. u16 dl_diag_len1;
  215. u16 dl_mdm_len2; /*
  216. * If this is 64, it can hold
  217. * 60 bytes + 4 that is length field
  218. */
  219. u16 dl_app1_len;
  220. u16 dl_diag_len2;
  221. u16 dl_ctrl_len;
  222. u16 dl_app2_len;
  223. u8 pad2[16];
  224. u16 ul_mdm_len1;
  225. u16 ul_start;
  226. u16 ul_diag_len;
  227. u16 ul_mdm_len2;
  228. u16 ul_app1_len;
  229. u16 ul_app2_len;
  230. u16 ul_ctrl_len;
  231. } __attribute__ ((packed));
  232. /* This stores all control downlink flags */
  233. struct ctrl_dl {
  234. u8 port;
  235. unsigned int reserved:4;
  236. unsigned int CTS:1;
  237. unsigned int RI:1;
  238. unsigned int DCD:1;
  239. unsigned int DSR:1;
  240. } __attribute__ ((packed));
  241. /* This stores all control uplink flags */
  242. struct ctrl_ul {
  243. u8 port;
  244. unsigned int reserved:6;
  245. unsigned int RTS:1;
  246. unsigned int DTR:1;
  247. } __attribute__ ((packed));
  248. #else
  249. /* Little endian */
  250. /* This represents the toggle information */
  251. struct toggles {
  252. unsigned int mdm_ul:1;
  253. unsigned int mdm_dl:1;
  254. unsigned int diag_dl:1;
  255. unsigned int enabled:5; /*
  256. * Toggle fields are valid if enabled is 0,
  257. * else A-channels must always be used.
  258. */
  259. } __attribute__ ((packed));
  260. /* Configuration table to read at startup of card */
  261. struct config_table {
  262. u32 signature;
  263. u16 version;
  264. u16 product_information;
  265. struct toggles toggle;
  266. u8 pad1[7];
  267. u16 dl_start;
  268. u16 dl_mdm_len1; /*
  269. * If this is 64, it can hold
  270. * 60 bytes + 4 that is length field
  271. */
  272. u16 dl_mdm_len2;
  273. u16 dl_diag_len1;
  274. u16 dl_diag_len2;
  275. u16 dl_app1_len;
  276. u16 dl_app2_len;
  277. u16 dl_ctrl_len;
  278. u8 pad2[16];
  279. u16 ul_start;
  280. u16 ul_mdm_len2;
  281. u16 ul_mdm_len1;
  282. u16 ul_diag_len;
  283. u16 ul_app1_len;
  284. u16 ul_app2_len;
  285. u16 ul_ctrl_len;
  286. } __attribute__ ((packed));
  287. /* This stores all control downlink flags */
  288. struct ctrl_dl {
  289. unsigned int DSR:1;
  290. unsigned int DCD:1;
  291. unsigned int RI:1;
  292. unsigned int CTS:1;
  293. unsigned int reserverd:4;
  294. u8 port;
  295. } __attribute__ ((packed));
  296. /* This stores all control uplink flags */
  297. struct ctrl_ul {
  298. unsigned int DTR:1;
  299. unsigned int RTS:1;
  300. unsigned int reserved:6;
  301. u8 port;
  302. } __attribute__ ((packed));
  303. #endif
  304. /* This holds all information that is needed regarding a port */
  305. struct port {
  306. struct tty_port port;
  307. u8 update_flow_control;
  308. struct ctrl_ul ctrl_ul;
  309. struct ctrl_dl ctrl_dl;
  310. struct kfifo fifo_ul;
  311. void __iomem *dl_addr[2];
  312. u32 dl_size[2];
  313. u8 toggle_dl;
  314. void __iomem *ul_addr[2];
  315. u32 ul_size[2];
  316. u8 toggle_ul;
  317. u16 token_dl;
  318. /* mutex to ensure one access patch to this port */
  319. struct mutex tty_sem;
  320. wait_queue_head_t tty_wait;
  321. struct async_icount tty_icount;
  322. struct nozomi *dc;
  323. };
  324. /* Private data one for each card in the system */
  325. struct nozomi {
  326. void __iomem *base_addr;
  327. unsigned long flip;
  328. /* Pointers to registers */
  329. void __iomem *reg_iir;
  330. void __iomem *reg_fcr;
  331. void __iomem *reg_ier;
  332. u16 last_ier;
  333. enum card_type card_type;
  334. struct config_table config_table; /* Configuration table */
  335. struct pci_dev *pdev;
  336. struct port port[NOZOMI_MAX_PORTS];
  337. u8 *send_buf;
  338. spinlock_t spin_mutex; /* secures access to registers and tty */
  339. unsigned int index_start;
  340. enum card_state state;
  341. u32 open_ttys;
  342. };
  343. /* This is a data packet that is read or written to/from card */
  344. struct buffer {
  345. u32 size; /* size is the length of the data buffer */
  346. u8 *data;
  347. } __attribute__ ((packed));
  348. /* Global variables */
  349. static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
  350. {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
  351. {},
  352. };
  353. MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
  354. static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
  355. static struct tty_driver *ntty_driver;
  356. static const struct tty_port_operations noz_tty_port_ops;
  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. memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
  598. memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
  599. }
  600. /* Enable control channel */
  601. dc->last_ier = dc->last_ier | CTRL_DL;
  602. writew(dc->last_ier, dc->reg_ier);
  603. dc->state = NOZOMI_STATE_ALLOCATED;
  604. dev_info(&dc->pdev->dev, "Initialization OK!\n");
  605. return 1;
  606. }
  607. if ((dc->config_table.version > 0)
  608. && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
  609. u32 offset = 0;
  610. DBG1("First phase: pushing upload buffers, clearing download");
  611. dev_info(&dc->pdev->dev, "Version of card: %d\n",
  612. dc->config_table.version);
  613. /* Here we should disable all I/O over F32. */
  614. setup_memory(dc);
  615. /*
  616. * We should send ALL channel pair tokens back along
  617. * with reset token
  618. */
  619. /* push upload modem buffers */
  620. write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
  621. (u32 *) &offset, 4);
  622. write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
  623. (u32 *) &offset, 4);
  624. writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
  625. DBG1("First phase done");
  626. }
  627. return 1;
  628. }
  629. /* Enable uplink interrupts */
  630. static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
  631. {
  632. static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
  633. if (port < NOZOMI_MAX_PORTS) {
  634. dc->last_ier |= mask[port];
  635. writew(dc->last_ier, dc->reg_ier);
  636. } else {
  637. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  638. }
  639. }
  640. /* Disable uplink interrupts */
  641. static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
  642. {
  643. static const u16 mask[] =
  644. {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
  645. if (port < NOZOMI_MAX_PORTS) {
  646. dc->last_ier &= mask[port];
  647. writew(dc->last_ier, dc->reg_ier);
  648. } else {
  649. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  650. }
  651. }
  652. /* Enable downlink interrupts */
  653. static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
  654. {
  655. static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
  656. if (port < NOZOMI_MAX_PORTS) {
  657. dc->last_ier |= mask[port];
  658. writew(dc->last_ier, dc->reg_ier);
  659. } else {
  660. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  661. }
  662. }
  663. /* Disable downlink interrupts */
  664. static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
  665. {
  666. static const u16 mask[] =
  667. {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
  668. if (port < NOZOMI_MAX_PORTS) {
  669. dc->last_ier &= mask[port];
  670. writew(dc->last_ier, dc->reg_ier);
  671. } else {
  672. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  673. }
  674. }
  675. /*
  676. * Return 1 - send buffer to card and ack.
  677. * Return 0 - don't ack, don't send buffer to card.
  678. */
  679. static int send_data(enum port_type index, struct nozomi *dc)
  680. {
  681. u32 size = 0;
  682. struct port *port = &dc->port[index];
  683. const u8 toggle = port->toggle_ul;
  684. void __iomem *addr = port->ul_addr[toggle];
  685. const u32 ul_size = port->ul_size[toggle];
  686. struct tty_struct *tty = tty_port_tty_get(&port->port);
  687. /* Get data from tty and place in buf for now */
  688. size = kfifo_out(&port->fifo_ul, dc->send_buf,
  689. ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
  690. if (size == 0) {
  691. DBG4("No more data to send, disable link:");
  692. tty_kref_put(tty);
  693. return 0;
  694. }
  695. /* DUMP(buf, size); */
  696. /* Write length + data */
  697. write_mem32(addr, (u32 *) &size, 4);
  698. write_mem32(addr + 4, (u32 *) dc->send_buf, size);
  699. if (tty)
  700. tty_wakeup(tty);
  701. tty_kref_put(tty);
  702. return 1;
  703. }
  704. /* If all data has been read, return 1, else 0 */
  705. static int receive_data(enum port_type index, struct nozomi *dc)
  706. {
  707. u8 buf[RECEIVE_BUF_MAX] = { 0 };
  708. int size;
  709. u32 offset = 4;
  710. struct port *port = &dc->port[index];
  711. void __iomem *addr = port->dl_addr[port->toggle_dl];
  712. struct tty_struct *tty = tty_port_tty_get(&port->port);
  713. int i, ret;
  714. if (unlikely(!tty)) {
  715. DBG1("tty not open for port: %d?", index);
  716. return 1;
  717. }
  718. read_mem32((u32 *) &size, addr, 4);
  719. /* DBG1( "%d bytes port: %d", size, index); */
  720. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  721. DBG1("No room in tty, don't read data, don't ack interrupt, "
  722. "disable interrupt");
  723. /* disable interrupt in downlink... */
  724. disable_transmit_dl(index, dc);
  725. ret = 0;
  726. goto put;
  727. }
  728. if (unlikely(size == 0)) {
  729. dev_err(&dc->pdev->dev, "size == 0?\n");
  730. ret = 1;
  731. goto put;
  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. ret = 1;
  750. put:
  751. tty_kref_put(tty);
  752. return ret;
  753. }
  754. /* Debug for interrupts */
  755. #ifdef DEBUG
  756. static char *interrupt2str(u16 interrupt)
  757. {
  758. static char buf[TMP_BUF_MAX];
  759. char *p = buf;
  760. interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
  761. interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  762. "MDM_DL2 ") : NULL;
  763. interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  764. "MDM_UL1 ") : NULL;
  765. interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  766. "MDM_UL2 ") : NULL;
  767. interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  768. "DIAG_DL1 ") : NULL;
  769. interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  770. "DIAG_DL2 ") : NULL;
  771. interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  772. "DIAG_UL ") : NULL;
  773. interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  774. "APP1_DL ") : NULL;
  775. interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  776. "APP2_DL ") : NULL;
  777. interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  778. "APP1_UL ") : NULL;
  779. interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  780. "APP2_UL ") : NULL;
  781. interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  782. "CTRL_DL ") : NULL;
  783. interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  784. "CTRL_UL ") : NULL;
  785. interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  786. "RESET ") : NULL;
  787. return buf;
  788. }
  789. #endif
  790. /*
  791. * Receive flow control
  792. * Return 1 - If ok, else 0
  793. */
  794. static int receive_flow_control(struct nozomi *dc)
  795. {
  796. enum port_type port = PORT_MDM;
  797. struct ctrl_dl ctrl_dl;
  798. struct ctrl_dl old_ctrl;
  799. u16 enable_ier = 0;
  800. read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
  801. switch (ctrl_dl.port) {
  802. case CTRL_CMD:
  803. DBG1("The Base Band sends this value as a response to a "
  804. "request for IMSI detach sent over the control "
  805. "channel uplink (see section 7.6.1).");
  806. break;
  807. case CTRL_MDM:
  808. port = PORT_MDM;
  809. enable_ier = MDM_DL;
  810. break;
  811. case CTRL_DIAG:
  812. port = PORT_DIAG;
  813. enable_ier = DIAG_DL;
  814. break;
  815. case CTRL_APP1:
  816. port = PORT_APP1;
  817. enable_ier = APP1_DL;
  818. break;
  819. case CTRL_APP2:
  820. port = PORT_APP2;
  821. enable_ier = APP2_DL;
  822. if (dc->state == NOZOMI_STATE_ALLOCATED) {
  823. /*
  824. * After card initialization the flow control
  825. * received for APP2 is always the last
  826. */
  827. dc->state = NOZOMI_STATE_READY;
  828. dev_info(&dc->pdev->dev, "Device READY!\n");
  829. }
  830. break;
  831. default:
  832. dev_err(&dc->pdev->dev,
  833. "ERROR: flow control received for non-existing port\n");
  834. return 0;
  835. };
  836. DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
  837. *((u16 *)&ctrl_dl));
  838. old_ctrl = dc->port[port].ctrl_dl;
  839. dc->port[port].ctrl_dl = ctrl_dl;
  840. if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
  841. DBG1("Disable interrupt (0x%04X) on port: %d",
  842. enable_ier, port);
  843. disable_transmit_ul(port, dc);
  844. } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
  845. if (kfifo_len(&dc->port[port].fifo_ul)) {
  846. DBG1("Enable interrupt (0x%04X) on port: %d",
  847. enable_ier, port);
  848. DBG1("Data in buffer [%d], enable transmit! ",
  849. kfifo_len(&dc->port[port].fifo_ul));
  850. enable_transmit_ul(port, dc);
  851. } else {
  852. DBG1("No data in buffer...");
  853. }
  854. }
  855. if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
  856. DBG1(" No change in mctrl");
  857. return 1;
  858. }
  859. /* Update statistics */
  860. if (old_ctrl.CTS != ctrl_dl.CTS)
  861. dc->port[port].tty_icount.cts++;
  862. if (old_ctrl.DSR != ctrl_dl.DSR)
  863. dc->port[port].tty_icount.dsr++;
  864. if (old_ctrl.RI != ctrl_dl.RI)
  865. dc->port[port].tty_icount.rng++;
  866. if (old_ctrl.DCD != ctrl_dl.DCD)
  867. dc->port[port].tty_icount.dcd++;
  868. wake_up_interruptible(&dc->port[port].tty_wait);
  869. DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
  870. port,
  871. dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
  872. dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
  873. return 1;
  874. }
  875. static enum ctrl_port_type port2ctrl(enum port_type port,
  876. const struct nozomi *dc)
  877. {
  878. switch (port) {
  879. case PORT_MDM:
  880. return CTRL_MDM;
  881. case PORT_DIAG:
  882. return CTRL_DIAG;
  883. case PORT_APP1:
  884. return CTRL_APP1;
  885. case PORT_APP2:
  886. return CTRL_APP2;
  887. default:
  888. dev_err(&dc->pdev->dev,
  889. "ERROR: send flow control " \
  890. "received for non-existing port\n");
  891. };
  892. return CTRL_ERROR;
  893. }
  894. /*
  895. * Send flow control, can only update one channel at a time
  896. * Return 0 - If we have updated all flow control
  897. * Return 1 - If we need to update more flow control, ack current enable more
  898. */
  899. static int send_flow_control(struct nozomi *dc)
  900. {
  901. u32 i, more_flow_control_to_be_updated = 0;
  902. u16 *ctrl;
  903. for (i = PORT_MDM; i < MAX_PORT; i++) {
  904. if (dc->port[i].update_flow_control) {
  905. if (more_flow_control_to_be_updated) {
  906. /* We have more flow control to be updated */
  907. return 1;
  908. }
  909. dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
  910. ctrl = (u16 *)&dc->port[i].ctrl_ul;
  911. write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
  912. (u32 *) ctrl, 2);
  913. dc->port[i].update_flow_control = 0;
  914. more_flow_control_to_be_updated = 1;
  915. }
  916. }
  917. return 0;
  918. }
  919. /*
  920. * Handle downlink data, ports that are handled are modem and diagnostics
  921. * Return 1 - ok
  922. * Return 0 - toggle fields are out of sync
  923. */
  924. static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
  925. u16 read_iir, u16 mask1, u16 mask2)
  926. {
  927. if (*toggle == 0 && read_iir & mask1) {
  928. if (receive_data(port, dc)) {
  929. writew(mask1, dc->reg_fcr);
  930. *toggle = !(*toggle);
  931. }
  932. if (read_iir & mask2) {
  933. if (receive_data(port, dc)) {
  934. writew(mask2, dc->reg_fcr);
  935. *toggle = !(*toggle);
  936. }
  937. }
  938. } else if (*toggle == 1 && read_iir & mask2) {
  939. if (receive_data(port, dc)) {
  940. writew(mask2, dc->reg_fcr);
  941. *toggle = !(*toggle);
  942. }
  943. if (read_iir & mask1) {
  944. if (receive_data(port, dc)) {
  945. writew(mask1, dc->reg_fcr);
  946. *toggle = !(*toggle);
  947. }
  948. }
  949. } else {
  950. dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
  951. *toggle);
  952. return 0;
  953. }
  954. return 1;
  955. }
  956. /*
  957. * Handle uplink data, this is currently for the modem port
  958. * Return 1 - ok
  959. * Return 0 - toggle field are out of sync
  960. */
  961. static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
  962. {
  963. u8 *toggle = &(dc->port[port].toggle_ul);
  964. if (*toggle == 0 && read_iir & MDM_UL1) {
  965. dc->last_ier &= ~MDM_UL;
  966. writew(dc->last_ier, dc->reg_ier);
  967. if (send_data(port, dc)) {
  968. writew(MDM_UL1, dc->reg_fcr);
  969. dc->last_ier = dc->last_ier | MDM_UL;
  970. writew(dc->last_ier, dc->reg_ier);
  971. *toggle = !*toggle;
  972. }
  973. if (read_iir & MDM_UL2) {
  974. dc->last_ier &= ~MDM_UL;
  975. writew(dc->last_ier, dc->reg_ier);
  976. if (send_data(port, dc)) {
  977. writew(MDM_UL2, dc->reg_fcr);
  978. dc->last_ier = dc->last_ier | MDM_UL;
  979. writew(dc->last_ier, dc->reg_ier);
  980. *toggle = !*toggle;
  981. }
  982. }
  983. } else if (*toggle == 1 && read_iir & MDM_UL2) {
  984. dc->last_ier &= ~MDM_UL;
  985. writew(dc->last_ier, dc->reg_ier);
  986. if (send_data(port, dc)) {
  987. writew(MDM_UL2, dc->reg_fcr);
  988. dc->last_ier = dc->last_ier | MDM_UL;
  989. writew(dc->last_ier, dc->reg_ier);
  990. *toggle = !*toggle;
  991. }
  992. if (read_iir & MDM_UL1) {
  993. dc->last_ier &= ~MDM_UL;
  994. writew(dc->last_ier, dc->reg_ier);
  995. if (send_data(port, dc)) {
  996. writew(MDM_UL1, dc->reg_fcr);
  997. dc->last_ier = dc->last_ier | MDM_UL;
  998. writew(dc->last_ier, dc->reg_ier);
  999. *toggle = !*toggle;
  1000. }
  1001. }
  1002. } else {
  1003. writew(read_iir & MDM_UL, dc->reg_fcr);
  1004. dev_err(&dc->pdev->dev, "port out of sync!\n");
  1005. return 0;
  1006. }
  1007. return 1;
  1008. }
  1009. static irqreturn_t interrupt_handler(int irq, void *dev_id)
  1010. {
  1011. struct nozomi *dc = dev_id;
  1012. unsigned int a;
  1013. u16 read_iir;
  1014. if (!dc)
  1015. return IRQ_NONE;
  1016. spin_lock(&dc->spin_mutex);
  1017. read_iir = readw(dc->reg_iir);
  1018. /* Card removed */
  1019. if (read_iir == (u16)-1)
  1020. goto none;
  1021. /*
  1022. * Just handle interrupt enabled in IER
  1023. * (by masking with dc->last_ier)
  1024. */
  1025. read_iir &= dc->last_ier;
  1026. if (read_iir == 0)
  1027. goto none;
  1028. DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
  1029. dc->last_ier);
  1030. if (read_iir & RESET) {
  1031. if (unlikely(!nozomi_read_config_table(dc))) {
  1032. dc->last_ier = 0x0;
  1033. writew(dc->last_ier, dc->reg_ier);
  1034. dev_err(&dc->pdev->dev, "Could not read status from "
  1035. "card, we should disable interface\n");
  1036. } else {
  1037. writew(RESET, dc->reg_fcr);
  1038. }
  1039. /* No more useful info if this was the reset interrupt. */
  1040. goto exit_handler;
  1041. }
  1042. if (read_iir & CTRL_UL) {
  1043. DBG1("CTRL_UL");
  1044. dc->last_ier &= ~CTRL_UL;
  1045. writew(dc->last_ier, dc->reg_ier);
  1046. if (send_flow_control(dc)) {
  1047. writew(CTRL_UL, dc->reg_fcr);
  1048. dc->last_ier = dc->last_ier | CTRL_UL;
  1049. writew(dc->last_ier, dc->reg_ier);
  1050. }
  1051. }
  1052. if (read_iir & CTRL_DL) {
  1053. receive_flow_control(dc);
  1054. writew(CTRL_DL, dc->reg_fcr);
  1055. }
  1056. if (read_iir & MDM_DL) {
  1057. if (!handle_data_dl(dc, PORT_MDM,
  1058. &(dc->port[PORT_MDM].toggle_dl), read_iir,
  1059. MDM_DL1, MDM_DL2)) {
  1060. dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
  1061. goto exit_handler;
  1062. }
  1063. }
  1064. if (read_iir & MDM_UL) {
  1065. if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
  1066. dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
  1067. goto exit_handler;
  1068. }
  1069. }
  1070. if (read_iir & DIAG_DL) {
  1071. if (!handle_data_dl(dc, PORT_DIAG,
  1072. &(dc->port[PORT_DIAG].toggle_dl), read_iir,
  1073. DIAG_DL1, DIAG_DL2)) {
  1074. dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
  1075. goto exit_handler;
  1076. }
  1077. }
  1078. if (read_iir & DIAG_UL) {
  1079. dc->last_ier &= ~DIAG_UL;
  1080. writew(dc->last_ier, dc->reg_ier);
  1081. if (send_data(PORT_DIAG, dc)) {
  1082. writew(DIAG_UL, dc->reg_fcr);
  1083. dc->last_ier = dc->last_ier | DIAG_UL;
  1084. writew(dc->last_ier, dc->reg_ier);
  1085. }
  1086. }
  1087. if (read_iir & APP1_DL) {
  1088. if (receive_data(PORT_APP1, dc))
  1089. writew(APP1_DL, dc->reg_fcr);
  1090. }
  1091. if (read_iir & APP1_UL) {
  1092. dc->last_ier &= ~APP1_UL;
  1093. writew(dc->last_ier, dc->reg_ier);
  1094. if (send_data(PORT_APP1, dc)) {
  1095. writew(APP1_UL, dc->reg_fcr);
  1096. dc->last_ier = dc->last_ier | APP1_UL;
  1097. writew(dc->last_ier, dc->reg_ier);
  1098. }
  1099. }
  1100. if (read_iir & APP2_DL) {
  1101. if (receive_data(PORT_APP2, dc))
  1102. writew(APP2_DL, dc->reg_fcr);
  1103. }
  1104. if (read_iir & APP2_UL) {
  1105. dc->last_ier &= ~APP2_UL;
  1106. writew(dc->last_ier, dc->reg_ier);
  1107. if (send_data(PORT_APP2, dc)) {
  1108. writew(APP2_UL, dc->reg_fcr);
  1109. dc->last_ier = dc->last_ier | APP2_UL;
  1110. writew(dc->last_ier, dc->reg_ier);
  1111. }
  1112. }
  1113. exit_handler:
  1114. spin_unlock(&dc->spin_mutex);
  1115. for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
  1116. struct tty_struct *tty;
  1117. if (test_and_clear_bit(a, &dc->flip)) {
  1118. tty = tty_port_tty_get(&dc->port[a].port);
  1119. if (tty)
  1120. tty_flip_buffer_push(tty);
  1121. tty_kref_put(tty);
  1122. }
  1123. }
  1124. return IRQ_HANDLED;
  1125. none:
  1126. spin_unlock(&dc->spin_mutex);
  1127. return IRQ_NONE;
  1128. }
  1129. static void nozomi_get_card_type(struct nozomi *dc)
  1130. {
  1131. int i;
  1132. u32 size = 0;
  1133. for (i = 0; i < 6; i++)
  1134. size += pci_resource_len(dc->pdev, i);
  1135. /* Assume card type F32_8 if no match */
  1136. dc->card_type = size == 2048 ? F32_2 : F32_8;
  1137. dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
  1138. }
  1139. static void nozomi_setup_private_data(struct nozomi *dc)
  1140. {
  1141. void __iomem *offset = dc->base_addr + dc->card_type / 2;
  1142. unsigned int i;
  1143. dc->reg_fcr = (void __iomem *)(offset + R_FCR);
  1144. dc->reg_iir = (void __iomem *)(offset + R_IIR);
  1145. dc->reg_ier = (void __iomem *)(offset + R_IER);
  1146. dc->last_ier = 0;
  1147. dc->flip = 0;
  1148. dc->port[PORT_MDM].token_dl = MDM_DL;
  1149. dc->port[PORT_DIAG].token_dl = DIAG_DL;
  1150. dc->port[PORT_APP1].token_dl = APP1_DL;
  1151. dc->port[PORT_APP2].token_dl = APP2_DL;
  1152. for (i = 0; i < MAX_PORT; i++)
  1153. init_waitqueue_head(&dc->port[i].tty_wait);
  1154. }
  1155. static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
  1156. char *buf)
  1157. {
  1158. const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
  1159. return sprintf(buf, "%d\n", dc->card_type);
  1160. }
  1161. static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
  1162. static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
  1163. char *buf)
  1164. {
  1165. const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
  1166. return sprintf(buf, "%u\n", dc->open_ttys);
  1167. }
  1168. static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
  1169. static void make_sysfs_files(struct nozomi *dc)
  1170. {
  1171. if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
  1172. dev_err(&dc->pdev->dev,
  1173. "Could not create sysfs file for card_type\n");
  1174. if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
  1175. dev_err(&dc->pdev->dev,
  1176. "Could not create sysfs file for open_ttys\n");
  1177. }
  1178. static void remove_sysfs_files(struct nozomi *dc)
  1179. {
  1180. device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
  1181. device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
  1182. }
  1183. /* Allocate memory for one device */
  1184. static int __devinit nozomi_card_init(struct pci_dev *pdev,
  1185. const struct pci_device_id *ent)
  1186. {
  1187. resource_size_t start;
  1188. int ret;
  1189. struct nozomi *dc = NULL;
  1190. int ndev_idx;
  1191. int i;
  1192. dev_dbg(&pdev->dev, "Init, new card found\n");
  1193. for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
  1194. if (!ndevs[ndev_idx])
  1195. break;
  1196. if (ndev_idx >= ARRAY_SIZE(ndevs)) {
  1197. dev_err(&pdev->dev, "no free tty range for this card left\n");
  1198. ret = -EIO;
  1199. goto err;
  1200. }
  1201. dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
  1202. if (unlikely(!dc)) {
  1203. dev_err(&pdev->dev, "Could not allocate memory\n");
  1204. ret = -ENOMEM;
  1205. goto err_free;
  1206. }
  1207. dc->pdev = pdev;
  1208. ret = pci_enable_device(dc->pdev);
  1209. if (ret) {
  1210. dev_err(&pdev->dev, "Failed to enable PCI Device\n");
  1211. goto err_free;
  1212. }
  1213. ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
  1214. if (ret) {
  1215. dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
  1216. (int) /* nozomi_private.io_addr */ 0);
  1217. goto err_disable_device;
  1218. }
  1219. start = pci_resource_start(dc->pdev, 0);
  1220. if (start == 0) {
  1221. dev_err(&pdev->dev, "No I/O address for card detected\n");
  1222. ret = -ENODEV;
  1223. goto err_rel_regs;
  1224. }
  1225. /* Find out what card type it is */
  1226. nozomi_get_card_type(dc);
  1227. dc->base_addr = ioremap_nocache(start, dc->card_type);
  1228. if (!dc->base_addr) {
  1229. dev_err(&pdev->dev, "Unable to map card MMIO\n");
  1230. ret = -ENODEV;
  1231. goto err_rel_regs;
  1232. }
  1233. dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
  1234. if (!dc->send_buf) {
  1235. dev_err(&pdev->dev, "Could not allocate send buffer?\n");
  1236. ret = -ENOMEM;
  1237. goto err_free_sbuf;
  1238. }
  1239. for (i = PORT_MDM; i < MAX_PORT; i++) {
  1240. if (kfifo_alloc(&dc->port[i].fifo_ul,
  1241. FIFO_BUFFER_SIZE_UL, GFP_ATOMIC)) {
  1242. dev_err(&pdev->dev,
  1243. "Could not allocate kfifo buffer\n");
  1244. ret = -ENOMEM;
  1245. goto err_free_kfifo;
  1246. }
  1247. }
  1248. spin_lock_init(&dc->spin_mutex);
  1249. nozomi_setup_private_data(dc);
  1250. /* Disable all interrupts */
  1251. dc->last_ier = 0;
  1252. writew(dc->last_ier, dc->reg_ier);
  1253. ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
  1254. NOZOMI_NAME, dc);
  1255. if (unlikely(ret)) {
  1256. dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
  1257. goto err_free_kfifo;
  1258. }
  1259. DBG1("base_addr: %p", dc->base_addr);
  1260. make_sysfs_files(dc);
  1261. dc->index_start = ndev_idx * MAX_PORT;
  1262. ndevs[ndev_idx] = dc;
  1263. pci_set_drvdata(pdev, dc);
  1264. /* Enable RESET interrupt */
  1265. dc->last_ier = RESET;
  1266. iowrite16(dc->last_ier, dc->reg_ier);
  1267. dc->state = NOZOMI_STATE_ENABLED;
  1268. for (i = 0; i < MAX_PORT; i++) {
  1269. struct device *tty_dev;
  1270. struct port *port = &dc->port[i];
  1271. port->dc = dc;
  1272. mutex_init(&port->tty_sem);
  1273. tty_port_init(&port->port);
  1274. port->port.ops = &noz_tty_port_ops;
  1275. tty_dev = tty_register_device(ntty_driver, dc->index_start + i,
  1276. &pdev->dev);
  1277. if (IS_ERR(tty_dev)) {
  1278. ret = PTR_ERR(tty_dev);
  1279. dev_err(&pdev->dev, "Could not allocate tty?\n");
  1280. goto err_free_tty;
  1281. }
  1282. }
  1283. return 0;
  1284. err_free_tty:
  1285. for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
  1286. tty_unregister_device(ntty_driver, i);
  1287. err_free_kfifo:
  1288. for (i = 0; i < MAX_PORT; i++)
  1289. kfifo_free(&dc->port[i].fifo_ul);
  1290. err_free_sbuf:
  1291. kfree(dc->send_buf);
  1292. iounmap(dc->base_addr);
  1293. err_rel_regs:
  1294. pci_release_regions(pdev);
  1295. err_disable_device:
  1296. pci_disable_device(pdev);
  1297. err_free:
  1298. kfree(dc);
  1299. err:
  1300. return ret;
  1301. }
  1302. static void __devexit tty_exit(struct nozomi *dc)
  1303. {
  1304. unsigned int i;
  1305. DBG1(" ");
  1306. flush_scheduled_work();
  1307. for (i = 0; i < MAX_PORT; ++i) {
  1308. struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
  1309. if (tty && list_empty(&tty->hangup_work.entry))
  1310. tty_hangup(tty);
  1311. tty_kref_put(tty);
  1312. }
  1313. /* Racy below - surely should wait for scheduled work to be done or
  1314. complete off a hangup method ? */
  1315. while (dc->open_ttys)
  1316. msleep(1);
  1317. for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
  1318. tty_unregister_device(ntty_driver, i);
  1319. }
  1320. /* Deallocate memory for one device */
  1321. static void __devexit nozomi_card_exit(struct pci_dev *pdev)
  1322. {
  1323. int i;
  1324. struct ctrl_ul ctrl;
  1325. struct nozomi *dc = pci_get_drvdata(pdev);
  1326. /* Disable all interrupts */
  1327. dc->last_ier = 0;
  1328. writew(dc->last_ier, dc->reg_ier);
  1329. tty_exit(dc);
  1330. /* Send 0x0001, command card to resend the reset token. */
  1331. /* This is to get the reset when the module is reloaded. */
  1332. ctrl.port = 0x00;
  1333. ctrl.reserved = 0;
  1334. ctrl.RTS = 0;
  1335. ctrl.DTR = 1;
  1336. DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
  1337. /* Setup dc->reg addresses to we can use defines here */
  1338. write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
  1339. writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
  1340. remove_sysfs_files(dc);
  1341. free_irq(pdev->irq, dc);
  1342. for (i = 0; i < MAX_PORT; i++)
  1343. kfifo_free(&dc->port[i].fifo_ul);
  1344. kfree(dc->send_buf);
  1345. iounmap(dc->base_addr);
  1346. pci_release_regions(pdev);
  1347. pci_disable_device(pdev);
  1348. ndevs[dc->index_start / MAX_PORT] = NULL;
  1349. kfree(dc);
  1350. }
  1351. static void set_rts(const struct tty_struct *tty, int rts)
  1352. {
  1353. struct port *port = get_port_by_tty(tty);
  1354. port->ctrl_ul.RTS = rts;
  1355. port->update_flow_control = 1;
  1356. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1357. }
  1358. static void set_dtr(const struct tty_struct *tty, int dtr)
  1359. {
  1360. struct port *port = get_port_by_tty(tty);
  1361. DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
  1362. port->ctrl_ul.DTR = dtr;
  1363. port->update_flow_control = 1;
  1364. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1365. }
  1366. /*
  1367. * ----------------------------------------------------------------------------
  1368. * TTY code
  1369. * ----------------------------------------------------------------------------
  1370. */
  1371. static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
  1372. {
  1373. struct port *port = get_port_by_tty(tty);
  1374. struct nozomi *dc = get_dc_by_tty(tty);
  1375. int ret;
  1376. if (!port || !dc || dc->state != NOZOMI_STATE_READY)
  1377. return -ENODEV;
  1378. ret = tty_init_termios(tty);
  1379. if (ret == 0) {
  1380. tty_driver_kref_get(driver);
  1381. driver->ttys[tty->index] = tty;
  1382. }
  1383. return ret;
  1384. }
  1385. static void ntty_cleanup(struct tty_struct *tty)
  1386. {
  1387. tty->driver_data = NULL;
  1388. }
  1389. static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
  1390. {
  1391. struct port *port = container_of(tport, struct port, port);
  1392. struct nozomi *dc = port->dc;
  1393. unsigned long flags;
  1394. DBG1("open: %d", port->token_dl);
  1395. spin_lock_irqsave(&dc->spin_mutex, flags);
  1396. dc->last_ier = dc->last_ier | port->token_dl;
  1397. writew(dc->last_ier, dc->reg_ier);
  1398. dc->open_ttys++;
  1399. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1400. printk("noz: activated %d: %p\n", tty->index, tport);
  1401. return 0;
  1402. }
  1403. static int ntty_open(struct tty_struct *tty, struct file *filp)
  1404. {
  1405. struct port *port = get_port_by_tty(tty);
  1406. return tty_port_open(&port->port, tty, filp);
  1407. }
  1408. static void ntty_shutdown(struct tty_port *tport)
  1409. {
  1410. struct port *port = container_of(tport, struct port, port);
  1411. struct nozomi *dc = port->dc;
  1412. unsigned long flags;
  1413. DBG1("close: %d", port->token_dl);
  1414. spin_lock_irqsave(&dc->spin_mutex, flags);
  1415. dc->last_ier &= ~(port->token_dl);
  1416. writew(dc->last_ier, dc->reg_ier);
  1417. dc->open_ttys--;
  1418. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1419. printk("noz: shutdown %p\n", tport);
  1420. }
  1421. static void ntty_close(struct tty_struct *tty, struct file *filp)
  1422. {
  1423. struct port *port = tty->driver_data;
  1424. if (port)
  1425. tty_port_close(&port->port, tty, filp);
  1426. }
  1427. static void ntty_hangup(struct tty_struct *tty)
  1428. {
  1429. struct port *port = tty->driver_data;
  1430. tty_port_hangup(&port->port);
  1431. }
  1432. /*
  1433. * called when the userspace process writes to the tty (/dev/noz*).
  1434. * Data is inserted into a fifo, which is then read and transfered to the modem.
  1435. */
  1436. static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
  1437. int count)
  1438. {
  1439. int rval = -EINVAL;
  1440. struct nozomi *dc = get_dc_by_tty(tty);
  1441. struct port *port = tty->driver_data;
  1442. unsigned long flags;
  1443. /* DBG1( "WRITEx: %d, index = %d", count, index); */
  1444. if (!dc || !port)
  1445. return -ENODEV;
  1446. mutex_lock(&port->tty_sem);
  1447. if (unlikely(!port->port.count)) {
  1448. DBG1(" ");
  1449. goto exit;
  1450. }
  1451. rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
  1452. /* notify card */
  1453. if (unlikely(dc == NULL)) {
  1454. DBG1("No device context?");
  1455. goto exit;
  1456. }
  1457. spin_lock_irqsave(&dc->spin_mutex, flags);
  1458. /* CTS is only valid on the modem channel */
  1459. if (port == &(dc->port[PORT_MDM])) {
  1460. if (port->ctrl_dl.CTS) {
  1461. DBG4("Enable interrupt");
  1462. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1463. } else {
  1464. dev_err(&dc->pdev->dev,
  1465. "CTS not active on modem port?\n");
  1466. }
  1467. } else {
  1468. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1469. }
  1470. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1471. exit:
  1472. mutex_unlock(&port->tty_sem);
  1473. return rval;
  1474. }
  1475. /*
  1476. * Calculate how much is left in device
  1477. * This method is called by the upper tty layer.
  1478. * #according to sources N_TTY.c it expects a value >= 0 and
  1479. * does not check for negative values.
  1480. *
  1481. * If the port is unplugged report lots of room and let the bits
  1482. * dribble away so we don't block anything.
  1483. */
  1484. static int ntty_write_room(struct tty_struct *tty)
  1485. {
  1486. struct port *port = tty->driver_data;
  1487. int room = 4096;
  1488. const struct nozomi *dc = get_dc_by_tty(tty);
  1489. if (dc) {
  1490. mutex_lock(&port->tty_sem);
  1491. if (port->port.count)
  1492. room = port->fifo_ul.size -
  1493. kfifo_len(&port->fifo_ul);
  1494. mutex_unlock(&port->tty_sem);
  1495. }
  1496. return room;
  1497. }
  1498. /* Gets io control parameters */
  1499. static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
  1500. {
  1501. const struct port *port = tty->driver_data;
  1502. const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
  1503. const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
  1504. /* Note: these could change under us but it is not clear this
  1505. matters if so */
  1506. return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
  1507. (ctrl_ul->DTR ? TIOCM_DTR : 0) |
  1508. (ctrl_dl->DCD ? TIOCM_CAR : 0) |
  1509. (ctrl_dl->RI ? TIOCM_RNG : 0) |
  1510. (ctrl_dl->DSR ? TIOCM_DSR : 0) |
  1511. (ctrl_dl->CTS ? TIOCM_CTS : 0);
  1512. }
  1513. /* Sets io controls parameters */
  1514. static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
  1515. unsigned int set, unsigned int clear)
  1516. {
  1517. struct nozomi *dc = get_dc_by_tty(tty);
  1518. unsigned long flags;
  1519. spin_lock_irqsave(&dc->spin_mutex, flags);
  1520. if (set & TIOCM_RTS)
  1521. set_rts(tty, 1);
  1522. else if (clear & TIOCM_RTS)
  1523. set_rts(tty, 0);
  1524. if (set & TIOCM_DTR)
  1525. set_dtr(tty, 1);
  1526. else if (clear & TIOCM_DTR)
  1527. set_dtr(tty, 0);
  1528. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1529. return 0;
  1530. }
  1531. static int ntty_cflags_changed(struct port *port, unsigned long flags,
  1532. struct async_icount *cprev)
  1533. {
  1534. const struct async_icount cnow = port->tty_icount;
  1535. int ret;
  1536. ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
  1537. ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
  1538. ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
  1539. ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
  1540. *cprev = cnow;
  1541. return ret;
  1542. }
  1543. static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
  1544. {
  1545. const struct async_icount cnow = port->tty_icount;
  1546. struct serial_icounter_struct icount;
  1547. icount.cts = cnow.cts;
  1548. icount.dsr = cnow.dsr;
  1549. icount.rng = cnow.rng;
  1550. icount.dcd = cnow.dcd;
  1551. icount.rx = cnow.rx;
  1552. icount.tx = cnow.tx;
  1553. icount.frame = cnow.frame;
  1554. icount.overrun = cnow.overrun;
  1555. icount.parity = cnow.parity;
  1556. icount.brk = cnow.brk;
  1557. icount.buf_overrun = cnow.buf_overrun;
  1558. return copy_to_user(argp, &icount, sizeof(icount)) ? -EFAULT : 0;
  1559. }
  1560. static int ntty_ioctl(struct tty_struct *tty, struct file *file,
  1561. unsigned int cmd, unsigned long arg)
  1562. {
  1563. struct port *port = tty->driver_data;
  1564. void __user *argp = (void __user *)arg;
  1565. int rval = -ENOIOCTLCMD;
  1566. DBG1("******** IOCTL, cmd: %d", cmd);
  1567. switch (cmd) {
  1568. case TIOCMIWAIT: {
  1569. struct async_icount cprev = port->tty_icount;
  1570. rval = wait_event_interruptible(port->tty_wait,
  1571. ntty_cflags_changed(port, arg, &cprev));
  1572. break;
  1573. } case TIOCGICOUNT:
  1574. rval = ntty_ioctl_tiocgicount(port, argp);
  1575. break;
  1576. default:
  1577. DBG1("ERR: 0x%08X, %d", cmd, cmd);
  1578. break;
  1579. };
  1580. return rval;
  1581. }
  1582. /*
  1583. * Called by the upper tty layer when tty buffers are ready
  1584. * to receive data again after a call to throttle.
  1585. */
  1586. static void ntty_unthrottle(struct tty_struct *tty)
  1587. {
  1588. struct nozomi *dc = get_dc_by_tty(tty);
  1589. unsigned long flags;
  1590. DBG1("UNTHROTTLE");
  1591. spin_lock_irqsave(&dc->spin_mutex, flags);
  1592. enable_transmit_dl(tty->index % MAX_PORT, dc);
  1593. set_rts(tty, 1);
  1594. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1595. }
  1596. /*
  1597. * Called by the upper tty layer when the tty buffers are almost full.
  1598. * The driver should stop send more data.
  1599. */
  1600. static void ntty_throttle(struct tty_struct *tty)
  1601. {
  1602. struct nozomi *dc = get_dc_by_tty(tty);
  1603. unsigned long flags;
  1604. DBG1("THROTTLE");
  1605. spin_lock_irqsave(&dc->spin_mutex, flags);
  1606. set_rts(tty, 0);
  1607. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1608. }
  1609. /* Returns number of chars in buffer, called by tty layer */
  1610. static s32 ntty_chars_in_buffer(struct tty_struct *tty)
  1611. {
  1612. struct port *port = tty->driver_data;
  1613. struct nozomi *dc = get_dc_by_tty(tty);
  1614. s32 rval = 0;
  1615. if (unlikely(!dc || !port)) {
  1616. goto exit_in_buffer;
  1617. }
  1618. if (unlikely(!port->port.count)) {
  1619. dev_err(&dc->pdev->dev, "No tty open?\n");
  1620. goto exit_in_buffer;
  1621. }
  1622. rval = kfifo_len(&port->fifo_ul);
  1623. exit_in_buffer:
  1624. return rval;
  1625. }
  1626. static const struct tty_port_operations noz_tty_port_ops = {
  1627. .activate = ntty_activate,
  1628. .shutdown = ntty_shutdown,
  1629. };
  1630. static const struct tty_operations tty_ops = {
  1631. .ioctl = ntty_ioctl,
  1632. .open = ntty_open,
  1633. .close = ntty_close,
  1634. .hangup = ntty_hangup,
  1635. .write = ntty_write,
  1636. .write_room = ntty_write_room,
  1637. .unthrottle = ntty_unthrottle,
  1638. .throttle = ntty_throttle,
  1639. .chars_in_buffer = ntty_chars_in_buffer,
  1640. .tiocmget = ntty_tiocmget,
  1641. .tiocmset = ntty_tiocmset,
  1642. .install = ntty_install,
  1643. .cleanup = ntty_cleanup,
  1644. };
  1645. /* Module initialization */
  1646. static struct pci_driver nozomi_driver = {
  1647. .name = NOZOMI_NAME,
  1648. .id_table = nozomi_pci_tbl,
  1649. .probe = nozomi_card_init,
  1650. .remove = __devexit_p(nozomi_card_exit),
  1651. };
  1652. static __init int nozomi_init(void)
  1653. {
  1654. int ret;
  1655. printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
  1656. ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
  1657. if (!ntty_driver)
  1658. return -ENOMEM;
  1659. ntty_driver->owner = THIS_MODULE;
  1660. ntty_driver->driver_name = NOZOMI_NAME_TTY;
  1661. ntty_driver->name = "noz";
  1662. ntty_driver->major = 0;
  1663. ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1664. ntty_driver->subtype = SERIAL_TYPE_NORMAL;
  1665. ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1666. ntty_driver->init_termios = tty_std_termios;
  1667. ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
  1668. HUPCL | CLOCAL;
  1669. ntty_driver->init_termios.c_ispeed = 115200;
  1670. ntty_driver->init_termios.c_ospeed = 115200;
  1671. tty_set_operations(ntty_driver, &tty_ops);
  1672. ret = tty_register_driver(ntty_driver);
  1673. if (ret) {
  1674. printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
  1675. goto free_tty;
  1676. }
  1677. ret = pci_register_driver(&nozomi_driver);
  1678. if (ret) {
  1679. printk(KERN_ERR "Nozomi: can't register pci driver\n");
  1680. goto unr_tty;
  1681. }
  1682. return 0;
  1683. unr_tty:
  1684. tty_unregister_driver(ntty_driver);
  1685. free_tty:
  1686. put_tty_driver(ntty_driver);
  1687. return ret;
  1688. }
  1689. static __exit void nozomi_exit(void)
  1690. {
  1691. printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
  1692. pci_unregister_driver(&nozomi_driver);
  1693. tty_unregister_driver(ntty_driver);
  1694. put_tty_driver(ntty_driver);
  1695. }
  1696. module_init(nozomi_init);
  1697. module_exit(nozomi_exit);
  1698. module_param(debug, int, S_IRUGO | S_IWUSR);
  1699. MODULE_LICENSE("Dual BSD/GPL");
  1700. MODULE_DESCRIPTION(DRIVER_DESC);