nozomi.c 47 KB

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