nozomi.c 48 KB

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