divasmain.c 21 KB

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