nozomi.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  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 <linux/slab.h>
  57. #include <asm/byteorder.h>
  58. #include <linux/delay.h>
  59. #define VERSION_STRING DRIVER_DESC " 2.1d"
  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. wait_queue_head_t tty_wait;
  319. struct async_icount tty_icount;
  320. struct nozomi *dc;
  321. };
  322. /* Private data one for each card in the system */
  323. struct nozomi {
  324. void __iomem *base_addr;
  325. unsigned long flip;
  326. /* Pointers to registers */
  327. void __iomem *reg_iir;
  328. void __iomem *reg_fcr;
  329. void __iomem *reg_ier;
  330. u16 last_ier;
  331. enum card_type card_type;
  332. struct config_table config_table; /* Configuration table */
  333. struct pci_dev *pdev;
  334. struct port port[NOZOMI_MAX_PORTS];
  335. u8 *send_buf;
  336. spinlock_t spin_mutex; /* secures access to registers and tty */
  337. unsigned int index_start;
  338. enum card_state state;
  339. u32 open_ttys;
  340. };
  341. /* This is a data packet that is read or written to/from card */
  342. struct buffer {
  343. u32 size; /* size is the length of the data buffer */
  344. u8 *data;
  345. } __attribute__ ((packed));
  346. /* Global variables */
  347. static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
  348. {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
  349. {},
  350. };
  351. MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
  352. static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
  353. static struct tty_driver *ntty_driver;
  354. static const struct tty_port_operations noz_tty_port_ops;
  355. /*
  356. * find card by tty_index
  357. */
  358. static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
  359. {
  360. return tty ? ndevs[tty->index / MAX_PORT] : NULL;
  361. }
  362. static inline struct port *get_port_by_tty(const struct tty_struct *tty)
  363. {
  364. struct nozomi *ndev = get_dc_by_tty(tty);
  365. return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
  366. }
  367. /*
  368. * TODO:
  369. * -Optimize
  370. * -Rewrite cleaner
  371. */
  372. static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
  373. u32 size_bytes)
  374. {
  375. u32 i = 0;
  376. const u32 __iomem *ptr = mem_addr_start;
  377. u16 *buf16;
  378. if (unlikely(!ptr || !buf))
  379. goto out;
  380. /* shortcut for extremely often used cases */
  381. switch (size_bytes) {
  382. case 2: /* 2 bytes */
  383. buf16 = (u16 *) buf;
  384. *buf16 = __le16_to_cpu(readw(ptr));
  385. goto out;
  386. break;
  387. case 4: /* 4 bytes */
  388. *(buf) = __le32_to_cpu(readl(ptr));
  389. goto out;
  390. break;
  391. }
  392. while (i < size_bytes) {
  393. if (size_bytes - i == 2) {
  394. /* Handle 2 bytes in the end */
  395. buf16 = (u16 *) buf;
  396. *(buf16) = __le16_to_cpu(readw(ptr));
  397. i += 2;
  398. } else {
  399. /* Read 4 bytes */
  400. *(buf) = __le32_to_cpu(readl(ptr));
  401. i += 4;
  402. }
  403. buf++;
  404. ptr++;
  405. }
  406. out:
  407. return;
  408. }
  409. /*
  410. * TODO:
  411. * -Optimize
  412. * -Rewrite cleaner
  413. */
  414. static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
  415. u32 size_bytes)
  416. {
  417. u32 i = 0;
  418. u32 __iomem *ptr = mem_addr_start;
  419. const u16 *buf16;
  420. if (unlikely(!ptr || !buf))
  421. return 0;
  422. /* shortcut for extremely often used cases */
  423. switch (size_bytes) {
  424. case 2: /* 2 bytes */
  425. buf16 = (const u16 *)buf;
  426. writew(__cpu_to_le16(*buf16), ptr);
  427. return 2;
  428. break;
  429. case 1: /*
  430. * also needs to write 4 bytes in this case
  431. * so falling through..
  432. */
  433. case 4: /* 4 bytes */
  434. writel(__cpu_to_le32(*buf), ptr);
  435. return 4;
  436. break;
  437. }
  438. while (i < size_bytes) {
  439. if (size_bytes - i == 2) {
  440. /* 2 bytes */
  441. buf16 = (const u16 *)buf;
  442. writew(__cpu_to_le16(*buf16), ptr);
  443. i += 2;
  444. } else {
  445. /* 4 bytes */
  446. writel(__cpu_to_le32(*buf), ptr);
  447. i += 4;
  448. }
  449. buf++;
  450. ptr++;
  451. }
  452. return i;
  453. }
  454. /* Setup pointers to different channels and also setup buffer sizes. */
  455. static void setup_memory(struct nozomi *dc)
  456. {
  457. void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
  458. /* The length reported is including the length field of 4 bytes,
  459. * hence subtract with 4.
  460. */
  461. const u16 buff_offset = 4;
  462. /* Modem port dl configuration */
  463. dc->port[PORT_MDM].dl_addr[CH_A] = offset;
  464. dc->port[PORT_MDM].dl_addr[CH_B] =
  465. (offset += dc->config_table.dl_mdm_len1);
  466. dc->port[PORT_MDM].dl_size[CH_A] =
  467. dc->config_table.dl_mdm_len1 - buff_offset;
  468. dc->port[PORT_MDM].dl_size[CH_B] =
  469. dc->config_table.dl_mdm_len2 - buff_offset;
  470. /* Diag port dl configuration */
  471. dc->port[PORT_DIAG].dl_addr[CH_A] =
  472. (offset += dc->config_table.dl_mdm_len2);
  473. dc->port[PORT_DIAG].dl_size[CH_A] =
  474. dc->config_table.dl_diag_len1 - buff_offset;
  475. dc->port[PORT_DIAG].dl_addr[CH_B] =
  476. (offset += dc->config_table.dl_diag_len1);
  477. dc->port[PORT_DIAG].dl_size[CH_B] =
  478. dc->config_table.dl_diag_len2 - buff_offset;
  479. /* App1 port dl configuration */
  480. dc->port[PORT_APP1].dl_addr[CH_A] =
  481. (offset += dc->config_table.dl_diag_len2);
  482. dc->port[PORT_APP1].dl_size[CH_A] =
  483. dc->config_table.dl_app1_len - buff_offset;
  484. /* App2 port dl configuration */
  485. dc->port[PORT_APP2].dl_addr[CH_A] =
  486. (offset += dc->config_table.dl_app1_len);
  487. dc->port[PORT_APP2].dl_size[CH_A] =
  488. dc->config_table.dl_app2_len - buff_offset;
  489. /* Ctrl dl configuration */
  490. dc->port[PORT_CTRL].dl_addr[CH_A] =
  491. (offset += dc->config_table.dl_app2_len);
  492. dc->port[PORT_CTRL].dl_size[CH_A] =
  493. dc->config_table.dl_ctrl_len - buff_offset;
  494. offset = dc->base_addr + dc->config_table.ul_start;
  495. /* Modem Port ul configuration */
  496. dc->port[PORT_MDM].ul_addr[CH_A] = offset;
  497. dc->port[PORT_MDM].ul_size[CH_A] =
  498. dc->config_table.ul_mdm_len1 - buff_offset;
  499. dc->port[PORT_MDM].ul_addr[CH_B] =
  500. (offset += dc->config_table.ul_mdm_len1);
  501. dc->port[PORT_MDM].ul_size[CH_B] =
  502. dc->config_table.ul_mdm_len2 - buff_offset;
  503. /* Diag port ul configuration */
  504. dc->port[PORT_DIAG].ul_addr[CH_A] =
  505. (offset += dc->config_table.ul_mdm_len2);
  506. dc->port[PORT_DIAG].ul_size[CH_A] =
  507. dc->config_table.ul_diag_len - buff_offset;
  508. /* App1 port ul configuration */
  509. dc->port[PORT_APP1].ul_addr[CH_A] =
  510. (offset += dc->config_table.ul_diag_len);
  511. dc->port[PORT_APP1].ul_size[CH_A] =
  512. dc->config_table.ul_app1_len - buff_offset;
  513. /* App2 port ul configuration */
  514. dc->port[PORT_APP2].ul_addr[CH_A] =
  515. (offset += dc->config_table.ul_app1_len);
  516. dc->port[PORT_APP2].ul_size[CH_A] =
  517. dc->config_table.ul_app2_len - buff_offset;
  518. /* Ctrl ul configuration */
  519. dc->port[PORT_CTRL].ul_addr[CH_A] =
  520. (offset += dc->config_table.ul_app2_len);
  521. dc->port[PORT_CTRL].ul_size[CH_A] =
  522. dc->config_table.ul_ctrl_len - buff_offset;
  523. }
  524. /* Dump config table under initalization phase */
  525. #ifdef DEBUG
  526. static void dump_table(const struct nozomi *dc)
  527. {
  528. DBG3("signature: 0x%08X", dc->config_table.signature);
  529. DBG3("version: 0x%04X", dc->config_table.version);
  530. DBG3("product_information: 0x%04X", \
  531. dc->config_table.product_information);
  532. DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
  533. DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
  534. DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
  535. DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
  536. DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
  537. DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
  538. dc->config_table.dl_mdm_len1);
  539. DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
  540. dc->config_table.dl_mdm_len2);
  541. DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
  542. dc->config_table.dl_diag_len1);
  543. DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
  544. dc->config_table.dl_diag_len2);
  545. DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
  546. dc->config_table.dl_app1_len);
  547. DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
  548. dc->config_table.dl_app2_len);
  549. DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
  550. dc->config_table.dl_ctrl_len);
  551. DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
  552. dc->config_table.ul_start);
  553. DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
  554. dc->config_table.ul_mdm_len1);
  555. DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
  556. dc->config_table.ul_mdm_len2);
  557. DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
  558. dc->config_table.ul_diag_len);
  559. DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
  560. dc->config_table.ul_app1_len);
  561. DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
  562. dc->config_table.ul_app2_len);
  563. DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
  564. dc->config_table.ul_ctrl_len);
  565. }
  566. #else
  567. static inline void dump_table(const struct nozomi *dc) { }
  568. #endif
  569. /*
  570. * Read configuration table from card under intalization phase
  571. * Returns 1 if ok, else 0
  572. */
  573. static int nozomi_read_config_table(struct nozomi *dc)
  574. {
  575. read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
  576. sizeof(struct config_table));
  577. if (dc->config_table.signature != CONFIG_MAGIC) {
  578. dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
  579. dc->config_table.signature, CONFIG_MAGIC);
  580. return 0;
  581. }
  582. if ((dc->config_table.version == 0)
  583. || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
  584. int i;
  585. DBG1("Second phase, configuring card");
  586. setup_memory(dc);
  587. dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
  588. dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
  589. dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
  590. DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
  591. dc->port[PORT_MDM].toggle_ul,
  592. dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
  593. dump_table(dc);
  594. for (i = PORT_MDM; i < MAX_PORT; i++) {
  595. memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
  596. memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
  597. }
  598. /* Enable control channel */
  599. dc->last_ier = dc->last_ier | CTRL_DL;
  600. writew(dc->last_ier, dc->reg_ier);
  601. dc->state = NOZOMI_STATE_ALLOCATED;
  602. dev_info(&dc->pdev->dev, "Initialization OK!\n");
  603. return 1;
  604. }
  605. if ((dc->config_table.version > 0)
  606. && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
  607. u32 offset = 0;
  608. DBG1("First phase: pushing upload buffers, clearing download");
  609. dev_info(&dc->pdev->dev, "Version of card: %d\n",
  610. dc->config_table.version);
  611. /* Here we should disable all I/O over F32. */
  612. setup_memory(dc);
  613. /*
  614. * We should send ALL channel pair tokens back along
  615. * with reset token
  616. */
  617. /* push upload modem buffers */
  618. write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
  619. (u32 *) &offset, 4);
  620. write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
  621. (u32 *) &offset, 4);
  622. writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
  623. DBG1("First phase done");
  624. }
  625. return 1;
  626. }
  627. /* Enable uplink interrupts */
  628. static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
  629. {
  630. static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
  631. if (port < NOZOMI_MAX_PORTS) {
  632. dc->last_ier |= mask[port];
  633. writew(dc->last_ier, dc->reg_ier);
  634. } else {
  635. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  636. }
  637. }
  638. /* Disable uplink interrupts */
  639. static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
  640. {
  641. static const u16 mask[] =
  642. {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
  643. if (port < NOZOMI_MAX_PORTS) {
  644. dc->last_ier &= mask[port];
  645. writew(dc->last_ier, dc->reg_ier);
  646. } else {
  647. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  648. }
  649. }
  650. /* Enable downlink interrupts */
  651. static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
  652. {
  653. static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
  654. if (port < NOZOMI_MAX_PORTS) {
  655. dc->last_ier |= mask[port];
  656. writew(dc->last_ier, dc->reg_ier);
  657. } else {
  658. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  659. }
  660. }
  661. /* Disable downlink interrupts */
  662. static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
  663. {
  664. static const u16 mask[] =
  665. {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
  666. if (port < NOZOMI_MAX_PORTS) {
  667. dc->last_ier &= mask[port];
  668. writew(dc->last_ier, dc->reg_ier);
  669. } else {
  670. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  671. }
  672. }
  673. /*
  674. * Return 1 - send buffer to card and ack.
  675. * Return 0 - don't ack, don't send buffer to card.
  676. */
  677. static int send_data(enum port_type index, struct nozomi *dc)
  678. {
  679. u32 size = 0;
  680. struct port *port = &dc->port[index];
  681. const u8 toggle = port->toggle_ul;
  682. void __iomem *addr = port->ul_addr[toggle];
  683. const u32 ul_size = port->ul_size[toggle];
  684. struct tty_struct *tty = tty_port_tty_get(&port->port);
  685. /* Get data from tty and place in buf for now */
  686. size = kfifo_out(&port->fifo_ul, dc->send_buf,
  687. ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
  688. if (size == 0) {
  689. DBG4("No more data to send, disable link:");
  690. tty_kref_put(tty);
  691. return 0;
  692. }
  693. /* DUMP(buf, size); */
  694. /* Write length + data */
  695. write_mem32(addr, (u32 *) &size, 4);
  696. write_mem32(addr + 4, (u32 *) dc->send_buf, size);
  697. if (tty)
  698. tty_wakeup(tty);
  699. tty_kref_put(tty);
  700. return 1;
  701. }
  702. /* If all data has been read, return 1, else 0 */
  703. static int receive_data(enum port_type index, struct nozomi *dc)
  704. {
  705. u8 buf[RECEIVE_BUF_MAX] = { 0 };
  706. int size;
  707. u32 offset = 4;
  708. struct port *port = &dc->port[index];
  709. void __iomem *addr = port->dl_addr[port->toggle_dl];
  710. struct tty_struct *tty = tty_port_tty_get(&port->port);
  711. int i, ret;
  712. if (unlikely(!tty)) {
  713. DBG1("tty not open for port: %d?", index);
  714. return 1;
  715. }
  716. read_mem32((u32 *) &size, addr, 4);
  717. /* DBG1( "%d bytes port: %d", size, index); */
  718. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  719. DBG1("No room in tty, don't read data, don't ack interrupt, "
  720. "disable interrupt");
  721. /* disable interrupt in downlink... */
  722. disable_transmit_dl(index, dc);
  723. ret = 0;
  724. goto put;
  725. }
  726. if (unlikely(size == 0)) {
  727. dev_err(&dc->pdev->dev, "size == 0?\n");
  728. ret = 1;
  729. goto put;
  730. }
  731. while (size > 0) {
  732. read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
  733. if (size == 1) {
  734. tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
  735. size = 0;
  736. } else if (size < RECEIVE_BUF_MAX) {
  737. size -= tty_insert_flip_string(tty, (char *) buf, size);
  738. } else {
  739. i = tty_insert_flip_string(tty, \
  740. (char *) buf, RECEIVE_BUF_MAX);
  741. size -= i;
  742. offset += i;
  743. }
  744. }
  745. set_bit(index, &dc->flip);
  746. ret = 1;
  747. put:
  748. tty_kref_put(tty);
  749. return ret;
  750. }
  751. /* Debug for interrupts */
  752. #ifdef DEBUG
  753. static char *interrupt2str(u16 interrupt)
  754. {
  755. static char buf[TMP_BUF_MAX];
  756. char *p = buf;
  757. interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
  758. interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  759. "MDM_DL2 ") : NULL;
  760. interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  761. "MDM_UL1 ") : NULL;
  762. interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  763. "MDM_UL2 ") : NULL;
  764. interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  765. "DIAG_DL1 ") : NULL;
  766. interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  767. "DIAG_DL2 ") : NULL;
  768. interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  769. "DIAG_UL ") : NULL;
  770. interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  771. "APP1_DL ") : NULL;
  772. interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  773. "APP2_DL ") : NULL;
  774. interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  775. "APP1_UL ") : NULL;
  776. interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  777. "APP2_UL ") : NULL;
  778. interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  779. "CTRL_DL ") : NULL;
  780. interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  781. "CTRL_UL ") : NULL;
  782. interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  783. "RESET ") : NULL;
  784. return buf;
  785. }
  786. #endif
  787. /*
  788. * Receive flow control
  789. * Return 1 - If ok, else 0
  790. */
  791. static int receive_flow_control(struct nozomi *dc)
  792. {
  793. enum port_type port = PORT_MDM;
  794. struct ctrl_dl ctrl_dl;
  795. struct ctrl_dl old_ctrl;
  796. u16 enable_ier = 0;
  797. read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
  798. switch (ctrl_dl.port) {
  799. case CTRL_CMD:
  800. DBG1("The Base Band sends this value as a response to a "
  801. "request for IMSI detach sent over the control "
  802. "channel uplink (see section 7.6.1).");
  803. break;
  804. case CTRL_MDM:
  805. port = PORT_MDM;
  806. enable_ier = MDM_DL;
  807. break;
  808. case CTRL_DIAG:
  809. port = PORT_DIAG;
  810. enable_ier = DIAG_DL;
  811. break;
  812. case CTRL_APP1:
  813. port = PORT_APP1;
  814. enable_ier = APP1_DL;
  815. break;
  816. case CTRL_APP2:
  817. port = PORT_APP2;
  818. enable_ier = APP2_DL;
  819. if (dc->state == NOZOMI_STATE_ALLOCATED) {
  820. /*
  821. * After card initialization the flow control
  822. * received for APP2 is always the last
  823. */
  824. dc->state = NOZOMI_STATE_READY;
  825. dev_info(&dc->pdev->dev, "Device READY!\n");
  826. }
  827. break;
  828. default:
  829. dev_err(&dc->pdev->dev,
  830. "ERROR: flow control received for non-existing port\n");
  831. return 0;
  832. };
  833. DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
  834. *((u16 *)&ctrl_dl));
  835. old_ctrl = dc->port[port].ctrl_dl;
  836. dc->port[port].ctrl_dl = ctrl_dl;
  837. if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
  838. DBG1("Disable interrupt (0x%04X) on port: %d",
  839. enable_ier, port);
  840. disable_transmit_ul(port, dc);
  841. } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
  842. if (kfifo_len(&dc->port[port].fifo_ul)) {
  843. DBG1("Enable interrupt (0x%04X) on port: %d",
  844. enable_ier, port);
  845. DBG1("Data in buffer [%d], enable transmit! ",
  846. kfifo_len(&dc->port[port].fifo_ul));
  847. enable_transmit_ul(port, dc);
  848. } else {
  849. DBG1("No data in buffer...");
  850. }
  851. }
  852. if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
  853. DBG1(" No change in mctrl");
  854. return 1;
  855. }
  856. /* Update statistics */
  857. if (old_ctrl.CTS != ctrl_dl.CTS)
  858. dc->port[port].tty_icount.cts++;
  859. if (old_ctrl.DSR != ctrl_dl.DSR)
  860. dc->port[port].tty_icount.dsr++;
  861. if (old_ctrl.RI != ctrl_dl.RI)
  862. dc->port[port].tty_icount.rng++;
  863. if (old_ctrl.DCD != ctrl_dl.DCD)
  864. dc->port[port].tty_icount.dcd++;
  865. wake_up_interruptible(&dc->port[port].tty_wait);
  866. DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
  867. port,
  868. dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
  869. dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
  870. return 1;
  871. }
  872. static enum ctrl_port_type port2ctrl(enum port_type port,
  873. const struct nozomi *dc)
  874. {
  875. switch (port) {
  876. case PORT_MDM:
  877. return CTRL_MDM;
  878. case PORT_DIAG:
  879. return CTRL_DIAG;
  880. case PORT_APP1:
  881. return CTRL_APP1;
  882. case PORT_APP2:
  883. return CTRL_APP2;
  884. default:
  885. dev_err(&dc->pdev->dev,
  886. "ERROR: send flow control " \
  887. "received for non-existing port\n");
  888. };
  889. return CTRL_ERROR;
  890. }
  891. /*
  892. * Send flow control, can only update one channel at a time
  893. * Return 0 - If we have updated all flow control
  894. * Return 1 - If we need to update more flow control, ack current enable more
  895. */
  896. static int send_flow_control(struct nozomi *dc)
  897. {
  898. u32 i, more_flow_control_to_be_updated = 0;
  899. u16 *ctrl;
  900. for (i = PORT_MDM; i < MAX_PORT; i++) {
  901. if (dc->port[i].update_flow_control) {
  902. if (more_flow_control_to_be_updated) {
  903. /* We have more flow control to be updated */
  904. return 1;
  905. }
  906. dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
  907. ctrl = (u16 *)&dc->port[i].ctrl_ul;
  908. write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
  909. (u32 *) ctrl, 2);
  910. dc->port[i].update_flow_control = 0;
  911. more_flow_control_to_be_updated = 1;
  912. }
  913. }
  914. return 0;
  915. }
  916. /*
  917. * Handle downlink data, ports that are handled are modem and diagnostics
  918. * Return 1 - ok
  919. * Return 0 - toggle fields are out of sync
  920. */
  921. static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
  922. u16 read_iir, u16 mask1, u16 mask2)
  923. {
  924. if (*toggle == 0 && read_iir & mask1) {
  925. if (receive_data(port, dc)) {
  926. writew(mask1, dc->reg_fcr);
  927. *toggle = !(*toggle);
  928. }
  929. if (read_iir & mask2) {
  930. if (receive_data(port, dc)) {
  931. writew(mask2, dc->reg_fcr);
  932. *toggle = !(*toggle);
  933. }
  934. }
  935. } else if (*toggle == 1 && read_iir & mask2) {
  936. if (receive_data(port, dc)) {
  937. writew(mask2, dc->reg_fcr);
  938. *toggle = !(*toggle);
  939. }
  940. if (read_iir & mask1) {
  941. if (receive_data(port, dc)) {
  942. writew(mask1, dc->reg_fcr);
  943. *toggle = !(*toggle);
  944. }
  945. }
  946. } else {
  947. dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
  948. *toggle);
  949. return 0;
  950. }
  951. return 1;
  952. }
  953. /*
  954. * Handle uplink data, this is currently for the modem port
  955. * Return 1 - ok
  956. * Return 0 - toggle field are out of sync
  957. */
  958. static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
  959. {
  960. u8 *toggle = &(dc->port[port].toggle_ul);
  961. if (*toggle == 0 && read_iir & MDM_UL1) {
  962. dc->last_ier &= ~MDM_UL;
  963. writew(dc->last_ier, dc->reg_ier);
  964. if (send_data(port, dc)) {
  965. writew(MDM_UL1, dc->reg_fcr);
  966. dc->last_ier = dc->last_ier | MDM_UL;
  967. writew(dc->last_ier, dc->reg_ier);
  968. *toggle = !*toggle;
  969. }
  970. if (read_iir & MDM_UL2) {
  971. dc->last_ier &= ~MDM_UL;
  972. writew(dc->last_ier, dc->reg_ier);
  973. if (send_data(port, dc)) {
  974. writew(MDM_UL2, dc->reg_fcr);
  975. dc->last_ier = dc->last_ier | MDM_UL;
  976. writew(dc->last_ier, dc->reg_ier);
  977. *toggle = !*toggle;
  978. }
  979. }
  980. } else if (*toggle == 1 && read_iir & MDM_UL2) {
  981. dc->last_ier &= ~MDM_UL;
  982. writew(dc->last_ier, dc->reg_ier);
  983. if (send_data(port, dc)) {
  984. writew(MDM_UL2, dc->reg_fcr);
  985. dc->last_ier = dc->last_ier | MDM_UL;
  986. writew(dc->last_ier, dc->reg_ier);
  987. *toggle = !*toggle;
  988. }
  989. if (read_iir & MDM_UL1) {
  990. dc->last_ier &= ~MDM_UL;
  991. writew(dc->last_ier, dc->reg_ier);
  992. if (send_data(port, dc)) {
  993. writew(MDM_UL1, dc->reg_fcr);
  994. dc->last_ier = dc->last_ier | MDM_UL;
  995. writew(dc->last_ier, dc->reg_ier);
  996. *toggle = !*toggle;
  997. }
  998. }
  999. } else {
  1000. writew(read_iir & MDM_UL, dc->reg_fcr);
  1001. dev_err(&dc->pdev->dev, "port out of sync!\n");
  1002. return 0;
  1003. }
  1004. return 1;
  1005. }
  1006. static irqreturn_t interrupt_handler(int irq, void *dev_id)
  1007. {
  1008. struct nozomi *dc = dev_id;
  1009. unsigned int a;
  1010. u16 read_iir;
  1011. if (!dc)
  1012. return IRQ_NONE;
  1013. spin_lock(&dc->spin_mutex);
  1014. read_iir = readw(dc->reg_iir);
  1015. /* Card removed */
  1016. if (read_iir == (u16)-1)
  1017. goto none;
  1018. /*
  1019. * Just handle interrupt enabled in IER
  1020. * (by masking with dc->last_ier)
  1021. */
  1022. read_iir &= dc->last_ier;
  1023. if (read_iir == 0)
  1024. goto none;
  1025. DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
  1026. dc->last_ier);
  1027. if (read_iir & RESET) {
  1028. if (unlikely(!nozomi_read_config_table(dc))) {
  1029. dc->last_ier = 0x0;
  1030. writew(dc->last_ier, dc->reg_ier);
  1031. dev_err(&dc->pdev->dev, "Could not read status from "
  1032. "card, we should disable interface\n");
  1033. } else {
  1034. writew(RESET, dc->reg_fcr);
  1035. }
  1036. /* No more useful info if this was the reset interrupt. */
  1037. goto exit_handler;
  1038. }
  1039. if (read_iir & CTRL_UL) {
  1040. DBG1("CTRL_UL");
  1041. dc->last_ier &= ~CTRL_UL;
  1042. writew(dc->last_ier, dc->reg_ier);
  1043. if (send_flow_control(dc)) {
  1044. writew(CTRL_UL, dc->reg_fcr);
  1045. dc->last_ier = dc->last_ier | CTRL_UL;
  1046. writew(dc->last_ier, dc->reg_ier);
  1047. }
  1048. }
  1049. if (read_iir & CTRL_DL) {
  1050. receive_flow_control(dc);
  1051. writew(CTRL_DL, dc->reg_fcr);
  1052. }
  1053. if (read_iir & MDM_DL) {
  1054. if (!handle_data_dl(dc, PORT_MDM,
  1055. &(dc->port[PORT_MDM].toggle_dl), read_iir,
  1056. MDM_DL1, MDM_DL2)) {
  1057. dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
  1058. goto exit_handler;
  1059. }
  1060. }
  1061. if (read_iir & MDM_UL) {
  1062. if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
  1063. dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
  1064. goto exit_handler;
  1065. }
  1066. }
  1067. if (read_iir & DIAG_DL) {
  1068. if (!handle_data_dl(dc, PORT_DIAG,
  1069. &(dc->port[PORT_DIAG].toggle_dl), read_iir,
  1070. DIAG_DL1, DIAG_DL2)) {
  1071. dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
  1072. goto exit_handler;
  1073. }
  1074. }
  1075. if (read_iir & DIAG_UL) {
  1076. dc->last_ier &= ~DIAG_UL;
  1077. writew(dc->last_ier, dc->reg_ier);
  1078. if (send_data(PORT_DIAG, dc)) {
  1079. writew(DIAG_UL, dc->reg_fcr);
  1080. dc->last_ier = dc->last_ier | DIAG_UL;
  1081. writew(dc->last_ier, dc->reg_ier);
  1082. }
  1083. }
  1084. if (read_iir & APP1_DL) {
  1085. if (receive_data(PORT_APP1, dc))
  1086. writew(APP1_DL, dc->reg_fcr);
  1087. }
  1088. if (read_iir & APP1_UL) {
  1089. dc->last_ier &= ~APP1_UL;
  1090. writew(dc->last_ier, dc->reg_ier);
  1091. if (send_data(PORT_APP1, dc)) {
  1092. writew(APP1_UL, dc->reg_fcr);
  1093. dc->last_ier = dc->last_ier | APP1_UL;
  1094. writew(dc->last_ier, dc->reg_ier);
  1095. }
  1096. }
  1097. if (read_iir & APP2_DL) {
  1098. if (receive_data(PORT_APP2, dc))
  1099. writew(APP2_DL, dc->reg_fcr);
  1100. }
  1101. if (read_iir & APP2_UL) {
  1102. dc->last_ier &= ~APP2_UL;
  1103. writew(dc->last_ier, dc->reg_ier);
  1104. if (send_data(PORT_APP2, dc)) {
  1105. writew(APP2_UL, dc->reg_fcr);
  1106. dc->last_ier = dc->last_ier | APP2_UL;
  1107. writew(dc->last_ier, dc->reg_ier);
  1108. }
  1109. }
  1110. exit_handler:
  1111. spin_unlock(&dc->spin_mutex);
  1112. for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
  1113. struct tty_struct *tty;
  1114. if (test_and_clear_bit(a, &dc->flip)) {
  1115. tty = tty_port_tty_get(&dc->port[a].port);
  1116. if (tty)
  1117. tty_flip_buffer_push(tty);
  1118. tty_kref_put(tty);
  1119. }
  1120. }
  1121. return IRQ_HANDLED;
  1122. none:
  1123. spin_unlock(&dc->spin_mutex);
  1124. return IRQ_NONE;
  1125. }
  1126. static void nozomi_get_card_type(struct nozomi *dc)
  1127. {
  1128. int i;
  1129. u32 size = 0;
  1130. for (i = 0; i < 6; i++)
  1131. size += pci_resource_len(dc->pdev, i);
  1132. /* Assume card type F32_8 if no match */
  1133. dc->card_type = size == 2048 ? F32_2 : F32_8;
  1134. dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
  1135. }
  1136. static void nozomi_setup_private_data(struct nozomi *dc)
  1137. {
  1138. void __iomem *offset = dc->base_addr + dc->card_type / 2;
  1139. unsigned int i;
  1140. dc->reg_fcr = (void __iomem *)(offset + R_FCR);
  1141. dc->reg_iir = (void __iomem *)(offset + R_IIR);
  1142. dc->reg_ier = (void __iomem *)(offset + R_IER);
  1143. dc->last_ier = 0;
  1144. dc->flip = 0;
  1145. dc->port[PORT_MDM].token_dl = MDM_DL;
  1146. dc->port[PORT_DIAG].token_dl = DIAG_DL;
  1147. dc->port[PORT_APP1].token_dl = APP1_DL;
  1148. dc->port[PORT_APP2].token_dl = APP2_DL;
  1149. for (i = 0; i < MAX_PORT; i++)
  1150. init_waitqueue_head(&dc->port[i].tty_wait);
  1151. }
  1152. static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
  1153. char *buf)
  1154. {
  1155. const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
  1156. return sprintf(buf, "%d\n", dc->card_type);
  1157. }
  1158. static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
  1159. static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
  1160. char *buf)
  1161. {
  1162. const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
  1163. return sprintf(buf, "%u\n", dc->open_ttys);
  1164. }
  1165. static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
  1166. static void make_sysfs_files(struct nozomi *dc)
  1167. {
  1168. if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
  1169. dev_err(&dc->pdev->dev,
  1170. "Could not create sysfs file for card_type\n");
  1171. if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
  1172. dev_err(&dc->pdev->dev,
  1173. "Could not create sysfs file for open_ttys\n");
  1174. }
  1175. static void remove_sysfs_files(struct nozomi *dc)
  1176. {
  1177. device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
  1178. device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
  1179. }
  1180. /* Allocate memory for one device */
  1181. static int __devinit nozomi_card_init(struct pci_dev *pdev,
  1182. const struct pci_device_id *ent)
  1183. {
  1184. resource_size_t start;
  1185. int ret;
  1186. struct nozomi *dc = NULL;
  1187. int ndev_idx;
  1188. int i;
  1189. dev_dbg(&pdev->dev, "Init, new card found\n");
  1190. for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
  1191. if (!ndevs[ndev_idx])
  1192. break;
  1193. if (ndev_idx >= ARRAY_SIZE(ndevs)) {
  1194. dev_err(&pdev->dev, "no free tty range for this card left\n");
  1195. ret = -EIO;
  1196. goto err;
  1197. }
  1198. dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
  1199. if (unlikely(!dc)) {
  1200. dev_err(&pdev->dev, "Could not allocate memory\n");
  1201. ret = -ENOMEM;
  1202. goto err_free;
  1203. }
  1204. dc->pdev = pdev;
  1205. ret = pci_enable_device(dc->pdev);
  1206. if (ret) {
  1207. dev_err(&pdev->dev, "Failed to enable PCI Device\n");
  1208. goto err_free;
  1209. }
  1210. ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
  1211. if (ret) {
  1212. dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
  1213. (int) /* nozomi_private.io_addr */ 0);
  1214. goto err_disable_device;
  1215. }
  1216. start = pci_resource_start(dc->pdev, 0);
  1217. if (start == 0) {
  1218. dev_err(&pdev->dev, "No I/O address for card detected\n");
  1219. ret = -ENODEV;
  1220. goto err_rel_regs;
  1221. }
  1222. /* Find out what card type it is */
  1223. nozomi_get_card_type(dc);
  1224. dc->base_addr = ioremap_nocache(start, dc->card_type);
  1225. if (!dc->base_addr) {
  1226. dev_err(&pdev->dev, "Unable to map card MMIO\n");
  1227. ret = -ENODEV;
  1228. goto err_rel_regs;
  1229. }
  1230. dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
  1231. if (!dc->send_buf) {
  1232. dev_err(&pdev->dev, "Could not allocate send buffer?\n");
  1233. ret = -ENOMEM;
  1234. goto err_free_sbuf;
  1235. }
  1236. for (i = PORT_MDM; i < MAX_PORT; i++) {
  1237. if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
  1238. GFP_KERNEL)) {
  1239. dev_err(&pdev->dev,
  1240. "Could not allocate kfifo buffer\n");
  1241. ret = -ENOMEM;
  1242. goto err_free_kfifo;
  1243. }
  1244. }
  1245. spin_lock_init(&dc->spin_mutex);
  1246. nozomi_setup_private_data(dc);
  1247. /* Disable all interrupts */
  1248. dc->last_ier = 0;
  1249. writew(dc->last_ier, dc->reg_ier);
  1250. ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
  1251. NOZOMI_NAME, dc);
  1252. if (unlikely(ret)) {
  1253. dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
  1254. goto err_free_kfifo;
  1255. }
  1256. DBG1("base_addr: %p", dc->base_addr);
  1257. make_sysfs_files(dc);
  1258. dc->index_start = ndev_idx * MAX_PORT;
  1259. ndevs[ndev_idx] = dc;
  1260. pci_set_drvdata(pdev, dc);
  1261. /* Enable RESET interrupt */
  1262. dc->last_ier = RESET;
  1263. iowrite16(dc->last_ier, dc->reg_ier);
  1264. dc->state = NOZOMI_STATE_ENABLED;
  1265. for (i = 0; i < MAX_PORT; i++) {
  1266. struct device *tty_dev;
  1267. struct port *port = &dc->port[i];
  1268. port->dc = dc;
  1269. tty_port_init(&port->port);
  1270. port->port.ops = &noz_tty_port_ops;
  1271. tty_dev = tty_register_device(ntty_driver, dc->index_start + i,
  1272. &pdev->dev);
  1273. if (IS_ERR(tty_dev)) {
  1274. ret = PTR_ERR(tty_dev);
  1275. dev_err(&pdev->dev, "Could not allocate tty?\n");
  1276. goto err_free_tty;
  1277. }
  1278. }
  1279. return 0;
  1280. err_free_tty:
  1281. for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
  1282. tty_unregister_device(ntty_driver, i);
  1283. err_free_kfifo:
  1284. for (i = 0; i < MAX_PORT; i++)
  1285. kfifo_free(&dc->port[i].fifo_ul);
  1286. err_free_sbuf:
  1287. kfree(dc->send_buf);
  1288. iounmap(dc->base_addr);
  1289. err_rel_regs:
  1290. pci_release_regions(pdev);
  1291. err_disable_device:
  1292. pci_disable_device(pdev);
  1293. err_free:
  1294. kfree(dc);
  1295. err:
  1296. return ret;
  1297. }
  1298. static void __devexit tty_exit(struct nozomi *dc)
  1299. {
  1300. unsigned int i;
  1301. DBG1(" ");
  1302. for (i = 0; i < MAX_PORT; ++i) {
  1303. struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
  1304. if (tty && list_empty(&tty->hangup_work.entry))
  1305. tty_hangup(tty);
  1306. tty_kref_put(tty);
  1307. }
  1308. /* Racy below - surely should wait for scheduled work to be done or
  1309. complete off a hangup method ? */
  1310. while (dc->open_ttys)
  1311. msleep(1);
  1312. for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
  1313. tty_unregister_device(ntty_driver, i);
  1314. }
  1315. /* Deallocate memory for one device */
  1316. static void __devexit nozomi_card_exit(struct pci_dev *pdev)
  1317. {
  1318. int i;
  1319. struct ctrl_ul ctrl;
  1320. struct nozomi *dc = pci_get_drvdata(pdev);
  1321. /* Disable all interrupts */
  1322. dc->last_ier = 0;
  1323. writew(dc->last_ier, dc->reg_ier);
  1324. tty_exit(dc);
  1325. /* Send 0x0001, command card to resend the reset token. */
  1326. /* This is to get the reset when the module is reloaded. */
  1327. ctrl.port = 0x00;
  1328. ctrl.reserved = 0;
  1329. ctrl.RTS = 0;
  1330. ctrl.DTR = 1;
  1331. DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
  1332. /* Setup dc->reg addresses to we can use defines here */
  1333. write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
  1334. writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
  1335. remove_sysfs_files(dc);
  1336. free_irq(pdev->irq, dc);
  1337. for (i = 0; i < MAX_PORT; i++)
  1338. kfifo_free(&dc->port[i].fifo_ul);
  1339. kfree(dc->send_buf);
  1340. iounmap(dc->base_addr);
  1341. pci_release_regions(pdev);
  1342. pci_disable_device(pdev);
  1343. ndevs[dc->index_start / MAX_PORT] = NULL;
  1344. kfree(dc);
  1345. }
  1346. static void set_rts(const struct tty_struct *tty, int rts)
  1347. {
  1348. struct port *port = get_port_by_tty(tty);
  1349. port->ctrl_ul.RTS = rts;
  1350. port->update_flow_control = 1;
  1351. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1352. }
  1353. static void set_dtr(const struct tty_struct *tty, int dtr)
  1354. {
  1355. struct port *port = get_port_by_tty(tty);
  1356. DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
  1357. port->ctrl_ul.DTR = dtr;
  1358. port->update_flow_control = 1;
  1359. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1360. }
  1361. /*
  1362. * ----------------------------------------------------------------------------
  1363. * TTY code
  1364. * ----------------------------------------------------------------------------
  1365. */
  1366. static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
  1367. {
  1368. struct port *port = get_port_by_tty(tty);
  1369. struct nozomi *dc = get_dc_by_tty(tty);
  1370. int ret;
  1371. if (!port || !dc || dc->state != NOZOMI_STATE_READY)
  1372. return -ENODEV;
  1373. ret = tty_init_termios(tty);
  1374. if (ret == 0) {
  1375. tty_driver_kref_get(driver);
  1376. tty->count++;
  1377. tty->driver_data = port;
  1378. driver->ttys[tty->index] = tty;
  1379. }
  1380. return ret;
  1381. }
  1382. static void ntty_cleanup(struct tty_struct *tty)
  1383. {
  1384. tty->driver_data = NULL;
  1385. }
  1386. static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
  1387. {
  1388. struct port *port = container_of(tport, struct port, port);
  1389. struct nozomi *dc = port->dc;
  1390. unsigned long flags;
  1391. DBG1("open: %d", port->token_dl);
  1392. spin_lock_irqsave(&dc->spin_mutex, flags);
  1393. dc->last_ier = dc->last_ier | port->token_dl;
  1394. writew(dc->last_ier, dc->reg_ier);
  1395. dc->open_ttys++;
  1396. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1397. printk("noz: activated %d: %p\n", tty->index, tport);
  1398. return 0;
  1399. }
  1400. static int ntty_open(struct tty_struct *tty, struct file *filp)
  1401. {
  1402. struct port *port = tty->driver_data;
  1403. return tty_port_open(&port->port, tty, filp);
  1404. }
  1405. static void ntty_shutdown(struct tty_port *tport)
  1406. {
  1407. struct port *port = container_of(tport, struct port, port);
  1408. struct nozomi *dc = port->dc;
  1409. unsigned long flags;
  1410. DBG1("close: %d", port->token_dl);
  1411. spin_lock_irqsave(&dc->spin_mutex, flags);
  1412. dc->last_ier &= ~(port->token_dl);
  1413. writew(dc->last_ier, dc->reg_ier);
  1414. dc->open_ttys--;
  1415. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1416. printk("noz: shutdown %p\n", tport);
  1417. }
  1418. static void ntty_close(struct tty_struct *tty, struct file *filp)
  1419. {
  1420. struct port *port = tty->driver_data;
  1421. if (port)
  1422. tty_port_close(&port->port, tty, filp);
  1423. }
  1424. static void ntty_hangup(struct tty_struct *tty)
  1425. {
  1426. struct port *port = tty->driver_data;
  1427. tty_port_hangup(&port->port);
  1428. }
  1429. /*
  1430. * called when the userspace process writes to the tty (/dev/noz*).
  1431. * Data is inserted into a fifo, which is then read and transferred to the modem.
  1432. */
  1433. static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
  1434. int count)
  1435. {
  1436. int rval = -EINVAL;
  1437. struct nozomi *dc = get_dc_by_tty(tty);
  1438. struct port *port = tty->driver_data;
  1439. unsigned long flags;
  1440. /* DBG1( "WRITEx: %d, index = %d", count, index); */
  1441. if (!dc || !port)
  1442. return -ENODEV;
  1443. rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
  1444. /* notify card */
  1445. if (unlikely(dc == NULL)) {
  1446. DBG1("No device context?");
  1447. goto exit;
  1448. }
  1449. spin_lock_irqsave(&dc->spin_mutex, flags);
  1450. /* CTS is only valid on the modem channel */
  1451. if (port == &(dc->port[PORT_MDM])) {
  1452. if (port->ctrl_dl.CTS) {
  1453. DBG4("Enable interrupt");
  1454. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1455. } else {
  1456. dev_err(&dc->pdev->dev,
  1457. "CTS not active on modem port?\n");
  1458. }
  1459. } else {
  1460. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1461. }
  1462. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1463. exit:
  1464. return rval;
  1465. }
  1466. /*
  1467. * Calculate how much is left in device
  1468. * This method is called by the upper tty layer.
  1469. * #according to sources N_TTY.c it expects a value >= 0 and
  1470. * does not check for negative values.
  1471. *
  1472. * If the port is unplugged report lots of room and let the bits
  1473. * dribble away so we don't block anything.
  1474. */
  1475. static int ntty_write_room(struct tty_struct *tty)
  1476. {
  1477. struct port *port = tty->driver_data;
  1478. int room = 4096;
  1479. const struct nozomi *dc = get_dc_by_tty(tty);
  1480. if (dc)
  1481. room = kfifo_avail(&port->fifo_ul);
  1482. return room;
  1483. }
  1484. /* Gets io control parameters */
  1485. static int ntty_tiocmget(struct tty_struct *tty)
  1486. {
  1487. const struct port *port = tty->driver_data;
  1488. const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
  1489. const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
  1490. /* Note: these could change under us but it is not clear this
  1491. matters if so */
  1492. return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
  1493. (ctrl_ul->DTR ? TIOCM_DTR : 0) |
  1494. (ctrl_dl->DCD ? TIOCM_CAR : 0) |
  1495. (ctrl_dl->RI ? TIOCM_RNG : 0) |
  1496. (ctrl_dl->DSR ? TIOCM_DSR : 0) |
  1497. (ctrl_dl->CTS ? TIOCM_CTS : 0);
  1498. }
  1499. /* Sets io controls parameters */
  1500. static int ntty_tiocmset(struct tty_struct *tty,
  1501. unsigned int set, unsigned int clear)
  1502. {
  1503. struct nozomi *dc = get_dc_by_tty(tty);
  1504. unsigned long flags;
  1505. spin_lock_irqsave(&dc->spin_mutex, flags);
  1506. if (set & TIOCM_RTS)
  1507. set_rts(tty, 1);
  1508. else if (clear & TIOCM_RTS)
  1509. set_rts(tty, 0);
  1510. if (set & TIOCM_DTR)
  1511. set_dtr(tty, 1);
  1512. else if (clear & TIOCM_DTR)
  1513. set_dtr(tty, 0);
  1514. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1515. return 0;
  1516. }
  1517. static int ntty_cflags_changed(struct port *port, unsigned long flags,
  1518. struct async_icount *cprev)
  1519. {
  1520. const struct async_icount cnow = port->tty_icount;
  1521. int ret;
  1522. ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
  1523. ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
  1524. ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
  1525. ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
  1526. *cprev = cnow;
  1527. return ret;
  1528. }
  1529. static int ntty_tiocgicount(struct tty_struct *tty,
  1530. struct serial_icounter_struct *icount)
  1531. {
  1532. struct port *port = tty->driver_data;
  1533. const struct async_icount cnow = port->tty_icount;
  1534. icount->cts = cnow.cts;
  1535. icount->dsr = cnow.dsr;
  1536. icount->rng = cnow.rng;
  1537. icount->dcd = cnow.dcd;
  1538. icount->rx = cnow.rx;
  1539. icount->tx = cnow.tx;
  1540. icount->frame = cnow.frame;
  1541. icount->overrun = cnow.overrun;
  1542. icount->parity = cnow.parity;
  1543. icount->brk = cnow.brk;
  1544. icount->buf_overrun = cnow.buf_overrun;
  1545. return 0;
  1546. }
  1547. static int ntty_ioctl(struct tty_struct *tty,
  1548. unsigned int cmd, unsigned long arg)
  1549. {
  1550. struct port *port = tty->driver_data;
  1551. int rval = -ENOIOCTLCMD;
  1552. DBG1("******** IOCTL, cmd: %d", cmd);
  1553. switch (cmd) {
  1554. case TIOCMIWAIT: {
  1555. struct async_icount cprev = port->tty_icount;
  1556. rval = wait_event_interruptible(port->tty_wait,
  1557. ntty_cflags_changed(port, arg, &cprev));
  1558. break;
  1559. }
  1560. default:
  1561. DBG1("ERR: 0x%08X, %d", cmd, cmd);
  1562. break;
  1563. };
  1564. return rval;
  1565. }
  1566. /*
  1567. * Called by the upper tty layer when tty buffers are ready
  1568. * to receive data again after a call to throttle.
  1569. */
  1570. static void ntty_unthrottle(struct tty_struct *tty)
  1571. {
  1572. struct nozomi *dc = get_dc_by_tty(tty);
  1573. unsigned long flags;
  1574. DBG1("UNTHROTTLE");
  1575. spin_lock_irqsave(&dc->spin_mutex, flags);
  1576. enable_transmit_dl(tty->index % MAX_PORT, dc);
  1577. set_rts(tty, 1);
  1578. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1579. }
  1580. /*
  1581. * Called by the upper tty layer when the tty buffers are almost full.
  1582. * The driver should stop send more data.
  1583. */
  1584. static void ntty_throttle(struct tty_struct *tty)
  1585. {
  1586. struct nozomi *dc = get_dc_by_tty(tty);
  1587. unsigned long flags;
  1588. DBG1("THROTTLE");
  1589. spin_lock_irqsave(&dc->spin_mutex, flags);
  1590. set_rts(tty, 0);
  1591. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1592. }
  1593. /* Returns number of chars in buffer, called by tty layer */
  1594. static s32 ntty_chars_in_buffer(struct tty_struct *tty)
  1595. {
  1596. struct port *port = tty->driver_data;
  1597. struct nozomi *dc = get_dc_by_tty(tty);
  1598. s32 rval = 0;
  1599. if (unlikely(!dc || !port)) {
  1600. goto exit_in_buffer;
  1601. }
  1602. rval = kfifo_len(&port->fifo_ul);
  1603. exit_in_buffer:
  1604. return rval;
  1605. }
  1606. static const struct tty_port_operations noz_tty_port_ops = {
  1607. .activate = ntty_activate,
  1608. .shutdown = ntty_shutdown,
  1609. };
  1610. static const struct tty_operations tty_ops = {
  1611. .ioctl = ntty_ioctl,
  1612. .open = ntty_open,
  1613. .close = ntty_close,
  1614. .hangup = ntty_hangup,
  1615. .write = ntty_write,
  1616. .write_room = ntty_write_room,
  1617. .unthrottle = ntty_unthrottle,
  1618. .throttle = ntty_throttle,
  1619. .chars_in_buffer = ntty_chars_in_buffer,
  1620. .tiocmget = ntty_tiocmget,
  1621. .tiocmset = ntty_tiocmset,
  1622. .get_icount = ntty_tiocgicount,
  1623. .install = ntty_install,
  1624. .cleanup = ntty_cleanup,
  1625. };
  1626. /* Module initialization */
  1627. static struct pci_driver nozomi_driver = {
  1628. .name = NOZOMI_NAME,
  1629. .id_table = nozomi_pci_tbl,
  1630. .probe = nozomi_card_init,
  1631. .remove = __devexit_p(nozomi_card_exit),
  1632. };
  1633. static __init int nozomi_init(void)
  1634. {
  1635. int ret;
  1636. printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
  1637. ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
  1638. if (!ntty_driver)
  1639. return -ENOMEM;
  1640. ntty_driver->owner = THIS_MODULE;
  1641. ntty_driver->driver_name = NOZOMI_NAME_TTY;
  1642. ntty_driver->name = "noz";
  1643. ntty_driver->major = 0;
  1644. ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1645. ntty_driver->subtype = SERIAL_TYPE_NORMAL;
  1646. ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1647. ntty_driver->init_termios = tty_std_termios;
  1648. ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
  1649. HUPCL | CLOCAL;
  1650. ntty_driver->init_termios.c_ispeed = 115200;
  1651. ntty_driver->init_termios.c_ospeed = 115200;
  1652. tty_set_operations(ntty_driver, &tty_ops);
  1653. ret = tty_register_driver(ntty_driver);
  1654. if (ret) {
  1655. printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
  1656. goto free_tty;
  1657. }
  1658. ret = pci_register_driver(&nozomi_driver);
  1659. if (ret) {
  1660. printk(KERN_ERR "Nozomi: can't register pci driver\n");
  1661. goto unr_tty;
  1662. }
  1663. return 0;
  1664. unr_tty:
  1665. tty_unregister_driver(ntty_driver);
  1666. free_tty:
  1667. put_tty_driver(ntty_driver);
  1668. return ret;
  1669. }
  1670. static __exit void nozomi_exit(void)
  1671. {
  1672. printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
  1673. pci_unregister_driver(&nozomi_driver);
  1674. tty_unregister_driver(ntty_driver);
  1675. put_tty_driver(ntty_driver);
  1676. }
  1677. module_init(nozomi_init);
  1678. module_exit(nozomi_exit);
  1679. module_param(debug, int, S_IRUGO | S_IWUSR);
  1680. MODULE_LICENSE("Dual BSD/GPL");
  1681. MODULE_DESCRIPTION(DRIVER_DESC);