common.c 26 KB

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