common.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. #define ASSIGNED 0x04
  31. void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
  32. size_t len, const unsigned char *buf)
  33. {
  34. unsigned char outbuf[80];
  35. unsigned char c;
  36. size_t space = sizeof outbuf - 1;
  37. unsigned char *out = outbuf;
  38. size_t numin = len;
  39. while (numin--) {
  40. c = *buf++;
  41. if (c == '~' || c == '^' || c == '\\') {
  42. if (!space--)
  43. break;
  44. *out++ = '\\';
  45. }
  46. if (c & 0x80) {
  47. if (!space--)
  48. break;
  49. *out++ = '~';
  50. c ^= 0x80;
  51. }
  52. if (c < 0x20 || c == 0x7f) {
  53. if (!space--)
  54. break;
  55. *out++ = '^';
  56. c ^= 0x40;
  57. }
  58. if (!space--)
  59. break;
  60. *out++ = c;
  61. }
  62. *out = 0;
  63. gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
  64. }
  65. EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
  66. static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
  67. {
  68. int r;
  69. r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
  70. cs->control_state = flags;
  71. if (r < 0)
  72. return r;
  73. if (delay) {
  74. set_current_state(TASK_INTERRUPTIBLE);
  75. schedule_timeout(delay * HZ / 1000);
  76. }
  77. return 0;
  78. }
  79. int gigaset_enterconfigmode(struct cardstate *cs)
  80. {
  81. int i, r;
  82. cs->control_state = TIOCM_RTS; //FIXME
  83. r = setflags(cs, TIOCM_DTR, 200);
  84. if (r < 0)
  85. goto error;
  86. r = setflags(cs, 0, 200);
  87. if (r < 0)
  88. goto error;
  89. for (i = 0; i < 5; ++i) {
  90. r = setflags(cs, TIOCM_RTS, 100);
  91. if (r < 0)
  92. goto error;
  93. r = setflags(cs, 0, 100);
  94. if (r < 0)
  95. goto error;
  96. }
  97. r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
  98. if (r < 0)
  99. goto error;
  100. return 0;
  101. error:
  102. dev_err(cs->dev, "error %d on setuartbits\n", -r);
  103. cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
  104. cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
  105. return -1; //r
  106. }
  107. static int test_timeout(struct at_state_t *at_state)
  108. {
  109. if (!at_state->timer_expires)
  110. return 0;
  111. if (--at_state->timer_expires) {
  112. gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
  113. at_state, at_state->timer_expires);
  114. return 0;
  115. }
  116. if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
  117. at_state->timer_index, NULL)) {
  118. //FIXME what should we do?
  119. }
  120. return 1;
  121. }
  122. static void timer_tick(unsigned long data)
  123. {
  124. struct cardstate *cs = (struct cardstate *) data;
  125. unsigned long flags;
  126. unsigned channel;
  127. struct at_state_t *at_state;
  128. int timeout = 0;
  129. spin_lock_irqsave(&cs->lock, flags);
  130. for (channel = 0; channel < cs->channels; ++channel)
  131. if (test_timeout(&cs->bcs[channel].at_state))
  132. timeout = 1;
  133. if (test_timeout(&cs->at_state))
  134. timeout = 1;
  135. list_for_each_entry(at_state, &cs->temp_at_states, list)
  136. if (test_timeout(at_state))
  137. timeout = 1;
  138. if (cs->running) {
  139. mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
  140. if (timeout) {
  141. gig_dbg(DEBUG_CMD, "scheduling timeout");
  142. tasklet_schedule(&cs->event_tasklet);
  143. }
  144. }
  145. spin_unlock_irqrestore(&cs->lock, flags);
  146. }
  147. int gigaset_get_channel(struct bc_state *bcs)
  148. {
  149. unsigned long flags;
  150. spin_lock_irqsave(&bcs->cs->lock, flags);
  151. if (bcs->use_count) {
  152. gig_dbg(DEBUG_ANY, "could not allocate channel %d",
  153. bcs->channel);
  154. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  155. return 0;
  156. }
  157. ++bcs->use_count;
  158. bcs->busy = 1;
  159. gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
  160. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  161. return 1;
  162. }
  163. void gigaset_free_channel(struct bc_state *bcs)
  164. {
  165. unsigned long flags;
  166. spin_lock_irqsave(&bcs->cs->lock, flags);
  167. if (!bcs->busy) {
  168. gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
  169. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  170. return;
  171. }
  172. --bcs->use_count;
  173. bcs->busy = 0;
  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. err("event queue full");
  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. static struct cardstate *ret = NULL;
  299. spin_lock_irqsave(&drv->lock, flags);
  300. for (i = 0; i < drv->minors; ++i) {
  301. if (!(drv->flags[i] & VALID_MINOR)) {
  302. drv->flags[i] = VALID_MINOR;
  303. ret = drv->cs + i;
  304. }
  305. if (ret)
  306. break;
  307. }
  308. spin_unlock_irqrestore(&drv->lock, flags);
  309. return ret;
  310. }
  311. static void free_cs(struct cardstate *cs)
  312. {
  313. unsigned long flags;
  314. struct gigaset_driver *drv = cs->driver;
  315. spin_lock_irqsave(&drv->lock, flags);
  316. drv->flags[cs->minor_index] = 0;
  317. spin_unlock_irqrestore(&drv->lock, flags);
  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. drv->flags[cs->minor_index] |= 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. drv->flags[cs->minor_index] &= ~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. atomic_set(&inbuf->head, 0);
  419. atomic_set(&inbuf->tail, 0);
  420. inbuf->cs = cs;
  421. inbuf->bcs = bcs; /*base driver: NULL*/
  422. inbuf->rcvbuf = NULL; //FIXME
  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 = atomic_read(&inbuf->tail);
  435. head = atomic_read(&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)", bytesleft);
  447. break;
  448. }
  449. if (n > bytesleft)
  450. n = bytesleft;
  451. memcpy(inbuf->data + tail, src, n);
  452. bytesleft -= n;
  453. tail = (tail + n) % RBUFSIZE;
  454. src += n;
  455. }
  456. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  457. atomic_set(&inbuf->tail, tail);
  458. return numbytes != bytesleft;
  459. }
  460. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  461. /* Initialize the b-channel structure */
  462. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  463. struct cardstate *cs, int channel)
  464. {
  465. int i;
  466. bcs->tx_skb = NULL; //FIXME -> hw part
  467. skb_queue_head_init(&bcs->squeue);
  468. bcs->corrupted = 0;
  469. bcs->trans_down = 0;
  470. bcs->trans_up = 0;
  471. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  472. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  473. bcs->rcvbytes = 0;
  474. #ifdef CONFIG_GIGASET_DEBUG
  475. bcs->emptycount = 0;
  476. #endif
  477. gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
  478. bcs->fcs = PPP_INITFCS;
  479. bcs->inputstate = 0;
  480. if (cs->ignoreframes) {
  481. bcs->inputstate |= INS_skip_frame;
  482. bcs->skb = NULL;
  483. } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
  484. skb_reserve(bcs->skb, HW_HDR_LEN);
  485. else {
  486. warn("could not allocate skb\n");
  487. bcs->inputstate |= INS_skip_frame;
  488. }
  489. bcs->channel = channel;
  490. bcs->cs = cs;
  491. bcs->chstate = 0;
  492. bcs->use_count = 1;
  493. bcs->busy = 0;
  494. bcs->ignore = cs->ignoreframes;
  495. for (i = 0; i < AT_NUM; ++i)
  496. bcs->commands[i] = NULL;
  497. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  498. if (cs->ops->initbcshw(bcs))
  499. return bcs;
  500. gig_dbg(DEBUG_INIT, " failed");
  501. gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
  502. if (bcs->skb)
  503. dev_kfree_skb(bcs->skb);
  504. return NULL;
  505. }
  506. /* gigaset_initcs
  507. * Allocate and initialize cardstate structure for Gigaset driver
  508. * Calls hardware dependent gigaset_initcshw() function
  509. * Calls B channel initialization function gigaset_initbcs() for each B channel
  510. * parameters:
  511. * drv hardware driver the device belongs to
  512. * channels number of B channels supported by device
  513. * onechannel !=0: B channel data and AT commands share one
  514. * communication channel
  515. * ==0: B channels have separate communication channels
  516. * ignoreframes number of frames to ignore after setting up B channel
  517. * cidmode !=0: start in CallID mode
  518. * modulename name of driver module (used for I4L registration)
  519. * return value:
  520. * pointer to cardstate structure
  521. */
  522. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  523. int onechannel, int ignoreframes,
  524. int cidmode, const char *modulename)
  525. {
  526. struct cardstate *cs = NULL;
  527. unsigned long flags;
  528. int i;
  529. gig_dbg(DEBUG_INIT, "allocating cs");
  530. cs = alloc_cs(drv);
  531. if (!cs)
  532. goto error;
  533. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  534. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  535. if (!cs->bcs)
  536. goto error;
  537. gig_dbg(DEBUG_INIT, "allocating inbuf");
  538. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  539. if (!cs->inbuf)
  540. goto error;
  541. cs->cs_init = 0;
  542. cs->channels = channels;
  543. cs->onechannel = onechannel;
  544. cs->ignoreframes = ignoreframes;
  545. INIT_LIST_HEAD(&cs->temp_at_states);
  546. cs->running = 0;
  547. init_timer(&cs->timer); /* clear next & prev */
  548. spin_lock_init(&cs->ev_lock);
  549. cs->ev_tail = 0;
  550. cs->ev_head = 0;
  551. mutex_init(&cs->mutex);
  552. mutex_lock(&cs->mutex);
  553. tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
  554. (unsigned long) cs);
  555. atomic_set(&cs->commands_pending, 0);
  556. cs->cur_at_seq = 0;
  557. cs->gotfwver = -1;
  558. cs->open_count = 0;
  559. cs->dev = NULL;
  560. cs->tty = NULL;
  561. cs->tty_dev = NULL;
  562. cs->cidmode = cidmode != 0;
  563. //if(onechannel) { //FIXME
  564. cs->tabnocid = gigaset_tab_nocid_m10x;
  565. cs->tabcid = gigaset_tab_cid_m10x;
  566. //} else {
  567. // cs->tabnocid = gigaset_tab_nocid;
  568. // cs->tabcid = gigaset_tab_cid;
  569. //}
  570. init_waitqueue_head(&cs->waitqueue);
  571. cs->waiting = 0;
  572. atomic_set(&cs->mode, M_UNKNOWN);
  573. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  574. for (i = 0; i < channels; ++i) {
  575. gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
  576. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  577. goto error;
  578. }
  579. ++cs->cs_init;
  580. gig_dbg(DEBUG_INIT, "setting up at_state");
  581. spin_lock_init(&cs->lock);
  582. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  583. cs->dle = 0;
  584. cs->cbytes = 0;
  585. gig_dbg(DEBUG_INIT, "setting up inbuf");
  586. if (onechannel) { //FIXME distinction necessary?
  587. gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
  588. } else
  589. gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
  590. cs->connected = 0;
  591. cs->isdn_up = 0;
  592. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  593. cs->cmdbuf = cs->lastcmdbuf = NULL;
  594. spin_lock_init(&cs->cmdlock);
  595. cs->curlen = 0;
  596. cs->cmdbytes = 0;
  597. gig_dbg(DEBUG_INIT, "setting up iif");
  598. if (!gigaset_register_to_LL(cs, modulename)) {
  599. err("register_isdn failed");
  600. goto error;
  601. }
  602. make_valid(cs, VALID_ID);
  603. ++cs->cs_init;
  604. gig_dbg(DEBUG_INIT, "setting up hw");
  605. if (!cs->ops->initcshw(cs))
  606. goto error;
  607. ++cs->cs_init;
  608. gigaset_if_init(cs);
  609. /* set up device sysfs */
  610. gigaset_init_dev_sysfs(cs);
  611. spin_lock_irqsave(&cs->lock, flags);
  612. cs->running = 1;
  613. spin_unlock_irqrestore(&cs->lock, flags);
  614. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  615. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  616. /* FIXME: can jiffies increase too much until the timer is added?
  617. * Same problem(?) with mod_timer() in timer_tick(). */
  618. add_timer(&cs->timer);
  619. gig_dbg(DEBUG_INIT, "cs initialized");
  620. mutex_unlock(&cs->mutex);
  621. return cs;
  622. error: if (cs)
  623. mutex_unlock(&cs->mutex);
  624. gig_dbg(DEBUG_INIT, "failed");
  625. gigaset_freecs(cs);
  626. return NULL;
  627. }
  628. EXPORT_SYMBOL_GPL(gigaset_initcs);
  629. /* ReInitialize the b-channel structure on hangup */
  630. void gigaset_bcs_reinit(struct bc_state *bcs)
  631. {
  632. struct sk_buff *skb;
  633. struct cardstate *cs = bcs->cs;
  634. unsigned long flags;
  635. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  636. dev_kfree_skb(skb);
  637. spin_lock_irqsave(&cs->lock, flags);
  638. clear_at_state(&bcs->at_state);
  639. bcs->at_state.ConState = 0;
  640. bcs->at_state.timer_active = 0;
  641. bcs->at_state.timer_expires = 0;
  642. bcs->at_state.cid = -1; /* No CID defined */
  643. spin_unlock_irqrestore(&cs->lock, flags);
  644. bcs->inputstate = 0;
  645. #ifdef CONFIG_GIGASET_DEBUG
  646. bcs->emptycount = 0;
  647. #endif
  648. bcs->fcs = PPP_INITFCS;
  649. bcs->chstate = 0;
  650. bcs->ignore = cs->ignoreframes;
  651. if (bcs->ignore)
  652. bcs->inputstate |= INS_skip_frame;
  653. cs->ops->reinitbcshw(bcs);
  654. }
  655. static void cleanup_cs(struct cardstate *cs)
  656. {
  657. struct cmdbuf_t *cb, *tcb;
  658. int i;
  659. unsigned long flags;
  660. spin_lock_irqsave(&cs->lock, flags);
  661. atomic_set(&cs->mode, M_UNKNOWN);
  662. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  663. clear_at_state(&cs->at_state);
  664. dealloc_at_states(cs);
  665. free_strings(&cs->at_state);
  666. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  667. kfree(cs->inbuf->rcvbuf);
  668. cs->inbuf->rcvbuf = NULL;
  669. cs->inbuf->inputstate = INS_command;
  670. atomic_set(&cs->inbuf->head, 0);
  671. atomic_set(&cs->inbuf->tail, 0);
  672. cb = cs->cmdbuf;
  673. while (cb) {
  674. tcb = cb;
  675. cb = cb->next;
  676. kfree(tcb);
  677. }
  678. cs->cmdbuf = cs->lastcmdbuf = NULL;
  679. cs->curlen = 0;
  680. cs->cmdbytes = 0;
  681. cs->gotfwver = -1;
  682. cs->dle = 0;
  683. cs->cur_at_seq = 0;
  684. atomic_set(&cs->commands_pending, 0);
  685. cs->cbytes = 0;
  686. spin_unlock_irqrestore(&cs->lock, flags);
  687. for (i = 0; i < cs->channels; ++i) {
  688. gigaset_freebcs(cs->bcs + i);
  689. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  690. break; //FIXME error handling
  691. }
  692. if (cs->waiting) {
  693. cs->cmd_result = -ENODEV;
  694. cs->waiting = 0;
  695. wake_up_interruptible(&cs->waitqueue);
  696. }
  697. }
  698. int gigaset_start(struct cardstate *cs)
  699. {
  700. unsigned long flags;
  701. if (mutex_lock_interruptible(&cs->mutex))
  702. return 0;
  703. spin_lock_irqsave(&cs->lock, flags);
  704. cs->connected = 1;
  705. spin_unlock_irqrestore(&cs->lock, flags);
  706. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  707. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  708. cs->ops->baud_rate(cs, B115200);
  709. cs->ops->set_line_ctrl(cs, CS8);
  710. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  711. } else {
  712. //FIXME use some saved values?
  713. }
  714. cs->waiting = 1;
  715. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  716. cs->waiting = 0;
  717. //FIXME what should we do?
  718. goto error;
  719. }
  720. gig_dbg(DEBUG_CMD, "scheduling START");
  721. gigaset_schedule_event(cs);
  722. wait_event(cs->waitqueue, !cs->waiting);
  723. mutex_unlock(&cs->mutex);
  724. return 1;
  725. error:
  726. mutex_unlock(&cs->mutex);
  727. return 0;
  728. }
  729. EXPORT_SYMBOL_GPL(gigaset_start);
  730. void gigaset_shutdown(struct cardstate *cs)
  731. {
  732. mutex_lock(&cs->mutex);
  733. cs->waiting = 1;
  734. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
  735. //FIXME what should we do?
  736. goto exit;
  737. }
  738. gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
  739. gigaset_schedule_event(cs);
  740. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  741. warn("%s: aborted", __func__);
  742. //FIXME
  743. }
  744. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  745. //FIXME?
  746. //gigaset_baud_rate(cs, B115200);
  747. //gigaset_set_line_ctrl(cs, CS8);
  748. //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
  749. //cs->control_state = 0;
  750. } else {
  751. //FIXME use some saved values?
  752. }
  753. cleanup_cs(cs);
  754. exit:
  755. mutex_unlock(&cs->mutex);
  756. }
  757. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  758. void gigaset_stop(struct cardstate *cs)
  759. {
  760. mutex_lock(&cs->mutex);
  761. cs->waiting = 1;
  762. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  763. //FIXME what should we do?
  764. goto exit;
  765. }
  766. gig_dbg(DEBUG_CMD, "scheduling STOP");
  767. gigaset_schedule_event(cs);
  768. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  769. warn("%s: aborted", __func__);
  770. //FIXME
  771. }
  772. cleanup_cs(cs);
  773. exit:
  774. mutex_unlock(&cs->mutex);
  775. }
  776. EXPORT_SYMBOL_GPL(gigaset_stop);
  777. static LIST_HEAD(drivers);
  778. static DEFINE_SPINLOCK(driver_lock);
  779. struct cardstate *gigaset_get_cs_by_id(int id)
  780. {
  781. unsigned long flags;
  782. static struct cardstate *ret = NULL;
  783. static struct cardstate *cs;
  784. struct gigaset_driver *drv;
  785. unsigned i;
  786. spin_lock_irqsave(&driver_lock, flags);
  787. list_for_each_entry(drv, &drivers, list) {
  788. spin_lock(&drv->lock);
  789. for (i = 0; i < drv->minors; ++i) {
  790. if (drv->flags[i] & VALID_ID) {
  791. cs = drv->cs + i;
  792. if (cs->myid == id)
  793. ret = cs;
  794. }
  795. if (ret)
  796. break;
  797. }
  798. spin_unlock(&drv->lock);
  799. if (ret)
  800. break;
  801. }
  802. spin_unlock_irqrestore(&driver_lock, flags);
  803. return ret;
  804. }
  805. void gigaset_debugdrivers(void)
  806. {
  807. unsigned long flags;
  808. static struct cardstate *cs;
  809. struct gigaset_driver *drv;
  810. unsigned i;
  811. spin_lock_irqsave(&driver_lock, flags);
  812. list_for_each_entry(drv, &drivers, list) {
  813. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  814. spin_lock(&drv->lock);
  815. for (i = 0; i < drv->minors; ++i) {
  816. gig_dbg(DEBUG_DRIVER, " index %u", i);
  817. gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
  818. drv->flags[i]);
  819. cs = drv->cs + i;
  820. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  821. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  822. cs->minor_index);
  823. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  824. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  825. }
  826. spin_unlock(&drv->lock);
  827. }
  828. spin_unlock_irqrestore(&driver_lock, flags);
  829. }
  830. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  831. {
  832. unsigned long flags;
  833. static struct cardstate *ret = NULL;
  834. struct gigaset_driver *drv;
  835. unsigned index;
  836. spin_lock_irqsave(&driver_lock, flags);
  837. list_for_each_entry(drv, &drivers, list) {
  838. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  839. continue;
  840. index = minor - drv->minor;
  841. spin_lock(&drv->lock);
  842. if (drv->flags[index] & VALID_MINOR)
  843. ret = drv->cs + index;
  844. spin_unlock(&drv->lock);
  845. if (ret)
  846. break;
  847. }
  848. spin_unlock_irqrestore(&driver_lock, flags);
  849. return ret;
  850. }
  851. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  852. {
  853. if (tty->index < 0 || tty->index >= tty->driver->num)
  854. return NULL;
  855. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  856. }
  857. void gigaset_freedriver(struct gigaset_driver *drv)
  858. {
  859. unsigned long flags;
  860. spin_lock_irqsave(&driver_lock, flags);
  861. list_del(&drv->list);
  862. spin_unlock_irqrestore(&driver_lock, flags);
  863. gigaset_if_freedriver(drv);
  864. module_put(drv->owner);
  865. kfree(drv->cs);
  866. kfree(drv->flags);
  867. kfree(drv);
  868. }
  869. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  870. /* gigaset_initdriver
  871. * Allocate and initialize gigaset_driver structure. Initialize interface.
  872. * parameters:
  873. * minor First minor number
  874. * minors Number of minors this driver can handle
  875. * procname Name of the driver
  876. * devname Name of the device files (prefix without minor number)
  877. * return value:
  878. * Pointer to the gigaset_driver structure on success, NULL on failure.
  879. */
  880. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  881. const char *procname,
  882. const char *devname,
  883. const struct gigaset_ops *ops,
  884. struct module *owner)
  885. {
  886. struct gigaset_driver *drv;
  887. unsigned long flags;
  888. unsigned i;
  889. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  890. if (!drv)
  891. return NULL;
  892. if (!try_module_get(owner))
  893. goto out1;
  894. drv->cs = NULL;
  895. drv->have_tty = 0;
  896. drv->minor = minor;
  897. drv->minors = minors;
  898. spin_lock_init(&drv->lock);
  899. drv->blocked = 0;
  900. drv->ops = ops;
  901. drv->owner = owner;
  902. INIT_LIST_HEAD(&drv->list);
  903. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  904. if (!drv->cs)
  905. goto out2;
  906. drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
  907. if (!drv->flags)
  908. goto out3;
  909. for (i = 0; i < minors; ++i) {
  910. drv->flags[i] = 0;
  911. drv->cs[i].driver = drv;
  912. drv->cs[i].ops = drv->ops;
  913. drv->cs[i].minor_index = i;
  914. }
  915. gigaset_if_initdriver(drv, procname, devname);
  916. spin_lock_irqsave(&driver_lock, flags);
  917. list_add(&drv->list, &drivers);
  918. spin_unlock_irqrestore(&driver_lock, flags);
  919. return drv;
  920. out3:
  921. kfree(drv->cs);
  922. out2:
  923. module_put(owner);
  924. out1:
  925. kfree(drv);
  926. return NULL;
  927. }
  928. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  929. /* For drivers without fixed assignment device<->cardstate (usb) */
  930. struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
  931. {
  932. unsigned long flags;
  933. struct cardstate *cs = NULL;
  934. unsigned i;
  935. spin_lock_irqsave(&drv->lock, flags);
  936. if (drv->blocked)
  937. goto exit;
  938. for (i = 0; i < drv->minors; ++i) {
  939. if ((drv->flags[i] & VALID_MINOR) &&
  940. !(drv->flags[i] & ASSIGNED)) {
  941. drv->flags[i] |= ASSIGNED;
  942. cs = drv->cs + i;
  943. break;
  944. }
  945. }
  946. exit:
  947. spin_unlock_irqrestore(&drv->lock, flags);
  948. return cs;
  949. }
  950. EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
  951. void gigaset_unassign(struct cardstate *cs)
  952. {
  953. unsigned long flags;
  954. unsigned *minor_flags;
  955. struct gigaset_driver *drv;
  956. if (!cs)
  957. return;
  958. drv = cs->driver;
  959. spin_lock_irqsave(&drv->lock, flags);
  960. minor_flags = drv->flags + cs->minor_index;
  961. if (*minor_flags & VALID_MINOR)
  962. *minor_flags &= ~ASSIGNED;
  963. spin_unlock_irqrestore(&drv->lock, flags);
  964. }
  965. EXPORT_SYMBOL_GPL(gigaset_unassign);
  966. void gigaset_blockdriver(struct gigaset_driver *drv)
  967. {
  968. unsigned long flags;
  969. spin_lock_irqsave(&drv->lock, flags);
  970. drv->blocked = 1;
  971. spin_unlock_irqrestore(&drv->lock, flags);
  972. }
  973. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  974. static int __init gigaset_init_module(void)
  975. {
  976. /* in accordance with the principle of least astonishment,
  977. * setting the 'debug' parameter to 1 activates a sensible
  978. * set of default debug levels
  979. */
  980. if (gigaset_debuglevel == 1)
  981. gigaset_debuglevel = DEBUG_DEFAULT;
  982. info(DRIVER_AUTHOR);
  983. info(DRIVER_DESC);
  984. return 0;
  985. }
  986. static void __exit gigaset_exit_module(void)
  987. {
  988. }
  989. module_init(gigaset_init_module);
  990. module_exit(gigaset_exit_module);
  991. MODULE_AUTHOR(DRIVER_AUTHOR);
  992. MODULE_DESCRIPTION(DRIVER_DESC);
  993. MODULE_LICENSE("GPL");