divasmain.c 21 KB

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