divasmain.c 21 KB

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