divasmain.c 21 KB

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