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