common.c 28 KB

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