common.c 27 KB

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