common.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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. wait_event(cs->waitqueue, !cs->waiting);
  751. cleanup_cs(cs);
  752. exit:
  753. mutex_unlock(&cs->mutex);
  754. }
  755. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  756. void gigaset_stop(struct cardstate *cs)
  757. {
  758. mutex_lock(&cs->mutex);
  759. cs->waiting = 1;
  760. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  761. //FIXME what should we do?
  762. goto exit;
  763. }
  764. gig_dbg(DEBUG_CMD, "scheduling STOP");
  765. gigaset_schedule_event(cs);
  766. wait_event(cs->waitqueue, !cs->waiting);
  767. cleanup_cs(cs);
  768. exit:
  769. mutex_unlock(&cs->mutex);
  770. }
  771. EXPORT_SYMBOL_GPL(gigaset_stop);
  772. static LIST_HEAD(drivers);
  773. static DEFINE_SPINLOCK(driver_lock);
  774. struct cardstate *gigaset_get_cs_by_id(int id)
  775. {
  776. unsigned long flags;
  777. struct cardstate *ret = NULL;
  778. struct cardstate *cs;
  779. struct gigaset_driver *drv;
  780. unsigned i;
  781. spin_lock_irqsave(&driver_lock, flags);
  782. list_for_each_entry(drv, &drivers, list) {
  783. spin_lock(&drv->lock);
  784. for (i = 0; i < drv->minors; ++i) {
  785. if (drv->flags[i] & VALID_ID) {
  786. cs = drv->cs + i;
  787. if (cs->myid == id)
  788. ret = cs;
  789. }
  790. if (ret)
  791. break;
  792. }
  793. spin_unlock(&drv->lock);
  794. if (ret)
  795. break;
  796. }
  797. spin_unlock_irqrestore(&driver_lock, flags);
  798. return ret;
  799. }
  800. void gigaset_debugdrivers(void)
  801. {
  802. unsigned long flags;
  803. static struct cardstate *cs;
  804. struct gigaset_driver *drv;
  805. unsigned i;
  806. spin_lock_irqsave(&driver_lock, flags);
  807. list_for_each_entry(drv, &drivers, list) {
  808. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  809. spin_lock(&drv->lock);
  810. for (i = 0; i < drv->minors; ++i) {
  811. gig_dbg(DEBUG_DRIVER, " index %u", i);
  812. gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
  813. drv->flags[i]);
  814. cs = drv->cs + i;
  815. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  816. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  817. cs->minor_index);
  818. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  819. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  820. }
  821. spin_unlock(&drv->lock);
  822. }
  823. spin_unlock_irqrestore(&driver_lock, flags);
  824. }
  825. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  826. {
  827. unsigned long flags;
  828. struct cardstate *ret = NULL;
  829. struct gigaset_driver *drv;
  830. unsigned index;
  831. spin_lock_irqsave(&driver_lock, flags);
  832. list_for_each_entry(drv, &drivers, list) {
  833. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  834. continue;
  835. index = minor - drv->minor;
  836. spin_lock(&drv->lock);
  837. if (drv->flags[index] & VALID_MINOR)
  838. ret = drv->cs + index;
  839. spin_unlock(&drv->lock);
  840. if (ret)
  841. break;
  842. }
  843. spin_unlock_irqrestore(&driver_lock, flags);
  844. return ret;
  845. }
  846. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  847. {
  848. if (tty->index < 0 || tty->index >= tty->driver->num)
  849. return NULL;
  850. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  851. }
  852. void gigaset_freedriver(struct gigaset_driver *drv)
  853. {
  854. unsigned long flags;
  855. spin_lock_irqsave(&driver_lock, flags);
  856. list_del(&drv->list);
  857. spin_unlock_irqrestore(&driver_lock, flags);
  858. gigaset_if_freedriver(drv);
  859. kfree(drv->cs);
  860. kfree(drv->flags);
  861. kfree(drv);
  862. }
  863. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  864. /* gigaset_initdriver
  865. * Allocate and initialize gigaset_driver structure. Initialize interface.
  866. * parameters:
  867. * minor First minor number
  868. * minors Number of minors this driver can handle
  869. * procname Name of the driver
  870. * devname Name of the device files (prefix without minor number)
  871. * return value:
  872. * Pointer to the gigaset_driver structure on success, NULL on failure.
  873. */
  874. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  875. const char *procname,
  876. const char *devname,
  877. const struct gigaset_ops *ops,
  878. struct module *owner)
  879. {
  880. struct gigaset_driver *drv;
  881. unsigned long flags;
  882. unsigned i;
  883. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  884. if (!drv)
  885. return NULL;
  886. drv->have_tty = 0;
  887. drv->minor = minor;
  888. drv->minors = minors;
  889. spin_lock_init(&drv->lock);
  890. drv->blocked = 0;
  891. drv->ops = ops;
  892. drv->owner = owner;
  893. INIT_LIST_HEAD(&drv->list);
  894. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  895. if (!drv->cs)
  896. goto error;
  897. drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
  898. if (!drv->flags)
  899. goto error;
  900. for (i = 0; i < minors; ++i) {
  901. drv->flags[i] = 0;
  902. drv->cs[i].driver = drv;
  903. drv->cs[i].ops = drv->ops;
  904. drv->cs[i].minor_index = i;
  905. }
  906. gigaset_if_initdriver(drv, procname, devname);
  907. spin_lock_irqsave(&driver_lock, flags);
  908. list_add(&drv->list, &drivers);
  909. spin_unlock_irqrestore(&driver_lock, flags);
  910. return drv;
  911. error:
  912. kfree(drv->cs);
  913. kfree(drv);
  914. return NULL;
  915. }
  916. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  917. /* For drivers without fixed assignment device<->cardstate (usb) */
  918. struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
  919. {
  920. unsigned long flags;
  921. struct cardstate *cs = NULL;
  922. unsigned i;
  923. spin_lock_irqsave(&drv->lock, flags);
  924. if (drv->blocked)
  925. goto exit;
  926. for (i = 0; i < drv->minors; ++i) {
  927. if ((drv->flags[i] & VALID_MINOR) &&
  928. !(drv->flags[i] & ASSIGNED)) {
  929. drv->flags[i] |= ASSIGNED;
  930. cs = drv->cs + i;
  931. break;
  932. }
  933. }
  934. exit:
  935. spin_unlock_irqrestore(&drv->lock, flags);
  936. return cs;
  937. }
  938. EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
  939. void gigaset_unassign(struct cardstate *cs)
  940. {
  941. unsigned long flags;
  942. unsigned *minor_flags;
  943. struct gigaset_driver *drv;
  944. if (!cs)
  945. return;
  946. drv = cs->driver;
  947. spin_lock_irqsave(&drv->lock, flags);
  948. minor_flags = drv->flags + cs->minor_index;
  949. if (*minor_flags & VALID_MINOR)
  950. *minor_flags &= ~ASSIGNED;
  951. spin_unlock_irqrestore(&drv->lock, flags);
  952. }
  953. EXPORT_SYMBOL_GPL(gigaset_unassign);
  954. void gigaset_blockdriver(struct gigaset_driver *drv)
  955. {
  956. unsigned long flags;
  957. spin_lock_irqsave(&drv->lock, flags);
  958. drv->blocked = 1;
  959. spin_unlock_irqrestore(&drv->lock, flags);
  960. }
  961. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  962. static int __init gigaset_init_module(void)
  963. {
  964. /* in accordance with the principle of least astonishment,
  965. * setting the 'debug' parameter to 1 activates a sensible
  966. * set of default debug levels
  967. */
  968. if (gigaset_debuglevel == 1)
  969. gigaset_debuglevel = DEBUG_DEFAULT;
  970. info(DRIVER_AUTHOR);
  971. info(DRIVER_DESC);
  972. return 0;
  973. }
  974. static void __exit gigaset_exit_module(void)
  975. {
  976. }
  977. module_init(gigaset_init_module);
  978. module_exit(gigaset_exit_module);
  979. MODULE_AUTHOR(DRIVER_AUTHOR);
  980. MODULE_DESCRIPTION(DRIVER_DESC);
  981. MODULE_LICENSE("GPL");