common.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. * Stuff used by all variants of the driver
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers,
  5. * Hansjoerg Lipp <hjlipp@web.de>,
  6. * Tilman Schmidt <tilman@imap.cc>.
  7. *
  8. * =====================================================================
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. * =====================================================================
  14. */
  15. #include "gigaset.h"
  16. #include <linux/ctype.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. /* Version Information */
  20. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
  21. #define DRIVER_DESC "Driver for Gigaset 307x"
  22. /* Module parameters */
  23. int gigaset_debuglevel = DEBUG_DEFAULT;
  24. EXPORT_SYMBOL_GPL(gigaset_debuglevel);
  25. module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
  26. MODULE_PARM_DESC(debug, "debug level");
  27. /* driver state flags */
  28. #define VALID_MINOR 0x01
  29. #define VALID_ID 0x02
  30. void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
  31. size_t len, const unsigned char *buf)
  32. {
  33. unsigned char outbuf[80];
  34. unsigned char c;
  35. size_t space = sizeof outbuf - 1;
  36. unsigned char *out = outbuf;
  37. size_t numin = len;
  38. while (numin--) {
  39. c = *buf++;
  40. if (c == '~' || c == '^' || c == '\\') {
  41. if (!space--)
  42. break;
  43. *out++ = '\\';
  44. }
  45. if (c & 0x80) {
  46. if (!space--)
  47. break;
  48. *out++ = '~';
  49. c ^= 0x80;
  50. }
  51. if (c < 0x20 || c == 0x7f) {
  52. if (!space--)
  53. break;
  54. *out++ = '^';
  55. c ^= 0x40;
  56. }
  57. if (!space--)
  58. break;
  59. *out++ = c;
  60. }
  61. *out = 0;
  62. gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
  63. }
  64. EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
  65. static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
  66. {
  67. int r;
  68. r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
  69. cs->control_state = flags;
  70. if (r < 0)
  71. return r;
  72. if (delay) {
  73. set_current_state(TASK_INTERRUPTIBLE);
  74. schedule_timeout(delay * HZ / 1000);
  75. }
  76. return 0;
  77. }
  78. int gigaset_enterconfigmode(struct cardstate *cs)
  79. {
  80. int i, r;
  81. cs->control_state = TIOCM_RTS; //FIXME
  82. r = setflags(cs, TIOCM_DTR, 200);
  83. if (r < 0)
  84. goto error;
  85. r = setflags(cs, 0, 200);
  86. if (r < 0)
  87. goto error;
  88. for (i = 0; i < 5; ++i) {
  89. r = setflags(cs, TIOCM_RTS, 100);
  90. if (r < 0)
  91. goto error;
  92. r = setflags(cs, 0, 100);
  93. if (r < 0)
  94. goto error;
  95. }
  96. r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
  97. if (r < 0)
  98. goto error;
  99. return 0;
  100. error:
  101. dev_err(cs->dev, "error %d on setuartbits\n", -r);
  102. cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
  103. cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
  104. return -1; //r
  105. }
  106. static int test_timeout(struct at_state_t *at_state)
  107. {
  108. if (!at_state->timer_expires)
  109. return 0;
  110. if (--at_state->timer_expires) {
  111. gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
  112. at_state, at_state->timer_expires);
  113. return 0;
  114. }
  115. if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
  116. at_state->timer_index, NULL)) {
  117. //FIXME what should we do?
  118. }
  119. return 1;
  120. }
  121. static void timer_tick(unsigned long data)
  122. {
  123. struct cardstate *cs = (struct cardstate *) data;
  124. unsigned long flags;
  125. unsigned channel;
  126. struct at_state_t *at_state;
  127. int timeout = 0;
  128. spin_lock_irqsave(&cs->lock, flags);
  129. for (channel = 0; channel < cs->channels; ++channel)
  130. if (test_timeout(&cs->bcs[channel].at_state))
  131. timeout = 1;
  132. if (test_timeout(&cs->at_state))
  133. timeout = 1;
  134. list_for_each_entry(at_state, &cs->temp_at_states, list)
  135. if (test_timeout(at_state))
  136. timeout = 1;
  137. if (cs->running) {
  138. mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
  139. if (timeout) {
  140. gig_dbg(DEBUG_CMD, "scheduling timeout");
  141. tasklet_schedule(&cs->event_tasklet);
  142. }
  143. }
  144. spin_unlock_irqrestore(&cs->lock, flags);
  145. }
  146. int gigaset_get_channel(struct bc_state *bcs)
  147. {
  148. unsigned long flags;
  149. spin_lock_irqsave(&bcs->cs->lock, flags);
  150. if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
  151. gig_dbg(DEBUG_ANY, "could not allocate channel %d",
  152. bcs->channel);
  153. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  154. return 0;
  155. }
  156. ++bcs->use_count;
  157. bcs->busy = 1;
  158. gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
  159. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  160. return 1;
  161. }
  162. void gigaset_free_channel(struct bc_state *bcs)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&bcs->cs->lock, flags);
  166. if (!bcs->busy) {
  167. gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
  168. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  169. return;
  170. }
  171. --bcs->use_count;
  172. bcs->busy = 0;
  173. module_put(bcs->cs->driver->owner);
  174. gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
  175. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  176. }
  177. int gigaset_get_channels(struct cardstate *cs)
  178. {
  179. unsigned long flags;
  180. int i;
  181. spin_lock_irqsave(&cs->lock, flags);
  182. for (i = 0; i < cs->channels; ++i)
  183. if (cs->bcs[i].use_count) {
  184. spin_unlock_irqrestore(&cs->lock, flags);
  185. gig_dbg(DEBUG_ANY, "could not allocate all channels");
  186. return 0;
  187. }
  188. for (i = 0; i < cs->channels; ++i)
  189. ++cs->bcs[i].use_count;
  190. spin_unlock_irqrestore(&cs->lock, flags);
  191. gig_dbg(DEBUG_ANY, "allocated all channels");
  192. return 1;
  193. }
  194. void gigaset_free_channels(struct cardstate *cs)
  195. {
  196. unsigned long flags;
  197. int i;
  198. gig_dbg(DEBUG_ANY, "unblocking all channels");
  199. spin_lock_irqsave(&cs->lock, flags);
  200. for (i = 0; i < cs->channels; ++i)
  201. --cs->bcs[i].use_count;
  202. spin_unlock_irqrestore(&cs->lock, flags);
  203. }
  204. void gigaset_block_channels(struct cardstate *cs)
  205. {
  206. unsigned long flags;
  207. int i;
  208. gig_dbg(DEBUG_ANY, "blocking all channels");
  209. spin_lock_irqsave(&cs->lock, flags);
  210. for (i = 0; i < cs->channels; ++i)
  211. ++cs->bcs[i].use_count;
  212. spin_unlock_irqrestore(&cs->lock, flags);
  213. }
  214. static void clear_events(struct cardstate *cs)
  215. {
  216. struct event_t *ev;
  217. unsigned head, tail;
  218. unsigned long flags;
  219. spin_lock_irqsave(&cs->ev_lock, flags);
  220. head = cs->ev_head;
  221. tail = cs->ev_tail;
  222. while (tail != head) {
  223. ev = cs->events + head;
  224. kfree(ev->ptr);
  225. head = (head + 1) % MAX_EVENTS;
  226. }
  227. cs->ev_head = tail;
  228. spin_unlock_irqrestore(&cs->ev_lock, flags);
  229. }
  230. struct event_t *gigaset_add_event(struct cardstate *cs,
  231. struct at_state_t *at_state, int type,
  232. void *ptr, int parameter, void *arg)
  233. {
  234. unsigned long flags;
  235. unsigned next, tail;
  236. struct event_t *event = NULL;
  237. spin_lock_irqsave(&cs->ev_lock, flags);
  238. tail = cs->ev_tail;
  239. next = (tail + 1) % MAX_EVENTS;
  240. if (unlikely(next == cs->ev_head))
  241. dev_err(cs->dev, "event queue full\n");
  242. else {
  243. event = cs->events + tail;
  244. event->type = type;
  245. event->at_state = at_state;
  246. event->cid = -1;
  247. event->ptr = ptr;
  248. event->arg = arg;
  249. event->parameter = parameter;
  250. cs->ev_tail = next;
  251. }
  252. spin_unlock_irqrestore(&cs->ev_lock, flags);
  253. return event;
  254. }
  255. EXPORT_SYMBOL_GPL(gigaset_add_event);
  256. static void free_strings(struct at_state_t *at_state)
  257. {
  258. int i;
  259. for (i = 0; i < STR_NUM; ++i) {
  260. kfree(at_state->str_var[i]);
  261. at_state->str_var[i] = NULL;
  262. }
  263. }
  264. static void clear_at_state(struct at_state_t *at_state)
  265. {
  266. free_strings(at_state);
  267. }
  268. static void dealloc_at_states(struct cardstate *cs)
  269. {
  270. struct at_state_t *cur, *next;
  271. list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
  272. list_del(&cur->list);
  273. free_strings(cur);
  274. kfree(cur);
  275. }
  276. }
  277. static void gigaset_freebcs(struct bc_state *bcs)
  278. {
  279. int i;
  280. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
  281. if (!bcs->cs->ops->freebcshw(bcs)) {
  282. gig_dbg(DEBUG_INIT, "failed");
  283. }
  284. gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
  285. clear_at_state(&bcs->at_state);
  286. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
  287. if (bcs->skb)
  288. dev_kfree_skb(bcs->skb);
  289. for (i = 0; i < AT_NUM; ++i) {
  290. kfree(bcs->commands[i]);
  291. bcs->commands[i] = NULL;
  292. }
  293. }
  294. static struct cardstate *alloc_cs(struct gigaset_driver *drv)
  295. {
  296. unsigned long flags;
  297. unsigned i;
  298. struct cardstate *cs;
  299. struct cardstate *ret = NULL;
  300. spin_lock_irqsave(&drv->lock, flags);
  301. if (drv->blocked)
  302. goto exit;
  303. for (i = 0; i < drv->minors; ++i) {
  304. cs = drv->cs + i;
  305. if (!(cs->flags & VALID_MINOR)) {
  306. cs->flags = VALID_MINOR;
  307. ret = cs;
  308. break;
  309. }
  310. }
  311. exit:
  312. spin_unlock_irqrestore(&drv->lock, flags);
  313. return ret;
  314. }
  315. static void free_cs(struct cardstate *cs)
  316. {
  317. cs->flags = 0;
  318. }
  319. static void make_valid(struct cardstate *cs, unsigned mask)
  320. {
  321. unsigned long flags;
  322. struct gigaset_driver *drv = cs->driver;
  323. spin_lock_irqsave(&drv->lock, flags);
  324. cs->flags |= mask;
  325. spin_unlock_irqrestore(&drv->lock, flags);
  326. }
  327. static void make_invalid(struct cardstate *cs, unsigned mask)
  328. {
  329. unsigned long flags;
  330. struct gigaset_driver *drv = cs->driver;
  331. spin_lock_irqsave(&drv->lock, flags);
  332. cs->flags &= ~mask;
  333. spin_unlock_irqrestore(&drv->lock, flags);
  334. }
  335. void gigaset_freecs(struct cardstate *cs)
  336. {
  337. int i;
  338. unsigned long flags;
  339. if (!cs)
  340. return;
  341. mutex_lock(&cs->mutex);
  342. if (!cs->bcs)
  343. goto f_cs;
  344. if (!cs->inbuf)
  345. goto f_bcs;
  346. spin_lock_irqsave(&cs->lock, flags);
  347. cs->running = 0;
  348. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  349. not rescheduled below */
  350. tasklet_kill(&cs->event_tasklet);
  351. del_timer_sync(&cs->timer);
  352. switch (cs->cs_init) {
  353. default:
  354. /* clear device sysfs */
  355. gigaset_free_dev_sysfs(cs);
  356. gigaset_if_free(cs);
  357. gig_dbg(DEBUG_INIT, "clearing hw");
  358. cs->ops->freecshw(cs);
  359. //FIXME cmdbuf
  360. /* fall through */
  361. case 2: /* error in initcshw */
  362. /* Deregister from LL */
  363. make_invalid(cs, VALID_ID);
  364. gig_dbg(DEBUG_INIT, "clearing iif");
  365. gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
  366. /* fall through */
  367. case 1: /* error when regestering to LL */
  368. gig_dbg(DEBUG_INIT, "clearing at_state");
  369. clear_at_state(&cs->at_state);
  370. dealloc_at_states(cs);
  371. /* fall through */
  372. case 0: /* error in one call to initbcs */
  373. for (i = 0; i < cs->channels; ++i) {
  374. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  375. gigaset_freebcs(cs->bcs + i);
  376. }
  377. clear_events(cs);
  378. gig_dbg(DEBUG_INIT, "freeing inbuf");
  379. kfree(cs->inbuf);
  380. }
  381. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  382. kfree(cs->bcs);
  383. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  384. mutex_unlock(&cs->mutex);
  385. free_cs(cs);
  386. }
  387. EXPORT_SYMBOL_GPL(gigaset_freecs);
  388. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  389. struct cardstate *cs, int cid)
  390. {
  391. int i;
  392. INIT_LIST_HEAD(&at_state->list);
  393. at_state->waiting = 0;
  394. at_state->getstring = 0;
  395. at_state->pending_commands = 0;
  396. at_state->timer_expires = 0;
  397. at_state->timer_active = 0;
  398. at_state->timer_index = 0;
  399. at_state->seq_index = 0;
  400. at_state->ConState = 0;
  401. for (i = 0; i < STR_NUM; ++i)
  402. at_state->str_var[i] = NULL;
  403. at_state->int_var[VAR_ZDLE] = 0;
  404. at_state->int_var[VAR_ZCTP] = -1;
  405. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  406. at_state->cs = cs;
  407. at_state->bcs = bcs;
  408. at_state->cid = cid;
  409. if (!cid)
  410. at_state->replystruct = cs->tabnocid;
  411. else
  412. at_state->replystruct = cs->tabcid;
  413. }
  414. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
  415. struct cardstate *cs, int inputstate)
  416. /* inbuf->read must be allocated before! */
  417. {
  418. inbuf->head = 0;
  419. inbuf->tail = 0;
  420. inbuf->cs = cs;
  421. inbuf->bcs = bcs; /*base driver: NULL*/
  422. inbuf->rcvbuf = NULL;
  423. inbuf->inputstate = inputstate;
  424. }
  425. /* append received bytes to inbuf */
  426. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  427. unsigned numbytes)
  428. {
  429. unsigned n, head, tail, bytesleft;
  430. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  431. if (!numbytes)
  432. return 0;
  433. bytesleft = numbytes;
  434. tail = inbuf->tail;
  435. head = inbuf->head;
  436. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  437. while (bytesleft) {
  438. if (head > tail)
  439. n = head - 1 - tail;
  440. else if (head == 0)
  441. n = (RBUFSIZE-1) - tail;
  442. else
  443. n = RBUFSIZE - tail;
  444. if (!n) {
  445. dev_err(inbuf->cs->dev,
  446. "buffer overflow (%u bytes lost)\n",
  447. bytesleft);
  448. break;
  449. }
  450. if (n > bytesleft)
  451. n = bytesleft;
  452. memcpy(inbuf->data + tail, src, n);
  453. bytesleft -= n;
  454. tail = (tail + n) % RBUFSIZE;
  455. src += n;
  456. }
  457. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  458. inbuf->tail = tail;
  459. return numbytes != bytesleft;
  460. }
  461. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  462. /* Initialize the b-channel structure */
  463. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  464. struct cardstate *cs, int channel)
  465. {
  466. int i;
  467. bcs->tx_skb = NULL; //FIXME -> hw part
  468. skb_queue_head_init(&bcs->squeue);
  469. bcs->corrupted = 0;
  470. bcs->trans_down = 0;
  471. bcs->trans_up = 0;
  472. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  473. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  474. bcs->rcvbytes = 0;
  475. #ifdef CONFIG_GIGASET_DEBUG
  476. bcs->emptycount = 0;
  477. #endif
  478. gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
  479. bcs->fcs = PPP_INITFCS;
  480. bcs->inputstate = 0;
  481. if (cs->ignoreframes) {
  482. bcs->inputstate |= INS_skip_frame;
  483. bcs->skb = NULL;
  484. } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
  485. skb_reserve(bcs->skb, HW_HDR_LEN);
  486. else {
  487. warn("could not allocate skb");
  488. bcs->inputstate |= INS_skip_frame;
  489. }
  490. bcs->channel = channel;
  491. bcs->cs = cs;
  492. bcs->chstate = 0;
  493. bcs->use_count = 1;
  494. bcs->busy = 0;
  495. bcs->ignore = cs->ignoreframes;
  496. for (i = 0; i < AT_NUM; ++i)
  497. bcs->commands[i] = NULL;
  498. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  499. if (cs->ops->initbcshw(bcs))
  500. return bcs;
  501. gig_dbg(DEBUG_INIT, " failed");
  502. gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
  503. if (bcs->skb)
  504. dev_kfree_skb(bcs->skb);
  505. return NULL;
  506. }
  507. /* gigaset_initcs
  508. * Allocate and initialize cardstate structure for Gigaset driver
  509. * Calls hardware dependent gigaset_initcshw() function
  510. * Calls B channel initialization function gigaset_initbcs() for each B channel
  511. * parameters:
  512. * drv hardware driver the device belongs to
  513. * channels number of B channels supported by device
  514. * onechannel !=0: B channel data and AT commands share one
  515. * communication channel
  516. * ==0: B channels have separate communication channels
  517. * ignoreframes number of frames to ignore after setting up B channel
  518. * cidmode !=0: start in CallID mode
  519. * modulename name of driver module (used for I4L registration)
  520. * return value:
  521. * pointer to cardstate structure
  522. */
  523. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  524. int onechannel, int ignoreframes,
  525. int cidmode, const char *modulename)
  526. {
  527. struct cardstate *cs = NULL;
  528. unsigned long flags;
  529. int i;
  530. gig_dbg(DEBUG_INIT, "allocating cs");
  531. if (!(cs = alloc_cs(drv))) {
  532. err("maximum number of devices exceeded");
  533. return NULL;
  534. }
  535. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  536. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  537. if (!cs->bcs) {
  538. err("out of memory");
  539. goto error;
  540. }
  541. gig_dbg(DEBUG_INIT, "allocating inbuf");
  542. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  543. if (!cs->inbuf) {
  544. err("out of memory");
  545. goto error;
  546. }
  547. cs->cs_init = 0;
  548. cs->channels = channels;
  549. cs->onechannel = onechannel;
  550. cs->ignoreframes = ignoreframes;
  551. INIT_LIST_HEAD(&cs->temp_at_states);
  552. cs->running = 0;
  553. init_timer(&cs->timer); /* clear next & prev */
  554. spin_lock_init(&cs->ev_lock);
  555. cs->ev_tail = 0;
  556. cs->ev_head = 0;
  557. tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
  558. (unsigned long) cs);
  559. cs->commands_pending = 0;
  560. cs->cur_at_seq = 0;
  561. cs->gotfwver = -1;
  562. cs->open_count = 0;
  563. cs->dev = NULL;
  564. cs->tty = NULL;
  565. cs->tty_dev = NULL;
  566. cs->cidmode = cidmode != 0;
  567. //if(onechannel) { //FIXME
  568. cs->tabnocid = gigaset_tab_nocid_m10x;
  569. cs->tabcid = gigaset_tab_cid_m10x;
  570. //} else {
  571. // cs->tabnocid = gigaset_tab_nocid;
  572. // cs->tabcid = gigaset_tab_cid;
  573. //}
  574. init_waitqueue_head(&cs->waitqueue);
  575. cs->waiting = 0;
  576. cs->mode = M_UNKNOWN;
  577. cs->mstate = MS_UNINITIALIZED;
  578. for (i = 0; i < channels; ++i) {
  579. gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
  580. if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
  581. err("could not allocate channel %d data", i);
  582. goto error;
  583. }
  584. }
  585. ++cs->cs_init;
  586. gig_dbg(DEBUG_INIT, "setting up at_state");
  587. spin_lock_init(&cs->lock);
  588. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  589. cs->dle = 0;
  590. cs->cbytes = 0;
  591. gig_dbg(DEBUG_INIT, "setting up inbuf");
  592. if (onechannel) { //FIXME distinction necessary?
  593. gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
  594. } else
  595. gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
  596. cs->connected = 0;
  597. cs->isdn_up = 0;
  598. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  599. cs->cmdbuf = cs->lastcmdbuf = NULL;
  600. spin_lock_init(&cs->cmdlock);
  601. cs->curlen = 0;
  602. cs->cmdbytes = 0;
  603. gig_dbg(DEBUG_INIT, "setting up iif");
  604. if (!gigaset_register_to_LL(cs, modulename)) {
  605. err("register_isdn failed");
  606. goto error;
  607. }
  608. make_valid(cs, VALID_ID);
  609. ++cs->cs_init;
  610. gig_dbg(DEBUG_INIT, "setting up hw");
  611. if (!cs->ops->initcshw(cs)) {
  612. err("could not allocate device specific data");
  613. goto error;
  614. }
  615. ++cs->cs_init;
  616. /* set up character device */
  617. gigaset_if_init(cs);
  618. /* set up device sysfs */
  619. gigaset_init_dev_sysfs(cs);
  620. spin_lock_irqsave(&cs->lock, flags);
  621. cs->running = 1;
  622. spin_unlock_irqrestore(&cs->lock, flags);
  623. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  624. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  625. /* FIXME: can jiffies increase too much until the timer is added?
  626. * Same problem(?) with mod_timer() in timer_tick(). */
  627. add_timer(&cs->timer);
  628. gig_dbg(DEBUG_INIT, "cs initialized");
  629. return cs;
  630. error:
  631. gig_dbg(DEBUG_INIT, "failed");
  632. gigaset_freecs(cs);
  633. return NULL;
  634. }
  635. EXPORT_SYMBOL_GPL(gigaset_initcs);
  636. /* ReInitialize the b-channel structure on hangup */
  637. void gigaset_bcs_reinit(struct bc_state *bcs)
  638. {
  639. struct sk_buff *skb;
  640. struct cardstate *cs = bcs->cs;
  641. unsigned long flags;
  642. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  643. dev_kfree_skb(skb);
  644. spin_lock_irqsave(&cs->lock, flags);
  645. clear_at_state(&bcs->at_state);
  646. bcs->at_state.ConState = 0;
  647. bcs->at_state.timer_active = 0;
  648. bcs->at_state.timer_expires = 0;
  649. bcs->at_state.cid = -1; /* No CID defined */
  650. spin_unlock_irqrestore(&cs->lock, flags);
  651. bcs->inputstate = 0;
  652. #ifdef CONFIG_GIGASET_DEBUG
  653. bcs->emptycount = 0;
  654. #endif
  655. bcs->fcs = PPP_INITFCS;
  656. bcs->chstate = 0;
  657. bcs->ignore = cs->ignoreframes;
  658. if (bcs->ignore)
  659. bcs->inputstate |= INS_skip_frame;
  660. cs->ops->reinitbcshw(bcs);
  661. }
  662. static void cleanup_cs(struct cardstate *cs)
  663. {
  664. struct cmdbuf_t *cb, *tcb;
  665. int i;
  666. unsigned long flags;
  667. spin_lock_irqsave(&cs->lock, flags);
  668. cs->mode = M_UNKNOWN;
  669. cs->mstate = MS_UNINITIALIZED;
  670. clear_at_state(&cs->at_state);
  671. dealloc_at_states(cs);
  672. free_strings(&cs->at_state);
  673. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  674. kfree(cs->inbuf->rcvbuf);
  675. cs->inbuf->rcvbuf = NULL;
  676. cs->inbuf->inputstate = INS_command;
  677. cs->inbuf->head = 0;
  678. cs->inbuf->tail = 0;
  679. cb = cs->cmdbuf;
  680. while (cb) {
  681. tcb = cb;
  682. cb = cb->next;
  683. kfree(tcb);
  684. }
  685. cs->cmdbuf = cs->lastcmdbuf = NULL;
  686. cs->curlen = 0;
  687. cs->cmdbytes = 0;
  688. cs->gotfwver = -1;
  689. cs->dle = 0;
  690. cs->cur_at_seq = 0;
  691. cs->commands_pending = 0;
  692. cs->cbytes = 0;
  693. spin_unlock_irqrestore(&cs->lock, flags);
  694. for (i = 0; i < cs->channels; ++i) {
  695. gigaset_freebcs(cs->bcs + i);
  696. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  697. break; //FIXME error handling
  698. }
  699. if (cs->waiting) {
  700. cs->cmd_result = -ENODEV;
  701. cs->waiting = 0;
  702. wake_up_interruptible(&cs->waitqueue);
  703. }
  704. }
  705. int gigaset_start(struct cardstate *cs)
  706. {
  707. unsigned long flags;
  708. if (mutex_lock_interruptible(&cs->mutex))
  709. return 0;
  710. spin_lock_irqsave(&cs->lock, flags);
  711. cs->connected = 1;
  712. spin_unlock_irqrestore(&cs->lock, flags);
  713. if (cs->mstate != MS_LOCKED) {
  714. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  715. cs->ops->baud_rate(cs, B115200);
  716. cs->ops->set_line_ctrl(cs, CS8);
  717. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  718. } else {
  719. //FIXME use some saved values?
  720. }
  721. cs->waiting = 1;
  722. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  723. cs->waiting = 0;
  724. //FIXME what should we do?
  725. goto error;
  726. }
  727. gig_dbg(DEBUG_CMD, "scheduling START");
  728. gigaset_schedule_event(cs);
  729. wait_event(cs->waitqueue, !cs->waiting);
  730. mutex_unlock(&cs->mutex);
  731. return 1;
  732. error:
  733. mutex_unlock(&cs->mutex);
  734. return 0;
  735. }
  736. EXPORT_SYMBOL_GPL(gigaset_start);
  737. /* gigaset_shutdown
  738. * check if a device is associated to the cardstate structure and stop it
  739. * return value: 0 if ok, -1 if no device was associated
  740. */
  741. int gigaset_shutdown(struct cardstate *cs)
  742. {
  743. mutex_lock(&cs->mutex);
  744. if (!(cs->flags & VALID_MINOR)) {
  745. mutex_unlock(&cs->mutex);
  746. return -1;
  747. }
  748. cs->waiting = 1;
  749. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
  750. //FIXME what should we do?
  751. goto exit;
  752. }
  753. gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
  754. gigaset_schedule_event(cs);
  755. wait_event(cs->waitqueue, !cs->waiting);
  756. cleanup_cs(cs);
  757. exit:
  758. mutex_unlock(&cs->mutex);
  759. return 0;
  760. }
  761. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  762. void gigaset_stop(struct cardstate *cs)
  763. {
  764. mutex_lock(&cs->mutex);
  765. cs->waiting = 1;
  766. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  767. //FIXME what should we do?
  768. goto exit;
  769. }
  770. gig_dbg(DEBUG_CMD, "scheduling STOP");
  771. gigaset_schedule_event(cs);
  772. wait_event(cs->waitqueue, !cs->waiting);
  773. cleanup_cs(cs);
  774. exit:
  775. mutex_unlock(&cs->mutex);
  776. }
  777. EXPORT_SYMBOL_GPL(gigaset_stop);
  778. static LIST_HEAD(drivers);
  779. static DEFINE_SPINLOCK(driver_lock);
  780. struct cardstate *gigaset_get_cs_by_id(int id)
  781. {
  782. unsigned long flags;
  783. struct cardstate *ret = NULL;
  784. struct cardstate *cs;
  785. struct gigaset_driver *drv;
  786. unsigned i;
  787. spin_lock_irqsave(&driver_lock, flags);
  788. list_for_each_entry(drv, &drivers, list) {
  789. spin_lock(&drv->lock);
  790. for (i = 0; i < drv->minors; ++i) {
  791. cs = drv->cs + i;
  792. if ((cs->flags & VALID_ID) && cs->myid == id) {
  793. ret = cs;
  794. break;
  795. }
  796. }
  797. spin_unlock(&drv->lock);
  798. if (ret)
  799. break;
  800. }
  801. spin_unlock_irqrestore(&driver_lock, flags);
  802. return ret;
  803. }
  804. void gigaset_debugdrivers(void)
  805. {
  806. unsigned long flags;
  807. static struct cardstate *cs;
  808. struct gigaset_driver *drv;
  809. unsigned i;
  810. spin_lock_irqsave(&driver_lock, flags);
  811. list_for_each_entry(drv, &drivers, list) {
  812. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  813. spin_lock(&drv->lock);
  814. for (i = 0; i < drv->minors; ++i) {
  815. gig_dbg(DEBUG_DRIVER, " index %u", i);
  816. cs = drv->cs + i;
  817. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  818. gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
  819. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  820. cs->minor_index);
  821. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  822. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  823. }
  824. spin_unlock(&drv->lock);
  825. }
  826. spin_unlock_irqrestore(&driver_lock, flags);
  827. }
  828. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  829. {
  830. unsigned long flags;
  831. struct cardstate *ret = NULL;
  832. struct gigaset_driver *drv;
  833. unsigned index;
  834. spin_lock_irqsave(&driver_lock, flags);
  835. list_for_each_entry(drv, &drivers, list) {
  836. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  837. continue;
  838. index = minor - drv->minor;
  839. spin_lock(&drv->lock);
  840. if (drv->cs[index].flags & VALID_MINOR)
  841. ret = drv->cs + index;
  842. spin_unlock(&drv->lock);
  843. if (ret)
  844. break;
  845. }
  846. spin_unlock_irqrestore(&driver_lock, flags);
  847. return ret;
  848. }
  849. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  850. {
  851. if (tty->index < 0 || tty->index >= tty->driver->num)
  852. return NULL;
  853. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  854. }
  855. void gigaset_freedriver(struct gigaset_driver *drv)
  856. {
  857. unsigned long flags;
  858. spin_lock_irqsave(&driver_lock, flags);
  859. list_del(&drv->list);
  860. spin_unlock_irqrestore(&driver_lock, flags);
  861. gigaset_if_freedriver(drv);
  862. kfree(drv->cs);
  863. kfree(drv);
  864. }
  865. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  866. /* gigaset_initdriver
  867. * Allocate and initialize gigaset_driver structure. Initialize interface.
  868. * parameters:
  869. * minor First minor number
  870. * minors Number of minors this driver can handle
  871. * procname Name of the driver
  872. * devname Name of the device files (prefix without minor number)
  873. * return value:
  874. * Pointer to the gigaset_driver structure on success, NULL on failure.
  875. */
  876. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  877. const char *procname,
  878. const char *devname,
  879. const struct gigaset_ops *ops,
  880. struct module *owner)
  881. {
  882. struct gigaset_driver *drv;
  883. unsigned long flags;
  884. unsigned i;
  885. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  886. if (!drv)
  887. return NULL;
  888. drv->have_tty = 0;
  889. drv->minor = minor;
  890. drv->minors = minors;
  891. spin_lock_init(&drv->lock);
  892. drv->blocked = 0;
  893. drv->ops = ops;
  894. drv->owner = owner;
  895. INIT_LIST_HEAD(&drv->list);
  896. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  897. if (!drv->cs)
  898. goto error;
  899. for (i = 0; i < minors; ++i) {
  900. drv->cs[i].flags = 0;
  901. drv->cs[i].driver = drv;
  902. drv->cs[i].ops = drv->ops;
  903. drv->cs[i].minor_index = i;
  904. mutex_init(&drv->cs[i].mutex);
  905. }
  906. gigaset_if_initdriver(drv, procname, devname);
  907. spin_lock_irqsave(&driver_lock, flags);
  908. list_add(&drv->list, &drivers);
  909. spin_unlock_irqrestore(&driver_lock, flags);
  910. return drv;
  911. error:
  912. kfree(drv->cs);
  913. kfree(drv);
  914. return NULL;
  915. }
  916. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  917. void gigaset_blockdriver(struct gigaset_driver *drv)
  918. {
  919. drv->blocked = 1;
  920. }
  921. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  922. static int __init gigaset_init_module(void)
  923. {
  924. /* in accordance with the principle of least astonishment,
  925. * setting the 'debug' parameter to 1 activates a sensible
  926. * set of default debug levels
  927. */
  928. if (gigaset_debuglevel == 1)
  929. gigaset_debuglevel = DEBUG_DEFAULT;
  930. info(DRIVER_AUTHOR);
  931. info(DRIVER_DESC);
  932. return 0;
  933. }
  934. static void __exit gigaset_exit_module(void)
  935. {
  936. }
  937. module_init(gigaset_init_module);
  938. module_exit(gigaset_exit_module);
  939. MODULE_AUTHOR(DRIVER_AUTHOR);
  940. MODULE_DESCRIPTION(DRIVER_DESC);
  941. MODULE_LICENSE("GPL");