divasmain.c 21 KB

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