nozomi.c 49 KB

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