common.c 28 KB

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