common.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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. 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. if (try_module_get(drv->owner)) {
  303. drv->flags[i] = VALID_MINOR;
  304. ret = drv->cs + i;
  305. }
  306. break;
  307. }
  308. }
  309. spin_unlock_irqrestore(&drv->lock, flags);
  310. return ret;
  311. }
  312. static void free_cs(struct cardstate *cs)
  313. {
  314. unsigned long flags;
  315. struct gigaset_driver *drv = cs->driver;
  316. spin_lock_irqsave(&drv->lock, flags);
  317. if (drv->flags[cs->minor_index] & VALID_MINOR)
  318. module_put(drv->owner);
  319. drv->flags[cs->minor_index] = 0;
  320. spin_unlock_irqrestore(&drv->lock, flags);
  321. }
  322. static void make_valid(struct cardstate *cs, unsigned mask)
  323. {
  324. unsigned long flags;
  325. struct gigaset_driver *drv = cs->driver;
  326. spin_lock_irqsave(&drv->lock, flags);
  327. drv->flags[cs->minor_index] |= mask;
  328. spin_unlock_irqrestore(&drv->lock, flags);
  329. }
  330. static void make_invalid(struct cardstate *cs, unsigned mask)
  331. {
  332. unsigned long flags;
  333. struct gigaset_driver *drv = cs->driver;
  334. spin_lock_irqsave(&drv->lock, flags);
  335. drv->flags[cs->minor_index] &= ~mask;
  336. spin_unlock_irqrestore(&drv->lock, flags);
  337. }
  338. void gigaset_freecs(struct cardstate *cs)
  339. {
  340. int i;
  341. unsigned long flags;
  342. if (!cs)
  343. return;
  344. mutex_lock(&cs->mutex);
  345. if (!cs->bcs)
  346. goto f_cs;
  347. if (!cs->inbuf)
  348. goto f_bcs;
  349. spin_lock_irqsave(&cs->lock, flags);
  350. cs->running = 0;
  351. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  352. not rescheduled below */
  353. tasklet_kill(&cs->event_tasklet);
  354. del_timer_sync(&cs->timer);
  355. switch (cs->cs_init) {
  356. default:
  357. /* clear device sysfs */
  358. gigaset_free_dev_sysfs(cs);
  359. gigaset_if_free(cs);
  360. gig_dbg(DEBUG_INIT, "clearing hw");
  361. cs->ops->freecshw(cs);
  362. //FIXME cmdbuf
  363. /* fall through */
  364. case 2: /* error in initcshw */
  365. /* Deregister from LL */
  366. make_invalid(cs, VALID_ID);
  367. gig_dbg(DEBUG_INIT, "clearing iif");
  368. gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
  369. /* fall through */
  370. case 1: /* error when regestering to LL */
  371. gig_dbg(DEBUG_INIT, "clearing at_state");
  372. clear_at_state(&cs->at_state);
  373. dealloc_at_states(cs);
  374. /* fall through */
  375. case 0: /* error in one call to initbcs */
  376. for (i = 0; i < cs->channels; ++i) {
  377. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  378. gigaset_freebcs(cs->bcs + i);
  379. }
  380. clear_events(cs);
  381. gig_dbg(DEBUG_INIT, "freeing inbuf");
  382. kfree(cs->inbuf);
  383. }
  384. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  385. kfree(cs->bcs);
  386. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  387. mutex_unlock(&cs->mutex);
  388. free_cs(cs);
  389. }
  390. EXPORT_SYMBOL_GPL(gigaset_freecs);
  391. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  392. struct cardstate *cs, int cid)
  393. {
  394. int i;
  395. INIT_LIST_HEAD(&at_state->list);
  396. at_state->waiting = 0;
  397. at_state->getstring = 0;
  398. at_state->pending_commands = 0;
  399. at_state->timer_expires = 0;
  400. at_state->timer_active = 0;
  401. at_state->timer_index = 0;
  402. at_state->seq_index = 0;
  403. at_state->ConState = 0;
  404. for (i = 0; i < STR_NUM; ++i)
  405. at_state->str_var[i] = NULL;
  406. at_state->int_var[VAR_ZDLE] = 0;
  407. at_state->int_var[VAR_ZCTP] = -1;
  408. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  409. at_state->cs = cs;
  410. at_state->bcs = bcs;
  411. at_state->cid = cid;
  412. if (!cid)
  413. at_state->replystruct = cs->tabnocid;
  414. else
  415. at_state->replystruct = cs->tabcid;
  416. }
  417. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
  418. struct cardstate *cs, int inputstate)
  419. /* inbuf->read must be allocated before! */
  420. {
  421. atomic_set(&inbuf->head, 0);
  422. atomic_set(&inbuf->tail, 0);
  423. inbuf->cs = cs;
  424. inbuf->bcs = bcs; /*base driver: NULL*/
  425. inbuf->rcvbuf = NULL; //FIXME
  426. inbuf->inputstate = inputstate;
  427. }
  428. /* append received bytes to inbuf */
  429. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  430. unsigned numbytes)
  431. {
  432. unsigned n, head, tail, bytesleft;
  433. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  434. if (!numbytes)
  435. return 0;
  436. bytesleft = numbytes;
  437. tail = atomic_read(&inbuf->tail);
  438. head = atomic_read(&inbuf->head);
  439. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  440. while (bytesleft) {
  441. if (head > tail)
  442. n = head - 1 - tail;
  443. else if (head == 0)
  444. n = (RBUFSIZE-1) - tail;
  445. else
  446. n = RBUFSIZE - tail;
  447. if (!n) {
  448. dev_err(inbuf->cs->dev,
  449. "buffer overflow (%u bytes lost)", bytesleft);
  450. break;
  451. }
  452. if (n > bytesleft)
  453. n = bytesleft;
  454. memcpy(inbuf->data + tail, src, n);
  455. bytesleft -= n;
  456. tail = (tail + n) % RBUFSIZE;
  457. src += n;
  458. }
  459. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  460. atomic_set(&inbuf->tail, tail);
  461. return numbytes != bytesleft;
  462. }
  463. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  464. /* Initialize the b-channel structure */
  465. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  466. struct cardstate *cs, int channel)
  467. {
  468. int i;
  469. bcs->tx_skb = NULL; //FIXME -> hw part
  470. skb_queue_head_init(&bcs->squeue);
  471. bcs->corrupted = 0;
  472. bcs->trans_down = 0;
  473. bcs->trans_up = 0;
  474. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  475. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  476. bcs->rcvbytes = 0;
  477. #ifdef CONFIG_GIGASET_DEBUG
  478. bcs->emptycount = 0;
  479. #endif
  480. gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
  481. bcs->fcs = PPP_INITFCS;
  482. bcs->inputstate = 0;
  483. if (cs->ignoreframes) {
  484. bcs->inputstate |= INS_skip_frame;
  485. bcs->skb = NULL;
  486. } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
  487. skb_reserve(bcs->skb, HW_HDR_LEN);
  488. else {
  489. warn("could not allocate skb");
  490. bcs->inputstate |= INS_skip_frame;
  491. }
  492. bcs->channel = channel;
  493. bcs->cs = cs;
  494. bcs->chstate = 0;
  495. bcs->use_count = 1;
  496. bcs->busy = 0;
  497. bcs->ignore = cs->ignoreframes;
  498. for (i = 0; i < AT_NUM; ++i)
  499. bcs->commands[i] = NULL;
  500. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  501. if (cs->ops->initbcshw(bcs))
  502. return bcs;
  503. gig_dbg(DEBUG_INIT, " failed");
  504. gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
  505. if (bcs->skb)
  506. dev_kfree_skb(bcs->skb);
  507. return NULL;
  508. }
  509. /* gigaset_initcs
  510. * Allocate and initialize cardstate structure for Gigaset driver
  511. * Calls hardware dependent gigaset_initcshw() function
  512. * Calls B channel initialization function gigaset_initbcs() for each B channel
  513. * parameters:
  514. * drv hardware driver the device belongs to
  515. * channels number of B channels supported by device
  516. * onechannel !=0: B channel data and AT commands share one
  517. * communication channel
  518. * ==0: B channels have separate communication channels
  519. * ignoreframes number of frames to ignore after setting up B channel
  520. * cidmode !=0: start in CallID mode
  521. * modulename name of driver module (used for I4L registration)
  522. * return value:
  523. * pointer to cardstate structure
  524. */
  525. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  526. int onechannel, int ignoreframes,
  527. int cidmode, const char *modulename)
  528. {
  529. struct cardstate *cs = NULL;
  530. unsigned long flags;
  531. int i;
  532. gig_dbg(DEBUG_INIT, "allocating cs");
  533. if (!(cs = alloc_cs(drv))) {
  534. err("maximum number of devices exceeded");
  535. return NULL;
  536. }
  537. mutex_init(&cs->mutex);
  538. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  539. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  540. if (!cs->bcs) {
  541. err("out of memory");
  542. goto error;
  543. }
  544. gig_dbg(DEBUG_INIT, "allocating inbuf");
  545. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  546. if (!cs->inbuf) {
  547. err("out of memory");
  548. goto error;
  549. }
  550. cs->cs_init = 0;
  551. cs->channels = channels;
  552. cs->onechannel = onechannel;
  553. cs->ignoreframes = ignoreframes;
  554. INIT_LIST_HEAD(&cs->temp_at_states);
  555. cs->running = 0;
  556. init_timer(&cs->timer); /* clear next & prev */
  557. spin_lock_init(&cs->ev_lock);
  558. cs->ev_tail = 0;
  559. cs->ev_head = 0;
  560. tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
  561. (unsigned long) cs);
  562. atomic_set(&cs->commands_pending, 0);
  563. cs->cur_at_seq = 0;
  564. cs->gotfwver = -1;
  565. cs->open_count = 0;
  566. cs->dev = NULL;
  567. cs->tty = NULL;
  568. cs->tty_dev = NULL;
  569. cs->cidmode = cidmode != 0;
  570. //if(onechannel) { //FIXME
  571. cs->tabnocid = gigaset_tab_nocid_m10x;
  572. cs->tabcid = gigaset_tab_cid_m10x;
  573. //} else {
  574. // cs->tabnocid = gigaset_tab_nocid;
  575. // cs->tabcid = gigaset_tab_cid;
  576. //}
  577. init_waitqueue_head(&cs->waitqueue);
  578. cs->waiting = 0;
  579. atomic_set(&cs->mode, M_UNKNOWN);
  580. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  581. for (i = 0; i < channels; ++i) {
  582. gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
  583. if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
  584. err("could not allocate channel %d data", i);
  585. goto error;
  586. }
  587. }
  588. ++cs->cs_init;
  589. gig_dbg(DEBUG_INIT, "setting up at_state");
  590. spin_lock_init(&cs->lock);
  591. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  592. cs->dle = 0;
  593. cs->cbytes = 0;
  594. gig_dbg(DEBUG_INIT, "setting up inbuf");
  595. if (onechannel) { //FIXME distinction necessary?
  596. gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
  597. } else
  598. gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
  599. cs->connected = 0;
  600. cs->isdn_up = 0;
  601. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  602. cs->cmdbuf = cs->lastcmdbuf = NULL;
  603. spin_lock_init(&cs->cmdlock);
  604. cs->curlen = 0;
  605. cs->cmdbytes = 0;
  606. gig_dbg(DEBUG_INIT, "setting up iif");
  607. if (!gigaset_register_to_LL(cs, modulename)) {
  608. err("register_isdn failed");
  609. goto error;
  610. }
  611. make_valid(cs, VALID_ID);
  612. ++cs->cs_init;
  613. gig_dbg(DEBUG_INIT, "setting up hw");
  614. if (!cs->ops->initcshw(cs)) {
  615. err("could not allocate device specific data");
  616. goto error;
  617. }
  618. ++cs->cs_init;
  619. /* set up character device */
  620. gigaset_if_init(cs);
  621. /* set up device sysfs */
  622. gigaset_init_dev_sysfs(cs);
  623. spin_lock_irqsave(&cs->lock, flags);
  624. cs->running = 1;
  625. spin_unlock_irqrestore(&cs->lock, flags);
  626. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  627. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  628. /* FIXME: can jiffies increase too much until the timer is added?
  629. * Same problem(?) with mod_timer() in timer_tick(). */
  630. add_timer(&cs->timer);
  631. gig_dbg(DEBUG_INIT, "cs initialized");
  632. return cs;
  633. error:
  634. gig_dbg(DEBUG_INIT, "failed");
  635. gigaset_freecs(cs);
  636. return NULL;
  637. }
  638. EXPORT_SYMBOL_GPL(gigaset_initcs);
  639. /* ReInitialize the b-channel structure on hangup */
  640. void gigaset_bcs_reinit(struct bc_state *bcs)
  641. {
  642. struct sk_buff *skb;
  643. struct cardstate *cs = bcs->cs;
  644. unsigned long flags;
  645. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  646. dev_kfree_skb(skb);
  647. spin_lock_irqsave(&cs->lock, flags);
  648. clear_at_state(&bcs->at_state);
  649. bcs->at_state.ConState = 0;
  650. bcs->at_state.timer_active = 0;
  651. bcs->at_state.timer_expires = 0;
  652. bcs->at_state.cid = -1; /* No CID defined */
  653. spin_unlock_irqrestore(&cs->lock, flags);
  654. bcs->inputstate = 0;
  655. #ifdef CONFIG_GIGASET_DEBUG
  656. bcs->emptycount = 0;
  657. #endif
  658. bcs->fcs = PPP_INITFCS;
  659. bcs->chstate = 0;
  660. bcs->ignore = cs->ignoreframes;
  661. if (bcs->ignore)
  662. bcs->inputstate |= INS_skip_frame;
  663. cs->ops->reinitbcshw(bcs);
  664. }
  665. static void cleanup_cs(struct cardstate *cs)
  666. {
  667. struct cmdbuf_t *cb, *tcb;
  668. int i;
  669. unsigned long flags;
  670. spin_lock_irqsave(&cs->lock, flags);
  671. atomic_set(&cs->mode, M_UNKNOWN);
  672. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  673. clear_at_state(&cs->at_state);
  674. dealloc_at_states(cs);
  675. free_strings(&cs->at_state);
  676. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  677. kfree(cs->inbuf->rcvbuf);
  678. cs->inbuf->rcvbuf = NULL;
  679. cs->inbuf->inputstate = INS_command;
  680. atomic_set(&cs->inbuf->head, 0);
  681. atomic_set(&cs->inbuf->tail, 0);
  682. cb = cs->cmdbuf;
  683. while (cb) {
  684. tcb = cb;
  685. cb = cb->next;
  686. kfree(tcb);
  687. }
  688. cs->cmdbuf = cs->lastcmdbuf = NULL;
  689. cs->curlen = 0;
  690. cs->cmdbytes = 0;
  691. cs->gotfwver = -1;
  692. cs->dle = 0;
  693. cs->cur_at_seq = 0;
  694. atomic_set(&cs->commands_pending, 0);
  695. cs->cbytes = 0;
  696. spin_unlock_irqrestore(&cs->lock, flags);
  697. for (i = 0; i < cs->channels; ++i) {
  698. gigaset_freebcs(cs->bcs + i);
  699. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  700. break; //FIXME error handling
  701. }
  702. if (cs->waiting) {
  703. cs->cmd_result = -ENODEV;
  704. cs->waiting = 0;
  705. wake_up_interruptible(&cs->waitqueue);
  706. }
  707. }
  708. int gigaset_start(struct cardstate *cs)
  709. {
  710. unsigned long flags;
  711. if (mutex_lock_interruptible(&cs->mutex))
  712. return 0;
  713. spin_lock_irqsave(&cs->lock, flags);
  714. cs->connected = 1;
  715. spin_unlock_irqrestore(&cs->lock, flags);
  716. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  717. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  718. cs->ops->baud_rate(cs, B115200);
  719. cs->ops->set_line_ctrl(cs, CS8);
  720. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  721. } else {
  722. //FIXME use some saved values?
  723. }
  724. cs->waiting = 1;
  725. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  726. cs->waiting = 0;
  727. //FIXME what should we do?
  728. goto error;
  729. }
  730. gig_dbg(DEBUG_CMD, "scheduling START");
  731. gigaset_schedule_event(cs);
  732. wait_event(cs->waitqueue, !cs->waiting);
  733. mutex_unlock(&cs->mutex);
  734. return 1;
  735. error:
  736. mutex_unlock(&cs->mutex);
  737. return 0;
  738. }
  739. EXPORT_SYMBOL_GPL(gigaset_start);
  740. void gigaset_shutdown(struct cardstate *cs)
  741. {
  742. mutex_lock(&cs->mutex);
  743. cs->waiting = 1;
  744. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
  745. //FIXME what should we do?
  746. goto exit;
  747. }
  748. gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
  749. gigaset_schedule_event(cs);
  750. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  751. warn("%s: aborted", __func__);
  752. //FIXME
  753. }
  754. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  755. //FIXME?
  756. //gigaset_baud_rate(cs, B115200);
  757. //gigaset_set_line_ctrl(cs, CS8);
  758. //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
  759. //cs->control_state = 0;
  760. } else {
  761. //FIXME use some saved values?
  762. }
  763. cleanup_cs(cs);
  764. exit:
  765. mutex_unlock(&cs->mutex);
  766. }
  767. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  768. void gigaset_stop(struct cardstate *cs)
  769. {
  770. mutex_lock(&cs->mutex);
  771. cs->waiting = 1;
  772. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  773. //FIXME what should we do?
  774. goto exit;
  775. }
  776. gig_dbg(DEBUG_CMD, "scheduling STOP");
  777. gigaset_schedule_event(cs);
  778. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  779. warn("%s: aborted", __func__);
  780. //FIXME
  781. }
  782. cleanup_cs(cs);
  783. exit:
  784. mutex_unlock(&cs->mutex);
  785. }
  786. EXPORT_SYMBOL_GPL(gigaset_stop);
  787. static LIST_HEAD(drivers);
  788. static DEFINE_SPINLOCK(driver_lock);
  789. struct cardstate *gigaset_get_cs_by_id(int id)
  790. {
  791. unsigned long flags;
  792. static struct cardstate *ret = NULL;
  793. static struct cardstate *cs;
  794. struct gigaset_driver *drv;
  795. unsigned i;
  796. spin_lock_irqsave(&driver_lock, flags);
  797. list_for_each_entry(drv, &drivers, list) {
  798. spin_lock(&drv->lock);
  799. for (i = 0; i < drv->minors; ++i) {
  800. if (drv->flags[i] & VALID_ID) {
  801. cs = drv->cs + i;
  802. if (cs->myid == id)
  803. ret = cs;
  804. }
  805. if (ret)
  806. break;
  807. }
  808. spin_unlock(&drv->lock);
  809. if (ret)
  810. break;
  811. }
  812. spin_unlock_irqrestore(&driver_lock, flags);
  813. return ret;
  814. }
  815. void gigaset_debugdrivers(void)
  816. {
  817. unsigned long flags;
  818. static struct cardstate *cs;
  819. struct gigaset_driver *drv;
  820. unsigned i;
  821. spin_lock_irqsave(&driver_lock, flags);
  822. list_for_each_entry(drv, &drivers, list) {
  823. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  824. spin_lock(&drv->lock);
  825. for (i = 0; i < drv->minors; ++i) {
  826. gig_dbg(DEBUG_DRIVER, " index %u", i);
  827. gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
  828. drv->flags[i]);
  829. cs = drv->cs + i;
  830. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  831. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  832. cs->minor_index);
  833. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  834. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  835. }
  836. spin_unlock(&drv->lock);
  837. }
  838. spin_unlock_irqrestore(&driver_lock, flags);
  839. }
  840. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  841. {
  842. unsigned long flags;
  843. static struct cardstate *ret = NULL;
  844. struct gigaset_driver *drv;
  845. unsigned index;
  846. spin_lock_irqsave(&driver_lock, flags);
  847. list_for_each_entry(drv, &drivers, list) {
  848. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  849. continue;
  850. index = minor - drv->minor;
  851. spin_lock(&drv->lock);
  852. if (drv->flags[index] & VALID_MINOR)
  853. ret = drv->cs + index;
  854. spin_unlock(&drv->lock);
  855. if (ret)
  856. break;
  857. }
  858. spin_unlock_irqrestore(&driver_lock, flags);
  859. return ret;
  860. }
  861. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  862. {
  863. if (tty->index < 0 || tty->index >= tty->driver->num)
  864. return NULL;
  865. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  866. }
  867. void gigaset_freedriver(struct gigaset_driver *drv)
  868. {
  869. unsigned long flags;
  870. spin_lock_irqsave(&driver_lock, flags);
  871. list_del(&drv->list);
  872. spin_unlock_irqrestore(&driver_lock, flags);
  873. gigaset_if_freedriver(drv);
  874. kfree(drv->cs);
  875. kfree(drv->flags);
  876. kfree(drv);
  877. }
  878. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  879. /* gigaset_initdriver
  880. * Allocate and initialize gigaset_driver structure. Initialize interface.
  881. * parameters:
  882. * minor First minor number
  883. * minors Number of minors this driver can handle
  884. * procname Name of the driver
  885. * devname Name of the device files (prefix without minor number)
  886. * return value:
  887. * Pointer to the gigaset_driver structure on success, NULL on failure.
  888. */
  889. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  890. const char *procname,
  891. const char *devname,
  892. const struct gigaset_ops *ops,
  893. struct module *owner)
  894. {
  895. struct gigaset_driver *drv;
  896. unsigned long flags;
  897. unsigned i;
  898. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  899. if (!drv)
  900. return NULL;
  901. drv->have_tty = 0;
  902. drv->minor = minor;
  903. drv->minors = minors;
  904. spin_lock_init(&drv->lock);
  905. drv->blocked = 0;
  906. drv->ops = ops;
  907. drv->owner = owner;
  908. INIT_LIST_HEAD(&drv->list);
  909. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  910. if (!drv->cs)
  911. goto error;
  912. drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
  913. if (!drv->flags)
  914. goto error;
  915. for (i = 0; i < minors; ++i) {
  916. drv->flags[i] = 0;
  917. drv->cs[i].driver = drv;
  918. drv->cs[i].ops = drv->ops;
  919. drv->cs[i].minor_index = i;
  920. }
  921. gigaset_if_initdriver(drv, procname, devname);
  922. spin_lock_irqsave(&driver_lock, flags);
  923. list_add(&drv->list, &drivers);
  924. spin_unlock_irqrestore(&driver_lock, flags);
  925. return drv;
  926. error:
  927. kfree(drv->cs);
  928. kfree(drv);
  929. return NULL;
  930. }
  931. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  932. /* For drivers without fixed assignment device<->cardstate (usb) */
  933. struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
  934. {
  935. unsigned long flags;
  936. struct cardstate *cs = NULL;
  937. unsigned i;
  938. spin_lock_irqsave(&drv->lock, flags);
  939. if (drv->blocked)
  940. goto exit;
  941. for (i = 0; i < drv->minors; ++i) {
  942. if ((drv->flags[i] & VALID_MINOR) &&
  943. !(drv->flags[i] & ASSIGNED)) {
  944. drv->flags[i] |= ASSIGNED;
  945. cs = drv->cs + i;
  946. break;
  947. }
  948. }
  949. exit:
  950. spin_unlock_irqrestore(&drv->lock, flags);
  951. return cs;
  952. }
  953. EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
  954. void gigaset_unassign(struct cardstate *cs)
  955. {
  956. unsigned long flags;
  957. unsigned *minor_flags;
  958. struct gigaset_driver *drv;
  959. if (!cs)
  960. return;
  961. drv = cs->driver;
  962. spin_lock_irqsave(&drv->lock, flags);
  963. minor_flags = drv->flags + cs->minor_index;
  964. if (*minor_flags & VALID_MINOR)
  965. *minor_flags &= ~ASSIGNED;
  966. spin_unlock_irqrestore(&drv->lock, flags);
  967. }
  968. EXPORT_SYMBOL_GPL(gigaset_unassign);
  969. void gigaset_blockdriver(struct gigaset_driver *drv)
  970. {
  971. unsigned long flags;
  972. spin_lock_irqsave(&drv->lock, flags);
  973. drv->blocked = 1;
  974. spin_unlock_irqrestore(&drv->lock, flags);
  975. }
  976. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  977. static int __init gigaset_init_module(void)
  978. {
  979. /* in accordance with the principle of least astonishment,
  980. * setting the 'debug' parameter to 1 activates a sensible
  981. * set of default debug levels
  982. */
  983. if (gigaset_debuglevel == 1)
  984. gigaset_debuglevel = DEBUG_DEFAULT;
  985. info(DRIVER_AUTHOR);
  986. info(DRIVER_DESC);
  987. return 0;
  988. }
  989. static void __exit gigaset_exit_module(void)
  990. {
  991. }
  992. module_init(gigaset_init_module);
  993. module_exit(gigaset_exit_module);
  994. MODULE_AUTHOR(DRIVER_AUTHOR);
  995. MODULE_DESCRIPTION(DRIVER_DESC);
  996. MODULE_LICENSE("GPL");