divasmain.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /* $Id: divasmain.c,v 1.55.4.6 2005/02/09 19:28:20 armin Exp $
  2. *
  3. * Low level driver for Eicon DIVA Server ISDN cards.
  4. *
  5. * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
  6. * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. */
  11. #include <linux/config.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/devfs_fs_kernel.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/io.h>
  19. #include <linux/ioport.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/pci.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/list.h>
  25. #include <linux/poll.h>
  26. #include <linux/kmod.h>
  27. #include "platform.h"
  28. #undef ID_MASK
  29. #undef N_DATA
  30. #include "pc.h"
  31. #include "di_defs.h"
  32. #include "divasync.h"
  33. #include "diva.h"
  34. #include "di.h"
  35. #include "io.h"
  36. #include "xdi_msg.h"
  37. #include "xdi_adapter.h"
  38. #include "xdi_vers.h"
  39. #include "diva_dma.h"
  40. #include "diva_pci.h"
  41. static char *main_revision = "$Revision: 1.55.4.6 $";
  42. static int major;
  43. static int dbgmask;
  44. MODULE_DESCRIPTION("Kernel driver for Eicon DIVA Server cards");
  45. MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
  46. MODULE_LICENSE("GPL");
  47. module_param(dbgmask, int, 0);
  48. MODULE_PARM_DESC(dbgmask, "initial debug mask");
  49. static char *DRIVERNAME =
  50. "Eicon DIVA Server driver (http://www.melware.net)";
  51. static char *DRIVERLNAME = "divas";
  52. static char *DEVNAME = "Divas";
  53. char *DRIVERRELEASE_DIVAS = "2.0";
  54. extern irqreturn_t diva_os_irq_wrapper(int irq, void *context,
  55. struct pt_regs *regs);
  56. extern int create_divas_proc(void);
  57. extern void remove_divas_proc(void);
  58. extern void diva_get_vserial_number(PISDN_ADAPTER IoAdapter, char *buf);
  59. extern int divasfunc_init(int dbgmask);
  60. extern void divasfunc_exit(void);
  61. typedef struct _diva_os_thread_dpc {
  62. struct tasklet_struct divas_task;
  63. diva_os_soft_isr_t *psoft_isr;
  64. } diva_os_thread_dpc_t;
  65. /* --------------------------------------------------------------------------
  66. PCI driver interface section
  67. -------------------------------------------------------------------------- */
  68. /*
  69. vendor, device Vendor and device ID to match (or PCI_ANY_ID)
  70. subvendor, Subsystem vendor and device ID to match (or PCI_ANY_ID)
  71. subdevice
  72. class, Device class to match. The class_mask tells which bits
  73. class_mask of the class are honored during the comparison.
  74. driver_data Data private to the driver.
  75. */
  76. #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2)
  77. #define PCI_DEVICE_ID_EICON_MAESTRAP_2 0xE015
  78. #endif
  79. #if !defined(PCI_DEVICE_ID_EICON_4BRI_VOIP)
  80. #define PCI_DEVICE_ID_EICON_4BRI_VOIP 0xE016
  81. #endif
  82. #if !defined(PCI_DEVICE_ID_EICON_4BRI_2_VOIP)
  83. #define PCI_DEVICE_ID_EICON_4BRI_2_VOIP 0xE017
  84. #endif
  85. #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2)
  86. #define PCI_DEVICE_ID_EICON_BRI2M_2 0xE018
  87. #endif
  88. #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP)
  89. #define PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP 0xE019
  90. #endif
  91. #if !defined(PCI_DEVICE_ID_EICON_2F)
  92. #define PCI_DEVICE_ID_EICON_2F 0xE01A
  93. #endif
  94. #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2_VOIP)
  95. #define PCI_DEVICE_ID_EICON_BRI2M_2_VOIP 0xE01B
  96. #endif
  97. /*
  98. This table should be sorted by PCI device ID
  99. */
  100. static struct pci_device_id divas_pci_tbl[] = {
  101. /* Diva Server BRI-2M PCI 0xE010 */
  102. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRA,
  103. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_MAESTRA_PCI},
  104. /* Diva Server 4BRI-8M PCI 0xE012 */
  105. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ,
  106. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_PCI},
  107. /* Diva Server 4BRI-8M 2.0 PCI 0xE013 */
  108. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ_U,
  109. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_V2_PCI},
  110. /* Diva Server PRI-30M PCI 0xE014 */
  111. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP,
  112. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_PCI},
  113. /* Diva Server PRI 2.0 adapter 0xE015 */
  114. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2,
  115. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_V2_PCI},
  116. /* Diva Server Voice 4BRI-8M PCI 0xE016 */
  117. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_VOIP,
  118. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_PCI},
  119. /* Diva Server Voice 4BRI-8M 2.0 PCI 0xE017 */
  120. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_2_VOIP,
  121. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_V2_PCI},
  122. /* Diva Server BRI-2M 2.0 PCI 0xE018 */
  123. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2,
  124. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2M_V2_PCI},
  125. /* Diva Server Voice PRI 2.0 PCI 0xE019 */
  126. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP,
  127. PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  128. CARDTYPE_DIVASRV_VOICE_P_30M_V2_PCI},
  129. /* Diva Server 2FX 0xE01A */
  130. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_2F,
  131. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2F_PCI},
  132. /* Diva Server Voice BRI-2M 2.0 PCI 0xE01B */
  133. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2_VOIP,
  134. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_B_2M_V2_PCI},
  135. {0,} /* 0 terminated list. */
  136. };
  137. MODULE_DEVICE_TABLE(pci, divas_pci_tbl);
  138. static int divas_init_one(struct pci_dev *pdev,
  139. const struct pci_device_id *ent);
  140. static void __devexit divas_remove_one(struct pci_dev *pdev);
  141. static struct pci_driver diva_pci_driver = {
  142. .name = "divas",
  143. .probe = divas_init_one,
  144. .remove = __devexit_p(divas_remove_one),
  145. .id_table = divas_pci_tbl,
  146. };
  147. /*********************************************************
  148. ** little helper functions
  149. *********************************************************/
  150. static char *getrev(const char *revision)
  151. {
  152. char *rev;
  153. char *p;
  154. if ((p = strchr(revision, ':'))) {
  155. rev = p + 2;
  156. p = strchr(rev, '$');
  157. *--p = 0;
  158. } else
  159. rev = "1.0";
  160. return rev;
  161. }
  162. void diva_log_info(unsigned char *format, ...)
  163. {
  164. va_list args;
  165. unsigned char line[160];
  166. va_start(args, format);
  167. vsprintf(line, format, args);
  168. va_end(args);
  169. printk(KERN_INFO "%s: %s\n", DRIVERLNAME, line);
  170. }
  171. void divas_get_version(char *p)
  172. {
  173. char tmprev[32];
  174. strcpy(tmprev, main_revision);
  175. sprintf(p, "%s: %s(%s) %s(%s) major=%d\n", DRIVERLNAME, DRIVERRELEASE_DIVAS,
  176. getrev(tmprev), diva_xdi_common_code_build, DIVA_BUILD, major);
  177. }
  178. /* --------------------------------------------------------------------------
  179. PCI Bus services
  180. -------------------------------------------------------------------------- */
  181. byte diva_os_get_pci_bus(void *pci_dev_handle)
  182. {
  183. struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
  184. return ((byte) pdev->bus->number);
  185. }
  186. byte diva_os_get_pci_func(void *pci_dev_handle)
  187. {
  188. struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
  189. return ((byte) pdev->devfn);
  190. }
  191. unsigned long divasa_get_pci_irq(unsigned char bus, unsigned char func,
  192. void *pci_dev_handle)
  193. {
  194. unsigned char irq = 0;
  195. struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
  196. irq = dev->irq;
  197. return ((unsigned long) irq);
  198. }
  199. unsigned long divasa_get_pci_bar(unsigned char bus, unsigned char func,
  200. int bar, void *pci_dev_handle)
  201. {
  202. unsigned long ret = 0;
  203. struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
  204. if (bar < 6) {
  205. ret = dev->resource[bar].start;
  206. }
  207. DBG_TRC(("GOT BAR[%d]=%08x", bar, ret));
  208. {
  209. unsigned long type = (ret & 0x00000001);
  210. if (type & PCI_BASE_ADDRESS_SPACE_IO) {
  211. DBG_TRC((" I/O"));
  212. ret &= PCI_BASE_ADDRESS_IO_MASK;
  213. } else {
  214. DBG_TRC((" memory"));
  215. ret &= PCI_BASE_ADDRESS_MEM_MASK;
  216. }
  217. DBG_TRC((" final=%08x", ret));
  218. }
  219. return (ret);
  220. }
  221. void PCIwrite(byte bus, byte func, int offset, void *data, int length,
  222. void *pci_dev_handle)
  223. {
  224. struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
  225. switch (length) {
  226. case 1: /* byte */
  227. pci_write_config_byte(dev, offset,
  228. *(unsigned char *) data);
  229. break;
  230. case 2: /* word */
  231. pci_write_config_word(dev, offset,
  232. *(unsigned short *) data);
  233. break;
  234. case 4: /* dword */
  235. pci_write_config_dword(dev, offset,
  236. *(unsigned int *) data);
  237. break;
  238. default: /* buffer */
  239. if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
  240. dword *p = (dword *) data;
  241. length /= 4;
  242. while (length--) {
  243. pci_write_config_dword(dev, offset,
  244. *(unsigned int *)
  245. p++);
  246. }
  247. } else { /* copy as byte stream */
  248. byte *p = (byte *) data;
  249. while (length--) {
  250. pci_write_config_byte(dev, offset,
  251. *(unsigned char *)
  252. p++);
  253. }
  254. }
  255. }
  256. }
  257. void PCIread(byte bus, byte func, int offset, void *data, int length,
  258. void *pci_dev_handle)
  259. {
  260. struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
  261. switch (length) {
  262. case 1: /* byte */
  263. pci_read_config_byte(dev, offset, (unsigned char *) data);
  264. break;
  265. case 2: /* word */
  266. pci_read_config_word(dev, offset, (unsigned short *) data);
  267. break;
  268. case 4: /* dword */
  269. pci_read_config_dword(dev, offset, (unsigned int *) data);
  270. break;
  271. default: /* buffer */
  272. if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
  273. dword *p = (dword *) data;
  274. length /= 4;
  275. while (length--) {
  276. pci_read_config_dword(dev, offset,
  277. (unsigned int *)
  278. p++);
  279. }
  280. } else { /* copy as byte stream */
  281. byte *p = (byte *) data;
  282. while (length--) {
  283. pci_read_config_byte(dev, offset,
  284. (unsigned char *)
  285. p++);
  286. }
  287. }
  288. }
  289. }
  290. /*
  291. Init map with DMA pages. It is not problem if some allocations fail -
  292. the channels that will not get one DMA page will use standard PIO
  293. interface
  294. */
  295. static void *diva_pci_alloc_consistent(struct pci_dev *hwdev,
  296. size_t size,
  297. dma_addr_t * dma_handle,
  298. void **addr_handle)
  299. {
  300. void *addr = pci_alloc_consistent(hwdev, size, dma_handle);
  301. *addr_handle = addr;
  302. return (addr);
  303. }
  304. void diva_init_dma_map(void *hdev,
  305. struct _diva_dma_map_entry **ppmap, int nentries)
  306. {
  307. struct pci_dev *pdev = (struct pci_dev *) hdev;
  308. struct _diva_dma_map_entry *pmap =
  309. diva_alloc_dma_map(hdev, nentries);
  310. if (pmap) {
  311. int i;
  312. dma_addr_t dma_handle;
  313. void *cpu_addr;
  314. void *addr_handle;
  315. for (i = 0; i < nentries; i++) {
  316. if (!(cpu_addr = diva_pci_alloc_consistent(pdev,
  317. PAGE_SIZE,
  318. &dma_handle,
  319. &addr_handle)))
  320. {
  321. break;
  322. }
  323. diva_init_dma_map_entry(pmap, i, cpu_addr,
  324. (dword) dma_handle,
  325. addr_handle);
  326. DBG_TRC(("dma map alloc [%d]=(%08lx:%08x:%08lx)",
  327. i, (unsigned long) cpu_addr,
  328. (dword) dma_handle,
  329. (unsigned long) addr_handle))}
  330. }
  331. *ppmap = pmap;
  332. }
  333. /*
  334. Free all contained in the map entries and memory used by the map
  335. Should be always called after adapter removal from DIDD array
  336. */
  337. void diva_free_dma_map(void *hdev, struct _diva_dma_map_entry *pmap)
  338. {
  339. struct pci_dev *pdev = (struct pci_dev *) hdev;
  340. int i;
  341. dword phys_addr;
  342. void *cpu_addr;
  343. dma_addr_t dma_handle;
  344. void *addr_handle;
  345. for (i = 0; (pmap != 0); i++) {
  346. diva_get_dma_map_entry(pmap, i, &cpu_addr, &phys_addr);
  347. if (!cpu_addr) {
  348. break;
  349. }
  350. addr_handle = diva_get_entry_handle(pmap, i);
  351. dma_handle = (dma_addr_t) phys_addr;
  352. pci_free_consistent(pdev, PAGE_SIZE, addr_handle,
  353. dma_handle);
  354. DBG_TRC(("dma map free [%d]=(%08lx:%08x:%08lx)", i,
  355. (unsigned long) cpu_addr, (dword) dma_handle,
  356. (unsigned long) addr_handle))
  357. }
  358. diva_free_dma_mapping(pmap);
  359. }
  360. /*********************************************************
  361. ** I/O port utilities
  362. *********************************************************/
  363. int
  364. diva_os_register_io_port(void *adapter, int on, unsigned long port,
  365. unsigned long length, const char *name, int id)
  366. {
  367. if (on) {
  368. if (!request_region(port, length, name)) {
  369. DBG_ERR(("A: I/O: can't register port=%08x", port))
  370. return (-1);
  371. }
  372. } else {
  373. release_region(port, length);
  374. }
  375. return (0);
  376. }
  377. void __iomem *divasa_remap_pci_bar(diva_os_xdi_adapter_t *a, int id, unsigned long bar, unsigned long area_length)
  378. {
  379. void __iomem *ret = ioremap(bar, area_length);
  380. DBG_TRC(("remap(%08x)->%p", bar, ret));
  381. return (ret);
  382. }
  383. void divasa_unmap_pci_bar(void __iomem *bar)
  384. {
  385. if (bar) {
  386. iounmap(bar);
  387. }
  388. }
  389. /*********************************************************
  390. ** I/O port access
  391. *********************************************************/
  392. byte __inline__ inpp(void __iomem *addr)
  393. {
  394. return (inb((unsigned long) addr));
  395. }
  396. word __inline__ inppw(void __iomem *addr)
  397. {
  398. return (inw((unsigned long) addr));
  399. }
  400. void __inline__ inppw_buffer(void __iomem *addr, void *P, int length)
  401. {
  402. insw((unsigned long) addr, (word *) P, length >> 1);
  403. }
  404. void __inline__ outppw_buffer(void __iomem *addr, void *P, int length)
  405. {
  406. outsw((unsigned long) addr, (word *) P, length >> 1);
  407. }
  408. void __inline__ outppw(void __iomem *addr, word w)
  409. {
  410. outw(w, (unsigned long) addr);
  411. }
  412. void __inline__ outpp(void __iomem *addr, word p)
  413. {
  414. outb(p, (unsigned long) addr);
  415. }
  416. /* --------------------------------------------------------------------------
  417. IRQ request / remove
  418. -------------------------------------------------------------------------- */
  419. int diva_os_register_irq(void *context, byte irq, const char *name)
  420. {
  421. int result = request_irq(irq, diva_os_irq_wrapper,
  422. SA_INTERRUPT | SA_SHIRQ, name, context);
  423. return (result);
  424. }
  425. void diva_os_remove_irq(void *context, byte irq)
  426. {
  427. free_irq(irq, context);
  428. }
  429. /* --------------------------------------------------------------------------
  430. DPC framework implementation
  431. -------------------------------------------------------------------------- */
  432. static void diva_os_dpc_proc(unsigned long context)
  433. {
  434. diva_os_thread_dpc_t *psoft_isr = (diva_os_thread_dpc_t *) context;
  435. diva_os_soft_isr_t *pisr = psoft_isr->psoft_isr;
  436. (*(pisr->callback)) (pisr, pisr->callback_context);
  437. }
  438. int diva_os_initialize_soft_isr(diva_os_soft_isr_t * psoft_isr,
  439. diva_os_soft_isr_callback_t callback,
  440. void *callback_context)
  441. {
  442. diva_os_thread_dpc_t *pdpc;
  443. pdpc = (diva_os_thread_dpc_t *) diva_os_malloc(0, sizeof(*pdpc));
  444. if (!(psoft_isr->object = pdpc)) {
  445. return (-1);
  446. }
  447. memset(pdpc, 0x00, sizeof(*pdpc));
  448. psoft_isr->callback = callback;
  449. psoft_isr->callback_context = callback_context;
  450. pdpc->psoft_isr = psoft_isr;
  451. tasklet_init(&pdpc->divas_task, diva_os_dpc_proc, (unsigned long)pdpc);
  452. return (0);
  453. }
  454. int diva_os_schedule_soft_isr(diva_os_soft_isr_t * psoft_isr)
  455. {
  456. if (psoft_isr && psoft_isr->object) {
  457. diva_os_thread_dpc_t *pdpc =
  458. (diva_os_thread_dpc_t *) psoft_isr->object;
  459. tasklet_schedule(&pdpc->divas_task);
  460. }
  461. return (1);
  462. }
  463. int diva_os_cancel_soft_isr(diva_os_soft_isr_t * psoft_isr)
  464. {
  465. return (0);
  466. }
  467. void diva_os_remove_soft_isr(diva_os_soft_isr_t * psoft_isr)
  468. {
  469. if (psoft_isr && psoft_isr->object) {
  470. diva_os_thread_dpc_t *pdpc =
  471. (diva_os_thread_dpc_t *) psoft_isr->object;
  472. void *mem;
  473. tasklet_kill(&pdpc->divas_task);
  474. flush_scheduled_work();
  475. mem = psoft_isr->object;
  476. psoft_isr->object = NULL;
  477. diva_os_free(0, mem);
  478. }
  479. }
  480. /*
  481. * kernel/user space copy functions
  482. */
  483. static int
  484. xdi_copy_to_user(void *os_handle, void __user *dst, const void *src, int length)
  485. {
  486. if (copy_to_user(dst, src, length)) {
  487. return (-EFAULT);
  488. }
  489. return (length);
  490. }
  491. static int
  492. xdi_copy_from_user(void *os_handle, void *dst, const void __user *src, int length)
  493. {
  494. if (copy_from_user(dst, src, length)) {
  495. return (-EFAULT);
  496. }
  497. return (length);
  498. }
  499. /*
  500. * device node operations
  501. */
  502. static int divas_open(struct inode *inode, struct file *file)
  503. {
  504. return (0);
  505. }
  506. static int divas_release(struct inode *inode, struct file *file)
  507. {
  508. if (file->private_data) {
  509. diva_xdi_close_adapter(file->private_data, file);
  510. }
  511. return (0);
  512. }
  513. static ssize_t divas_write(struct file *file, const char __user *buf,
  514. size_t count, loff_t * ppos)
  515. {
  516. int ret = -EINVAL;
  517. if (!file->private_data) {
  518. file->private_data = diva_xdi_open_adapter(file, buf,
  519. count,
  520. xdi_copy_from_user);
  521. }
  522. if (!file->private_data) {
  523. return (-ENODEV);
  524. }
  525. ret = diva_xdi_write(file->private_data, file,
  526. buf, count, xdi_copy_from_user);
  527. switch (ret) {
  528. case -1: /* Message should be removed from rx mailbox first */
  529. ret = -EBUSY;
  530. break;
  531. case -2: /* invalid adapter was specified in this call */
  532. ret = -ENOMEM;
  533. break;
  534. case -3:
  535. ret = -ENXIO;
  536. break;
  537. }
  538. DBG_TRC(("write: ret %d", ret));
  539. return (ret);
  540. }
  541. static ssize_t divas_read(struct file *file, char __user *buf,
  542. size_t count, loff_t * ppos)
  543. {
  544. int ret = -EINVAL;
  545. if (!file->private_data) {
  546. file->private_data = diva_xdi_open_adapter(file, buf,
  547. count,
  548. xdi_copy_from_user);
  549. }
  550. if (!file->private_data) {
  551. return (-ENODEV);
  552. }
  553. ret = diva_xdi_read(file->private_data, file,
  554. buf, count, xdi_copy_to_user);
  555. switch (ret) {
  556. case -1: /* RX mailbox is empty */
  557. ret = -EAGAIN;
  558. break;
  559. case -2: /* no memory, mailbox was cleared, last command is failed */
  560. ret = -ENOMEM;
  561. break;
  562. case -3: /* can't copy to user, retry */
  563. ret = -EFAULT;
  564. break;
  565. }
  566. DBG_TRC(("read: ret %d", ret));
  567. return (ret);
  568. }
  569. static unsigned int divas_poll(struct file *file, poll_table * wait)
  570. {
  571. if (!file->private_data) {
  572. return (POLLERR);
  573. }
  574. return (POLLIN | POLLRDNORM);
  575. }
  576. static struct file_operations divas_fops = {
  577. .owner = THIS_MODULE,
  578. .llseek = no_llseek,
  579. .read = divas_read,
  580. .write = divas_write,
  581. .poll = divas_poll,
  582. .open = divas_open,
  583. .release = divas_release
  584. };
  585. static void divas_unregister_chrdev(void)
  586. {
  587. devfs_remove(DEVNAME);
  588. unregister_chrdev(major, DEVNAME);
  589. }
  590. static int DIVA_INIT_FUNCTION divas_register_chrdev(void)
  591. {
  592. if ((major = register_chrdev(0, DEVNAME, &divas_fops)) < 0)
  593. {
  594. printk(KERN_ERR "%s: failed to create /dev entry.\n",
  595. DRIVERLNAME);
  596. return (0);
  597. }
  598. devfs_mk_cdev(MKDEV(major, 0), S_IFCHR|S_IRUSR|S_IWUSR, DEVNAME);
  599. return (1);
  600. }
  601. /* --------------------------------------------------------------------------
  602. PCI driver section
  603. -------------------------------------------------------------------------- */
  604. static int __devinit divas_init_one(struct pci_dev *pdev,
  605. const struct pci_device_id *ent)
  606. {
  607. void *pdiva = NULL;
  608. u8 pci_latency;
  609. u8 new_latency = 32;
  610. DBG_TRC(("%s bus: %08x fn: %08x insertion.\n",
  611. CardProperties[ent->driver_data].Name,
  612. pdev->bus->number, pdev->devfn))
  613. printk(KERN_INFO "%s: %s bus: %08x fn: %08x insertion.\n",
  614. DRIVERLNAME, CardProperties[ent->driver_data].Name,
  615. pdev->bus->number, pdev->devfn);
  616. if (pci_enable_device(pdev)) {
  617. DBG_TRC(("%s: %s bus: %08x fn: %08x device init failed.\n",
  618. DRIVERLNAME,
  619. CardProperties[ent->driver_data].Name,
  620. pdev->bus->number,
  621. pdev->devfn))
  622. printk(KERN_ERR
  623. "%s: %s bus: %08x fn: %08x device init failed.\n",
  624. DRIVERLNAME,
  625. CardProperties[ent->driver_data].
  626. Name, pdev->bus->number,
  627. pdev->devfn);
  628. return (-EIO);
  629. }
  630. pci_set_master(pdev);
  631. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
  632. if (!pci_latency) {
  633. DBG_TRC(("%s: bus: %08x fn: %08x fix latency.\n",
  634. DRIVERLNAME, pdev->bus->number, pdev->devfn))
  635. printk(KERN_INFO
  636. "%s: bus: %08x fn: %08x fix latency.\n",
  637. DRIVERLNAME, pdev->bus->number, pdev->devfn);
  638. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, new_latency);
  639. }
  640. if (!(pdiva = diva_driver_add_card(pdev, ent->driver_data))) {
  641. DBG_TRC(("%s: %s bus: %08x fn: %08x card init failed.\n",
  642. DRIVERLNAME,
  643. CardProperties[ent->driver_data].Name,
  644. pdev->bus->number,
  645. pdev->devfn))
  646. printk(KERN_ERR
  647. "%s: %s bus: %08x fn: %08x card init failed.\n",
  648. DRIVERLNAME,
  649. CardProperties[ent->driver_data].
  650. Name, pdev->bus->number,
  651. pdev->devfn);
  652. return (-EIO);
  653. }
  654. pci_set_drvdata(pdev, pdiva);
  655. return (0);
  656. }
  657. static void __devexit divas_remove_one(struct pci_dev *pdev)
  658. {
  659. void *pdiva = pci_get_drvdata(pdev);
  660. DBG_TRC(("bus: %08x fn: %08x removal.\n",
  661. pdev->bus->number, pdev->devfn))
  662. printk(KERN_INFO "%s: bus: %08x fn: %08x removal.\n",
  663. DRIVERLNAME, pdev->bus->number, pdev->devfn);
  664. if (pdiva) {
  665. diva_driver_remove_card(pdiva);
  666. }
  667. }
  668. /* --------------------------------------------------------------------------
  669. Driver Load / Startup
  670. -------------------------------------------------------------------------- */
  671. static int DIVA_INIT_FUNCTION divas_init(void)
  672. {
  673. char tmprev[50];
  674. int ret = 0;
  675. printk(KERN_INFO "%s\n", DRIVERNAME);
  676. printk(KERN_INFO "%s: Rel:%s Rev:", DRIVERLNAME, DRIVERRELEASE_DIVAS);
  677. strcpy(tmprev, main_revision);
  678. printk("%s Build: %s(%s)\n", getrev(tmprev),
  679. diva_xdi_common_code_build, DIVA_BUILD);
  680. printk(KERN_INFO "%s: support for: ", DRIVERLNAME);
  681. #ifdef CONFIG_ISDN_DIVAS_BRIPCI
  682. printk("BRI/PCI ");
  683. #endif
  684. #ifdef CONFIG_ISDN_DIVAS_PRIPCI
  685. printk("PRI/PCI ");
  686. #endif
  687. printk("adapters\n");
  688. if (!divasfunc_init(dbgmask)) {
  689. printk(KERN_ERR "%s: failed to connect to DIDD.\n",
  690. DRIVERLNAME);
  691. ret = -EIO;
  692. goto out;
  693. }
  694. if (!divas_register_chrdev()) {
  695. #ifdef MODULE
  696. divasfunc_exit();
  697. #endif
  698. ret = -EIO;
  699. goto out;
  700. }
  701. if (!create_divas_proc()) {
  702. #ifdef MODULE
  703. remove_divas_proc();
  704. divas_unregister_chrdev();
  705. divasfunc_exit();
  706. #endif
  707. printk(KERN_ERR "%s: failed to create proc entry.\n",
  708. DRIVERLNAME);
  709. ret = -EIO;
  710. goto out;
  711. }
  712. if ((ret = pci_register_driver(&diva_pci_driver))) {
  713. #ifdef MODULE
  714. remove_divas_proc();
  715. divas_unregister_chrdev();
  716. divasfunc_exit();
  717. #endif
  718. printk(KERN_ERR "%s: failed to init pci driver.\n",
  719. DRIVERLNAME);
  720. goto out;
  721. }
  722. printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
  723. out:
  724. return (ret);
  725. }
  726. /* --------------------------------------------------------------------------
  727. Driver Unload
  728. -------------------------------------------------------------------------- */
  729. static void DIVA_EXIT_FUNCTION divas_exit(void)
  730. {
  731. pci_unregister_driver(&diva_pci_driver);
  732. remove_divas_proc();
  733. divas_unregister_chrdev();
  734. divasfunc_exit();
  735. printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
  736. }
  737. module_init(divas_init);
  738. module_exit(divas_exit);