common.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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. spin_lock_irqsave(&cs->lock, flags);
  400. cs->running = 0;
  401. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  402. not rescheduled below */
  403. tasklet_kill(&cs->event_tasklet);
  404. del_timer_sync(&cs->timer);
  405. switch (cs->cs_init) {
  406. default:
  407. /* clear B channel structures */
  408. for (i = 0; i < cs->channels; ++i) {
  409. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  410. gigaset_freebcs(cs->bcs + i);
  411. }
  412. /* clear device sysfs */
  413. gigaset_free_dev_sysfs(cs);
  414. gigaset_if_free(cs);
  415. gig_dbg(DEBUG_INIT, "clearing hw");
  416. cs->ops->freecshw(cs);
  417. /* fall through */
  418. case 2: /* error in initcshw */
  419. /* Deregister from LL */
  420. make_invalid(cs, VALID_ID);
  421. gigaset_isdn_unregdev(cs);
  422. /* fall through */
  423. case 1: /* error when registering to LL */
  424. gig_dbg(DEBUG_INIT, "clearing at_state");
  425. clear_at_state(&cs->at_state);
  426. dealloc_temp_at_states(cs);
  427. clear_events(cs);
  428. tty_port_destroy(&cs->port);
  429. /* fall through */
  430. case 0: /* error in basic setup */
  431. gig_dbg(DEBUG_INIT, "freeing inbuf");
  432. kfree(cs->inbuf);
  433. kfree(cs->bcs);
  434. }
  435. mutex_unlock(&cs->mutex);
  436. free_cs(cs);
  437. }
  438. EXPORT_SYMBOL_GPL(gigaset_freecs);
  439. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  440. struct cardstate *cs, int cid)
  441. {
  442. int i;
  443. INIT_LIST_HEAD(&at_state->list);
  444. at_state->waiting = 0;
  445. at_state->getstring = 0;
  446. at_state->pending_commands = 0;
  447. at_state->timer_expires = 0;
  448. at_state->timer_active = 0;
  449. at_state->timer_index = 0;
  450. at_state->seq_index = 0;
  451. at_state->ConState = 0;
  452. for (i = 0; i < STR_NUM; ++i)
  453. at_state->str_var[i] = NULL;
  454. at_state->int_var[VAR_ZDLE] = 0;
  455. at_state->int_var[VAR_ZCTP] = -1;
  456. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  457. at_state->cs = cs;
  458. at_state->bcs = bcs;
  459. at_state->cid = cid;
  460. if (!cid)
  461. at_state->replystruct = cs->tabnocid;
  462. else
  463. at_state->replystruct = cs->tabcid;
  464. }
  465. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
  466. /* inbuf->read must be allocated before! */
  467. {
  468. inbuf->head = 0;
  469. inbuf->tail = 0;
  470. inbuf->cs = cs;
  471. inbuf->inputstate = INS_command;
  472. }
  473. /**
  474. * gigaset_fill_inbuf() - append received data to input buffer
  475. * @inbuf: buffer structure.
  476. * @src: received data.
  477. * @numbytes: number of bytes received.
  478. *
  479. * Return value: !=0 if some data was appended
  480. */
  481. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  482. unsigned numbytes)
  483. {
  484. unsigned n, head, tail, bytesleft;
  485. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  486. if (!numbytes)
  487. return 0;
  488. bytesleft = numbytes;
  489. tail = inbuf->tail;
  490. head = inbuf->head;
  491. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  492. while (bytesleft) {
  493. if (head > tail)
  494. n = head - 1 - tail;
  495. else if (head == 0)
  496. n = (RBUFSIZE - 1) - tail;
  497. else
  498. n = RBUFSIZE - tail;
  499. if (!n) {
  500. dev_err(inbuf->cs->dev,
  501. "buffer overflow (%u bytes lost)\n",
  502. bytesleft);
  503. break;
  504. }
  505. if (n > bytesleft)
  506. n = bytesleft;
  507. memcpy(inbuf->data + tail, src, n);
  508. bytesleft -= n;
  509. tail = (tail + n) % RBUFSIZE;
  510. src += n;
  511. }
  512. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  513. inbuf->tail = tail;
  514. return numbytes != bytesleft;
  515. }
  516. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  517. /* Initialize the b-channel structure */
  518. static int gigaset_initbcs(struct bc_state *bcs, struct cardstate *cs,
  519. int channel)
  520. {
  521. int i;
  522. bcs->tx_skb = NULL;
  523. skb_queue_head_init(&bcs->squeue);
  524. bcs->corrupted = 0;
  525. bcs->trans_down = 0;
  526. bcs->trans_up = 0;
  527. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  528. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  529. #ifdef CONFIG_GIGASET_DEBUG
  530. bcs->emptycount = 0;
  531. #endif
  532. bcs->rx_bufsize = 0;
  533. bcs->rx_skb = NULL;
  534. bcs->rx_fcs = PPP_INITFCS;
  535. bcs->inputstate = 0;
  536. bcs->channel = channel;
  537. bcs->cs = cs;
  538. bcs->chstate = 0;
  539. bcs->use_count = 1;
  540. bcs->busy = 0;
  541. bcs->ignore = cs->ignoreframes;
  542. for (i = 0; i < AT_NUM; ++i)
  543. bcs->commands[i] = NULL;
  544. spin_lock_init(&bcs->aplock);
  545. bcs->ap = NULL;
  546. bcs->apconnstate = 0;
  547. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  548. return cs->ops->initbcshw(bcs);
  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;
  573. unsigned long flags;
  574. int i;
  575. gig_dbg(DEBUG_INIT, "allocating cs");
  576. cs = alloc_cs(drv);
  577. if (!cs) {
  578. pr_err("maximum number of devices exceeded\n");
  579. return NULL;
  580. }
  581. cs->cs_init = 0;
  582. cs->channels = channels;
  583. cs->onechannel = onechannel;
  584. cs->ignoreframes = ignoreframes;
  585. INIT_LIST_HEAD(&cs->temp_at_states);
  586. cs->running = 0;
  587. init_timer(&cs->timer); /* clear next & prev */
  588. spin_lock_init(&cs->ev_lock);
  589. cs->ev_tail = 0;
  590. cs->ev_head = 0;
  591. tasklet_init(&cs->event_tasklet, gigaset_handle_event,
  592. (unsigned long) cs);
  593. tty_port_init(&cs->port);
  594. cs->commands_pending = 0;
  595. cs->cur_at_seq = 0;
  596. cs->gotfwver = -1;
  597. cs->dev = NULL;
  598. cs->tty_dev = NULL;
  599. cs->cidmode = cidmode != 0;
  600. cs->tabnocid = gigaset_tab_nocid;
  601. cs->tabcid = gigaset_tab_cid;
  602. init_waitqueue_head(&cs->waitqueue);
  603. cs->waiting = 0;
  604. cs->mode = M_UNKNOWN;
  605. cs->mstate = MS_UNINITIALIZED;
  606. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  607. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  608. if (!cs->bcs || !cs->inbuf) {
  609. pr_err("out of memory\n");
  610. goto error;
  611. }
  612. ++cs->cs_init;
  613. gig_dbg(DEBUG_INIT, "setting up at_state");
  614. spin_lock_init(&cs->lock);
  615. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  616. cs->dle = 0;
  617. cs->cbytes = 0;
  618. gig_dbg(DEBUG_INIT, "setting up inbuf");
  619. gigaset_inbuf_init(cs->inbuf, cs);
  620. cs->connected = 0;
  621. cs->isdn_up = 0;
  622. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  623. cs->cmdbuf = cs->lastcmdbuf = NULL;
  624. spin_lock_init(&cs->cmdlock);
  625. cs->curlen = 0;
  626. cs->cmdbytes = 0;
  627. gig_dbg(DEBUG_INIT, "setting up iif");
  628. if (gigaset_isdn_regdev(cs, modulename) < 0) {
  629. pr_err("error registering ISDN device\n");
  630. goto error;
  631. }
  632. make_valid(cs, VALID_ID);
  633. ++cs->cs_init;
  634. gig_dbg(DEBUG_INIT, "setting up hw");
  635. if (cs->ops->initcshw(cs) < 0)
  636. goto error;
  637. ++cs->cs_init;
  638. /* set up character device */
  639. gigaset_if_init(cs);
  640. /* set up device sysfs */
  641. gigaset_init_dev_sysfs(cs);
  642. /* set up channel data structures */
  643. for (i = 0; i < channels; ++i) {
  644. gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
  645. if (gigaset_initbcs(cs->bcs + i, cs, i) < 0) {
  646. pr_err("could not allocate channel %d data\n", i);
  647. goto error;
  648. }
  649. }
  650. spin_lock_irqsave(&cs->lock, flags);
  651. cs->running = 1;
  652. spin_unlock_irqrestore(&cs->lock, flags);
  653. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  654. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  655. add_timer(&cs->timer);
  656. gig_dbg(DEBUG_INIT, "cs initialized");
  657. return cs;
  658. error:
  659. gig_dbg(DEBUG_INIT, "failed");
  660. gigaset_freecs(cs);
  661. return NULL;
  662. }
  663. EXPORT_SYMBOL_GPL(gigaset_initcs);
  664. /* ReInitialize the b-channel structure on hangup */
  665. void gigaset_bcs_reinit(struct bc_state *bcs)
  666. {
  667. struct sk_buff *skb;
  668. struct cardstate *cs = bcs->cs;
  669. unsigned long flags;
  670. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  671. dev_kfree_skb(skb);
  672. spin_lock_irqsave(&cs->lock, flags);
  673. clear_at_state(&bcs->at_state);
  674. bcs->at_state.ConState = 0;
  675. bcs->at_state.timer_active = 0;
  676. bcs->at_state.timer_expires = 0;
  677. bcs->at_state.cid = -1; /* No CID defined */
  678. spin_unlock_irqrestore(&cs->lock, flags);
  679. bcs->inputstate = 0;
  680. #ifdef CONFIG_GIGASET_DEBUG
  681. bcs->emptycount = 0;
  682. #endif
  683. bcs->rx_fcs = PPP_INITFCS;
  684. bcs->chstate = 0;
  685. bcs->ignore = cs->ignoreframes;
  686. dev_kfree_skb(bcs->rx_skb);
  687. bcs->rx_skb = NULL;
  688. cs->ops->reinitbcshw(bcs);
  689. }
  690. static void cleanup_cs(struct cardstate *cs)
  691. {
  692. struct cmdbuf_t *cb, *tcb;
  693. int i;
  694. unsigned long flags;
  695. spin_lock_irqsave(&cs->lock, flags);
  696. cs->mode = M_UNKNOWN;
  697. cs->mstate = MS_UNINITIALIZED;
  698. clear_at_state(&cs->at_state);
  699. dealloc_temp_at_states(cs);
  700. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  701. cs->inbuf->inputstate = INS_command;
  702. cs->inbuf->head = 0;
  703. cs->inbuf->tail = 0;
  704. cb = cs->cmdbuf;
  705. while (cb) {
  706. tcb = cb;
  707. cb = cb->next;
  708. kfree(tcb);
  709. }
  710. cs->cmdbuf = cs->lastcmdbuf = NULL;
  711. cs->curlen = 0;
  712. cs->cmdbytes = 0;
  713. cs->gotfwver = -1;
  714. cs->dle = 0;
  715. cs->cur_at_seq = 0;
  716. cs->commands_pending = 0;
  717. cs->cbytes = 0;
  718. spin_unlock_irqrestore(&cs->lock, flags);
  719. for (i = 0; i < cs->channels; ++i) {
  720. gigaset_freebcs(cs->bcs + i);
  721. if (gigaset_initbcs(cs->bcs + i, cs, i) < 0)
  722. pr_err("could not allocate channel %d data\n", i);
  723. }
  724. if (cs->waiting) {
  725. cs->cmd_result = -ENODEV;
  726. cs->waiting = 0;
  727. wake_up_interruptible(&cs->waitqueue);
  728. }
  729. }
  730. /**
  731. * gigaset_start() - start device operations
  732. * @cs: device descriptor structure.
  733. *
  734. * Prepares the device for use by setting up communication parameters,
  735. * scheduling an EV_START event to initiate device initialization, and
  736. * waiting for completion of the initialization.
  737. *
  738. * Return value:
  739. * 0 on success, error code < 0 on failure
  740. */
  741. int gigaset_start(struct cardstate *cs)
  742. {
  743. unsigned long flags;
  744. if (mutex_lock_interruptible(&cs->mutex))
  745. return -EBUSY;
  746. spin_lock_irqsave(&cs->lock, flags);
  747. cs->connected = 1;
  748. spin_unlock_irqrestore(&cs->lock, flags);
  749. if (cs->mstate != MS_LOCKED) {
  750. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS);
  751. cs->ops->baud_rate(cs, B115200);
  752. cs->ops->set_line_ctrl(cs, CS8);
  753. cs->control_state = TIOCM_DTR | TIOCM_RTS;
  754. }
  755. cs->waiting = 1;
  756. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  757. cs->waiting = 0;
  758. goto error;
  759. }
  760. gigaset_schedule_event(cs);
  761. wait_event(cs->waitqueue, !cs->waiting);
  762. mutex_unlock(&cs->mutex);
  763. return 0;
  764. error:
  765. mutex_unlock(&cs->mutex);
  766. return -ENOMEM;
  767. }
  768. EXPORT_SYMBOL_GPL(gigaset_start);
  769. /**
  770. * gigaset_shutdown() - shut down device operations
  771. * @cs: device descriptor structure.
  772. *
  773. * Deactivates the device by scheduling an EV_SHUTDOWN event and
  774. * waiting for completion of the shutdown.
  775. *
  776. * Return value:
  777. * 0 - success, -ENODEV - error (no device associated)
  778. */
  779. int gigaset_shutdown(struct cardstate *cs)
  780. {
  781. mutex_lock(&cs->mutex);
  782. if (!(cs->flags & VALID_MINOR)) {
  783. mutex_unlock(&cs->mutex);
  784. return -ENODEV;
  785. }
  786. cs->waiting = 1;
  787. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
  788. goto exit;
  789. gigaset_schedule_event(cs);
  790. wait_event(cs->waitqueue, !cs->waiting);
  791. cleanup_cs(cs);
  792. exit:
  793. mutex_unlock(&cs->mutex);
  794. return 0;
  795. }
  796. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  797. /**
  798. * gigaset_stop() - stop device operations
  799. * @cs: device descriptor structure.
  800. *
  801. * Stops operations on the device by scheduling an EV_STOP event and
  802. * waiting for completion of the shutdown.
  803. */
  804. void gigaset_stop(struct cardstate *cs)
  805. {
  806. mutex_lock(&cs->mutex);
  807. cs->waiting = 1;
  808. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
  809. goto exit;
  810. gigaset_schedule_event(cs);
  811. wait_event(cs->waitqueue, !cs->waiting);
  812. cleanup_cs(cs);
  813. exit:
  814. mutex_unlock(&cs->mutex);
  815. }
  816. EXPORT_SYMBOL_GPL(gigaset_stop);
  817. static LIST_HEAD(drivers);
  818. static DEFINE_SPINLOCK(driver_lock);
  819. struct cardstate *gigaset_get_cs_by_id(int id)
  820. {
  821. unsigned long flags;
  822. struct cardstate *ret = NULL;
  823. struct cardstate *cs;
  824. struct gigaset_driver *drv;
  825. unsigned i;
  826. spin_lock_irqsave(&driver_lock, flags);
  827. list_for_each_entry(drv, &drivers, list) {
  828. spin_lock(&drv->lock);
  829. for (i = 0; i < drv->minors; ++i) {
  830. cs = drv->cs + i;
  831. if ((cs->flags & VALID_ID) && cs->myid == id) {
  832. ret = cs;
  833. break;
  834. }
  835. }
  836. spin_unlock(&drv->lock);
  837. if (ret)
  838. break;
  839. }
  840. spin_unlock_irqrestore(&driver_lock, flags);
  841. return ret;
  842. }
  843. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  844. {
  845. unsigned long flags;
  846. struct cardstate *ret = NULL;
  847. struct gigaset_driver *drv;
  848. unsigned index;
  849. spin_lock_irqsave(&driver_lock, flags);
  850. list_for_each_entry(drv, &drivers, list) {
  851. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  852. continue;
  853. index = minor - drv->minor;
  854. spin_lock(&drv->lock);
  855. if (drv->cs[index].flags & VALID_MINOR)
  856. ret = drv->cs + index;
  857. spin_unlock(&drv->lock);
  858. if (ret)
  859. break;
  860. }
  861. spin_unlock_irqrestore(&driver_lock, flags);
  862. return ret;
  863. }
  864. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  865. {
  866. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  867. }
  868. /**
  869. * gigaset_freedriver() - free all associated ressources of a driver
  870. * @drv: driver descriptor structure.
  871. *
  872. * Unregisters the driver from the system and deallocates the driver
  873. * structure @drv and all structures referenced from it.
  874. * All devices should be shut down before calling this.
  875. */
  876. void gigaset_freedriver(struct gigaset_driver *drv)
  877. {
  878. unsigned long flags;
  879. spin_lock_irqsave(&driver_lock, flags);
  880. list_del(&drv->list);
  881. spin_unlock_irqrestore(&driver_lock, flags);
  882. gigaset_if_freedriver(drv);
  883. kfree(drv->cs);
  884. kfree(drv);
  885. }
  886. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  887. /**
  888. * gigaset_initdriver() - initialize driver structure
  889. * @minor: First minor number
  890. * @minors: Number of minors this driver can handle
  891. * @procname: Name of the driver
  892. * @devname: Name of the device files (prefix without minor number)
  893. *
  894. * Allocate and initialize gigaset_driver structure. Initialize interface.
  895. *
  896. * Return value:
  897. * Pointer to the gigaset_driver structure on success, NULL on failure.
  898. */
  899. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  900. const char *procname,
  901. const char *devname,
  902. const struct gigaset_ops *ops,
  903. struct module *owner)
  904. {
  905. struct gigaset_driver *drv;
  906. unsigned long flags;
  907. unsigned i;
  908. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  909. if (!drv)
  910. return NULL;
  911. drv->have_tty = 0;
  912. drv->minor = minor;
  913. drv->minors = minors;
  914. spin_lock_init(&drv->lock);
  915. drv->blocked = 0;
  916. drv->ops = ops;
  917. drv->owner = owner;
  918. INIT_LIST_HEAD(&drv->list);
  919. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  920. if (!drv->cs)
  921. goto error;
  922. for (i = 0; i < minors; ++i) {
  923. drv->cs[i].flags = 0;
  924. drv->cs[i].driver = drv;
  925. drv->cs[i].ops = drv->ops;
  926. drv->cs[i].minor_index = i;
  927. mutex_init(&drv->cs[i].mutex);
  928. }
  929. gigaset_if_initdriver(drv, procname, devname);
  930. spin_lock_irqsave(&driver_lock, flags);
  931. list_add(&drv->list, &drivers);
  932. spin_unlock_irqrestore(&driver_lock, flags);
  933. return drv;
  934. error:
  935. kfree(drv);
  936. return NULL;
  937. }
  938. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  939. /**
  940. * gigaset_blockdriver() - block driver
  941. * @drv: driver descriptor structure.
  942. *
  943. * Prevents the driver from attaching new devices, in preparation for
  944. * deregistration.
  945. */
  946. void gigaset_blockdriver(struct gigaset_driver *drv)
  947. {
  948. drv->blocked = 1;
  949. }
  950. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  951. static int __init gigaset_init_module(void)
  952. {
  953. /* in accordance with the principle of least astonishment,
  954. * setting the 'debug' parameter to 1 activates a sensible
  955. * set of default debug levels
  956. */
  957. if (gigaset_debuglevel == 1)
  958. gigaset_debuglevel = DEBUG_DEFAULT;
  959. pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
  960. gigaset_isdn_regdrv();
  961. return 0;
  962. }
  963. static void __exit gigaset_exit_module(void)
  964. {
  965. gigaset_isdn_unregdrv();
  966. }
  967. module_init(gigaset_init_module);
  968. module_exit(gigaset_exit_module);
  969. MODULE_AUTHOR(DRIVER_AUTHOR);
  970. MODULE_DESCRIPTION(DRIVER_DESC);
  971. MODULE_LICENSE("GPL");