common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. /*
  2. * Stuff used by all variants of the driver
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers,
  5. * Hansjoerg Lipp <hjlipp@web.de>,
  6. * Tilman Schmidt <tilman@imap.cc>.
  7. *
  8. * =====================================================================
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. * =====================================================================
  14. */
  15. #include "gigaset.h"
  16. #include <linux/ctype.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. /* Version Information */
  20. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
  21. #define DRIVER_DESC "Driver for Gigaset 307x"
  22. /* Module parameters */
  23. int gigaset_debuglevel = DEBUG_DEFAULT;
  24. EXPORT_SYMBOL_GPL(gigaset_debuglevel);
  25. module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
  26. MODULE_PARM_DESC(debug, "debug level");
  27. /* driver state flags */
  28. #define VALID_MINOR 0x01
  29. #define VALID_ID 0x02
  30. #define ASSIGNED 0x04
  31. /* bitwise byte inversion table */
  32. __u8 gigaset_invtab[256] = {
  33. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  34. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  35. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  36. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  37. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  38. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  39. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  40. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  41. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  42. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  43. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  44. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  45. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  46. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  47. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  48. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  49. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  50. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  51. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  52. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  53. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  54. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  55. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  56. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  57. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  58. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  59. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  60. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  61. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  62. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  63. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  64. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
  65. };
  66. EXPORT_SYMBOL_GPL(gigaset_invtab);
  67. void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
  68. size_t len, const unsigned char *buf)
  69. {
  70. unsigned char outbuf[80];
  71. unsigned char c;
  72. size_t space = sizeof outbuf - 1;
  73. unsigned char *out = outbuf;
  74. size_t numin = len;
  75. while (numin--) {
  76. c = *buf++;
  77. if (c == '~' || c == '^' || c == '\\') {
  78. if (!space--)
  79. break;
  80. *out++ = '\\';
  81. }
  82. if (c & 0x80) {
  83. if (!space--)
  84. break;
  85. *out++ = '~';
  86. c ^= 0x80;
  87. }
  88. if (c < 0x20 || c == 0x7f) {
  89. if (!space--)
  90. break;
  91. *out++ = '^';
  92. c ^= 0x40;
  93. }
  94. if (!space--)
  95. break;
  96. *out++ = c;
  97. }
  98. *out = 0;
  99. gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
  100. }
  101. EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
  102. static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
  103. {
  104. int r;
  105. r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
  106. cs->control_state = flags;
  107. if (r < 0)
  108. return r;
  109. if (delay) {
  110. set_current_state(TASK_INTERRUPTIBLE);
  111. schedule_timeout(delay * HZ / 1000);
  112. }
  113. return 0;
  114. }
  115. int gigaset_enterconfigmode(struct cardstate *cs)
  116. {
  117. int i, r;
  118. cs->control_state = TIOCM_RTS; //FIXME
  119. r = setflags(cs, TIOCM_DTR, 200);
  120. if (r < 0)
  121. goto error;
  122. r = setflags(cs, 0, 200);
  123. if (r < 0)
  124. goto error;
  125. for (i = 0; i < 5; ++i) {
  126. r = setflags(cs, TIOCM_RTS, 100);
  127. if (r < 0)
  128. goto error;
  129. r = setflags(cs, 0, 100);
  130. if (r < 0)
  131. goto error;
  132. }
  133. r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
  134. if (r < 0)
  135. goto error;
  136. return 0;
  137. error:
  138. dev_err(cs->dev, "error %d on setuartbits\n", -r);
  139. cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
  140. cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
  141. return -1; //r
  142. }
  143. static int test_timeout(struct at_state_t *at_state)
  144. {
  145. if (!at_state->timer_expires)
  146. return 0;
  147. if (--at_state->timer_expires) {
  148. gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
  149. at_state, at_state->timer_expires);
  150. return 0;
  151. }
  152. if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
  153. at_state->timer_index, NULL)) {
  154. //FIXME what should we do?
  155. }
  156. return 1;
  157. }
  158. static void timer_tick(unsigned long data)
  159. {
  160. struct cardstate *cs = (struct cardstate *) data;
  161. unsigned long flags;
  162. unsigned channel;
  163. struct at_state_t *at_state;
  164. int timeout = 0;
  165. spin_lock_irqsave(&cs->lock, flags);
  166. for (channel = 0; channel < cs->channels; ++channel)
  167. if (test_timeout(&cs->bcs[channel].at_state))
  168. timeout = 1;
  169. if (test_timeout(&cs->at_state))
  170. timeout = 1;
  171. list_for_each_entry(at_state, &cs->temp_at_states, list)
  172. if (test_timeout(at_state))
  173. timeout = 1;
  174. if (cs->running) {
  175. mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
  176. if (timeout) {
  177. gig_dbg(DEBUG_CMD, "scheduling timeout");
  178. tasklet_schedule(&cs->event_tasklet);
  179. }
  180. }
  181. spin_unlock_irqrestore(&cs->lock, flags);
  182. }
  183. int gigaset_get_channel(struct bc_state *bcs)
  184. {
  185. unsigned long flags;
  186. spin_lock_irqsave(&bcs->cs->lock, flags);
  187. if (bcs->use_count) {
  188. gig_dbg(DEBUG_ANY, "could not allocate channel %d",
  189. bcs->channel);
  190. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  191. return 0;
  192. }
  193. ++bcs->use_count;
  194. bcs->busy = 1;
  195. gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
  196. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  197. return 1;
  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_ANY, "could not free channel %d", bcs->channel);
  205. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  206. return;
  207. }
  208. --bcs->use_count;
  209. bcs->busy = 0;
  210. gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
  211. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  212. }
  213. int gigaset_get_channels(struct cardstate *cs)
  214. {
  215. unsigned long flags;
  216. int i;
  217. spin_lock_irqsave(&cs->lock, flags);
  218. for (i = 0; i < cs->channels; ++i)
  219. if (cs->bcs[i].use_count) {
  220. spin_unlock_irqrestore(&cs->lock, flags);
  221. gig_dbg(DEBUG_ANY, "could not allocate all channels");
  222. return 0;
  223. }
  224. for (i = 0; i < cs->channels; ++i)
  225. ++cs->bcs[i].use_count;
  226. spin_unlock_irqrestore(&cs->lock, flags);
  227. gig_dbg(DEBUG_ANY, "allocated all channels");
  228. return 1;
  229. }
  230. void gigaset_free_channels(struct cardstate *cs)
  231. {
  232. unsigned long flags;
  233. int i;
  234. gig_dbg(DEBUG_ANY, "unblocking all channels");
  235. spin_lock_irqsave(&cs->lock, flags);
  236. for (i = 0; i < cs->channels; ++i)
  237. --cs->bcs[i].use_count;
  238. spin_unlock_irqrestore(&cs->lock, flags);
  239. }
  240. void gigaset_block_channels(struct cardstate *cs)
  241. {
  242. unsigned long flags;
  243. int i;
  244. gig_dbg(DEBUG_ANY, "blocking all channels");
  245. spin_lock_irqsave(&cs->lock, flags);
  246. for (i = 0; i < cs->channels; ++i)
  247. ++cs->bcs[i].use_count;
  248. spin_unlock_irqrestore(&cs->lock, flags);
  249. }
  250. static void clear_events(struct cardstate *cs)
  251. {
  252. struct event_t *ev;
  253. unsigned head, tail;
  254. unsigned long flags;
  255. spin_lock_irqsave(&cs->ev_lock, flags);
  256. head = cs->ev_head;
  257. tail = cs->ev_tail;
  258. while (tail != head) {
  259. ev = cs->events + head;
  260. kfree(ev->ptr);
  261. head = (head + 1) % MAX_EVENTS;
  262. }
  263. cs->ev_head = tail;
  264. spin_unlock_irqrestore(&cs->ev_lock, flags);
  265. }
  266. struct event_t *gigaset_add_event(struct cardstate *cs,
  267. struct at_state_t *at_state, int type,
  268. void *ptr, int parameter, void *arg)
  269. {
  270. unsigned long flags;
  271. unsigned next, tail;
  272. struct event_t *event = NULL;
  273. spin_lock_irqsave(&cs->ev_lock, flags);
  274. tail = cs->ev_tail;
  275. next = (tail + 1) % MAX_EVENTS;
  276. if (unlikely(next == cs->ev_head))
  277. err("event queue full");
  278. else {
  279. event = cs->events + tail;
  280. event->type = type;
  281. event->at_state = at_state;
  282. event->cid = -1;
  283. event->ptr = ptr;
  284. event->arg = arg;
  285. event->parameter = parameter;
  286. cs->ev_tail = next;
  287. }
  288. spin_unlock_irqrestore(&cs->ev_lock, flags);
  289. return event;
  290. }
  291. EXPORT_SYMBOL_GPL(gigaset_add_event);
  292. static void free_strings(struct at_state_t *at_state)
  293. {
  294. int i;
  295. for (i = 0; i < STR_NUM; ++i) {
  296. kfree(at_state->str_var[i]);
  297. at_state->str_var[i] = NULL;
  298. }
  299. }
  300. static void clear_at_state(struct at_state_t *at_state)
  301. {
  302. free_strings(at_state);
  303. }
  304. static void dealloc_at_states(struct cardstate *cs)
  305. {
  306. struct at_state_t *cur, *next;
  307. list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
  308. list_del(&cur->list);
  309. free_strings(cur);
  310. kfree(cur);
  311. }
  312. }
  313. static void gigaset_freebcs(struct bc_state *bcs)
  314. {
  315. int i;
  316. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
  317. if (!bcs->cs->ops->freebcshw(bcs)) {
  318. gig_dbg(DEBUG_INIT, "failed");
  319. }
  320. gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
  321. clear_at_state(&bcs->at_state);
  322. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
  323. if (bcs->skb)
  324. dev_kfree_skb(bcs->skb);
  325. for (i = 0; i < AT_NUM; ++i) {
  326. kfree(bcs->commands[i]);
  327. bcs->commands[i] = NULL;
  328. }
  329. }
  330. static struct cardstate *alloc_cs(struct gigaset_driver *drv)
  331. {
  332. unsigned long flags;
  333. unsigned i;
  334. static struct cardstate *ret = NULL;
  335. spin_lock_irqsave(&drv->lock, flags);
  336. for (i = 0; i < drv->minors; ++i) {
  337. if (!(drv->flags[i] & VALID_MINOR)) {
  338. drv->flags[i] = VALID_MINOR;
  339. ret = drv->cs + i;
  340. }
  341. if (ret)
  342. break;
  343. }
  344. spin_unlock_irqrestore(&drv->lock, flags);
  345. return ret;
  346. }
  347. static void free_cs(struct cardstate *cs)
  348. {
  349. unsigned long flags;
  350. struct gigaset_driver *drv = cs->driver;
  351. spin_lock_irqsave(&drv->lock, flags);
  352. drv->flags[cs->minor_index] = 0;
  353. spin_unlock_irqrestore(&drv->lock, flags);
  354. }
  355. static void make_valid(struct cardstate *cs, unsigned mask)
  356. {
  357. unsigned long flags;
  358. struct gigaset_driver *drv = cs->driver;
  359. spin_lock_irqsave(&drv->lock, flags);
  360. drv->flags[cs->minor_index] |= mask;
  361. spin_unlock_irqrestore(&drv->lock, flags);
  362. }
  363. static void make_invalid(struct cardstate *cs, unsigned mask)
  364. {
  365. unsigned long flags;
  366. struct gigaset_driver *drv = cs->driver;
  367. spin_lock_irqsave(&drv->lock, flags);
  368. drv->flags[cs->minor_index] &= ~mask;
  369. spin_unlock_irqrestore(&drv->lock, flags);
  370. }
  371. void gigaset_freecs(struct cardstate *cs)
  372. {
  373. int i;
  374. unsigned long flags;
  375. if (!cs)
  376. return;
  377. mutex_lock(&cs->mutex);
  378. if (!cs->bcs)
  379. goto f_cs;
  380. if (!cs->inbuf)
  381. goto f_bcs;
  382. spin_lock_irqsave(&cs->lock, flags);
  383. cs->running = 0;
  384. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  385. not rescheduled below */
  386. tasklet_kill(&cs->event_tasklet);
  387. del_timer_sync(&cs->timer);
  388. switch (cs->cs_init) {
  389. default:
  390. gigaset_if_free(cs);
  391. gig_dbg(DEBUG_INIT, "clearing hw");
  392. cs->ops->freecshw(cs);
  393. //FIXME cmdbuf
  394. /* fall through */
  395. case 2: /* error in initcshw */
  396. /* Deregister from LL */
  397. make_invalid(cs, VALID_ID);
  398. gig_dbg(DEBUG_INIT, "clearing iif");
  399. gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
  400. /* fall through */
  401. case 1: /* error when regestering to LL */
  402. gig_dbg(DEBUG_INIT, "clearing at_state");
  403. clear_at_state(&cs->at_state);
  404. dealloc_at_states(cs);
  405. /* fall through */
  406. case 0: /* error in one call to initbcs */
  407. for (i = 0; i < cs->channels; ++i) {
  408. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  409. gigaset_freebcs(cs->bcs + i);
  410. }
  411. clear_events(cs);
  412. gig_dbg(DEBUG_INIT, "freeing inbuf");
  413. kfree(cs->inbuf);
  414. }
  415. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  416. kfree(cs->bcs);
  417. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  418. mutex_unlock(&cs->mutex);
  419. free_cs(cs);
  420. }
  421. EXPORT_SYMBOL_GPL(gigaset_freecs);
  422. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  423. struct cardstate *cs, int cid)
  424. {
  425. int i;
  426. INIT_LIST_HEAD(&at_state->list);
  427. at_state->waiting = 0;
  428. at_state->getstring = 0;
  429. at_state->pending_commands = 0;
  430. at_state->timer_expires = 0;
  431. at_state->timer_active = 0;
  432. at_state->timer_index = 0;
  433. at_state->seq_index = 0;
  434. at_state->ConState = 0;
  435. for (i = 0; i < STR_NUM; ++i)
  436. at_state->str_var[i] = NULL;
  437. at_state->int_var[VAR_ZDLE] = 0;
  438. at_state->int_var[VAR_ZCTP] = -1;
  439. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  440. at_state->cs = cs;
  441. at_state->bcs = bcs;
  442. at_state->cid = cid;
  443. if (!cid)
  444. at_state->replystruct = cs->tabnocid;
  445. else
  446. at_state->replystruct = cs->tabcid;
  447. }
  448. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
  449. struct cardstate *cs, int inputstate)
  450. /* inbuf->read must be allocated before! */
  451. {
  452. atomic_set(&inbuf->head, 0);
  453. atomic_set(&inbuf->tail, 0);
  454. inbuf->cs = cs;
  455. inbuf->bcs = bcs; /*base driver: NULL*/
  456. inbuf->rcvbuf = NULL; //FIXME
  457. inbuf->inputstate = inputstate;
  458. }
  459. /* append received bytes to inbuf */
  460. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  461. unsigned numbytes)
  462. {
  463. unsigned n, head, tail, bytesleft;
  464. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  465. if (!numbytes)
  466. return 0;
  467. bytesleft = numbytes;
  468. tail = atomic_read(&inbuf->tail);
  469. head = atomic_read(&inbuf->head);
  470. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  471. while (bytesleft) {
  472. if (head > tail)
  473. n = head - 1 - tail;
  474. else if (head == 0)
  475. n = (RBUFSIZE-1) - tail;
  476. else
  477. n = RBUFSIZE - tail;
  478. if (!n) {
  479. dev_err(inbuf->cs->dev,
  480. "buffer overflow (%u bytes lost)", bytesleft);
  481. break;
  482. }
  483. if (n > bytesleft)
  484. n = bytesleft;
  485. memcpy(inbuf->data + tail, src, n);
  486. bytesleft -= n;
  487. tail = (tail + n) % RBUFSIZE;
  488. src += n;
  489. }
  490. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  491. atomic_set(&inbuf->tail, tail);
  492. return numbytes != bytesleft;
  493. }
  494. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  495. /* Initialize the b-channel structure */
  496. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  497. struct cardstate *cs, int channel)
  498. {
  499. int i;
  500. bcs->tx_skb = NULL; //FIXME -> hw part
  501. skb_queue_head_init(&bcs->squeue);
  502. bcs->corrupted = 0;
  503. bcs->trans_down = 0;
  504. bcs->trans_up = 0;
  505. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  506. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  507. bcs->rcvbytes = 0;
  508. #ifdef CONFIG_GIGASET_DEBUG
  509. bcs->emptycount = 0;
  510. #endif
  511. gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
  512. bcs->fcs = PPP_INITFCS;
  513. bcs->inputstate = 0;
  514. if (cs->ignoreframes) {
  515. bcs->inputstate |= INS_skip_frame;
  516. bcs->skb = NULL;
  517. } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
  518. skb_reserve(bcs->skb, HW_HDR_LEN);
  519. else {
  520. dev_warn(cs->dev, "could not allocate skb\n");
  521. bcs->inputstate |= INS_skip_frame;
  522. }
  523. bcs->channel = channel;
  524. bcs->cs = cs;
  525. bcs->chstate = 0;
  526. bcs->use_count = 1;
  527. bcs->busy = 0;
  528. bcs->ignore = cs->ignoreframes;
  529. for (i = 0; i < AT_NUM; ++i)
  530. bcs->commands[i] = NULL;
  531. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  532. if (cs->ops->initbcshw(bcs))
  533. return bcs;
  534. gig_dbg(DEBUG_INIT, " failed");
  535. gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
  536. if (bcs->skb)
  537. dev_kfree_skb(bcs->skb);
  538. return NULL;
  539. }
  540. /* gigaset_initcs
  541. * Allocate and initialize cardstate structure for Gigaset driver
  542. * Calls hardware dependent gigaset_initcshw() function
  543. * Calls B channel initialization function gigaset_initbcs() for each B channel
  544. * parameters:
  545. * drv hardware driver the device belongs to
  546. * channels number of B channels supported by device
  547. * onechannel !=0: B channel data and AT commands share one
  548. * communication channel
  549. * ==0: B channels have separate communication channels
  550. * ignoreframes number of frames to ignore after setting up B channel
  551. * cidmode !=0: start in CallID mode
  552. * modulename name of driver module (used for I4L registration)
  553. * return value:
  554. * pointer to cardstate structure
  555. */
  556. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  557. int onechannel, int ignoreframes,
  558. int cidmode, const char *modulename)
  559. {
  560. struct cardstate *cs = NULL;
  561. unsigned long flags;
  562. int i;
  563. gig_dbg(DEBUG_INIT, "allocating cs");
  564. cs = alloc_cs(drv);
  565. if (!cs)
  566. goto error;
  567. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  568. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  569. if (!cs->bcs)
  570. goto error;
  571. gig_dbg(DEBUG_INIT, "allocating inbuf");
  572. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  573. if (!cs->inbuf)
  574. goto error;
  575. cs->cs_init = 0;
  576. cs->channels = channels;
  577. cs->onechannel = onechannel;
  578. cs->ignoreframes = ignoreframes;
  579. INIT_LIST_HEAD(&cs->temp_at_states);
  580. cs->running = 0;
  581. init_timer(&cs->timer); /* clear next & prev */
  582. spin_lock_init(&cs->ev_lock);
  583. cs->ev_tail = 0;
  584. cs->ev_head = 0;
  585. mutex_init(&cs->mutex);
  586. mutex_lock(&cs->mutex);
  587. tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
  588. (unsigned long) cs);
  589. atomic_set(&cs->commands_pending, 0);
  590. cs->cur_at_seq = 0;
  591. cs->gotfwver = -1;
  592. cs->open_count = 0;
  593. cs->dev = NULL;
  594. cs->tty = NULL;
  595. cs->cidmode = cidmode != 0;
  596. //if(onechannel) { //FIXME
  597. cs->tabnocid = gigaset_tab_nocid_m10x;
  598. cs->tabcid = gigaset_tab_cid_m10x;
  599. //} else {
  600. // cs->tabnocid = gigaset_tab_nocid;
  601. // cs->tabcid = gigaset_tab_cid;
  602. //}
  603. init_waitqueue_head(&cs->waitqueue);
  604. cs->waiting = 0;
  605. atomic_set(&cs->mode, M_UNKNOWN);
  606. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  607. for (i = 0; i < channels; ++i) {
  608. gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
  609. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  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. if (onechannel) { //FIXME distinction necessary?
  620. gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
  621. } else
  622. gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
  623. cs->connected = 0;
  624. cs->isdn_up = 0;
  625. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  626. cs->cmdbuf = cs->lastcmdbuf = NULL;
  627. spin_lock_init(&cs->cmdlock);
  628. cs->curlen = 0;
  629. cs->cmdbytes = 0;
  630. gig_dbg(DEBUG_INIT, "setting up iif");
  631. if (!gigaset_register_to_LL(cs, modulename)) {
  632. err("register_isdn failed");
  633. goto error;
  634. }
  635. make_valid(cs, VALID_ID);
  636. ++cs->cs_init;
  637. gig_dbg(DEBUG_INIT, "setting up hw");
  638. if (!cs->ops->initcshw(cs))
  639. goto error;
  640. ++cs->cs_init;
  641. gigaset_if_init(cs);
  642. spin_lock_irqsave(&cs->lock, flags);
  643. cs->running = 1;
  644. spin_unlock_irqrestore(&cs->lock, flags);
  645. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  646. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  647. /* FIXME: can jiffies increase too much until the timer is added?
  648. * Same problem(?) with mod_timer() in timer_tick(). */
  649. add_timer(&cs->timer);
  650. gig_dbg(DEBUG_INIT, "cs initialized");
  651. mutex_unlock(&cs->mutex);
  652. return cs;
  653. error: if (cs)
  654. mutex_unlock(&cs->mutex);
  655. gig_dbg(DEBUG_INIT, "failed");
  656. gigaset_freecs(cs);
  657. return NULL;
  658. }
  659. EXPORT_SYMBOL_GPL(gigaset_initcs);
  660. /* ReInitialize the b-channel structure on hangup */
  661. void gigaset_bcs_reinit(struct bc_state *bcs)
  662. {
  663. struct sk_buff *skb;
  664. struct cardstate *cs = bcs->cs;
  665. unsigned long flags;
  666. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  667. dev_kfree_skb(skb);
  668. spin_lock_irqsave(&cs->lock, flags);
  669. clear_at_state(&bcs->at_state);
  670. bcs->at_state.ConState = 0;
  671. bcs->at_state.timer_active = 0;
  672. bcs->at_state.timer_expires = 0;
  673. bcs->at_state.cid = -1; /* No CID defined */
  674. spin_unlock_irqrestore(&cs->lock, flags);
  675. bcs->inputstate = 0;
  676. #ifdef CONFIG_GIGASET_DEBUG
  677. bcs->emptycount = 0;
  678. #endif
  679. bcs->fcs = PPP_INITFCS;
  680. bcs->chstate = 0;
  681. bcs->ignore = cs->ignoreframes;
  682. if (bcs->ignore)
  683. bcs->inputstate |= INS_skip_frame;
  684. cs->ops->reinitbcshw(bcs);
  685. }
  686. static void cleanup_cs(struct cardstate *cs)
  687. {
  688. struct cmdbuf_t *cb, *tcb;
  689. int i;
  690. unsigned long flags;
  691. spin_lock_irqsave(&cs->lock, flags);
  692. atomic_set(&cs->mode, M_UNKNOWN);
  693. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  694. clear_at_state(&cs->at_state);
  695. dealloc_at_states(cs);
  696. free_strings(&cs->at_state);
  697. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  698. kfree(cs->inbuf->rcvbuf);
  699. cs->inbuf->rcvbuf = NULL;
  700. cs->inbuf->inputstate = INS_command;
  701. atomic_set(&cs->inbuf->head, 0);
  702. atomic_set(&cs->inbuf->tail, 0);
  703. cb = cs->cmdbuf;
  704. while (cb) {
  705. tcb = cb;
  706. cb = cb->next;
  707. kfree(tcb);
  708. }
  709. cs->cmdbuf = cs->lastcmdbuf = NULL;
  710. cs->curlen = 0;
  711. cs->cmdbytes = 0;
  712. cs->gotfwver = -1;
  713. cs->dle = 0;
  714. cs->cur_at_seq = 0;
  715. atomic_set(&cs->commands_pending, 0);
  716. cs->cbytes = 0;
  717. spin_unlock_irqrestore(&cs->lock, flags);
  718. for (i = 0; i < cs->channels; ++i) {
  719. gigaset_freebcs(cs->bcs + i);
  720. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  721. break; //FIXME error handling
  722. }
  723. if (cs->waiting) {
  724. cs->cmd_result = -ENODEV;
  725. cs->waiting = 0;
  726. wake_up_interruptible(&cs->waitqueue);
  727. }
  728. }
  729. int gigaset_start(struct cardstate *cs)
  730. {
  731. unsigned long flags;
  732. if (mutex_lock_interruptible(&cs->mutex))
  733. return 0;
  734. spin_lock_irqsave(&cs->lock, flags);
  735. cs->connected = 1;
  736. spin_unlock_irqrestore(&cs->lock, flags);
  737. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  738. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  739. cs->ops->baud_rate(cs, B115200);
  740. cs->ops->set_line_ctrl(cs, CS8);
  741. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  742. } else {
  743. //FIXME use some saved values?
  744. }
  745. cs->waiting = 1;
  746. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  747. cs->waiting = 0;
  748. //FIXME what should we do?
  749. goto error;
  750. }
  751. gig_dbg(DEBUG_CMD, "scheduling START");
  752. gigaset_schedule_event(cs);
  753. wait_event(cs->waitqueue, !cs->waiting);
  754. /* set up device sysfs */
  755. gigaset_init_dev_sysfs(cs);
  756. mutex_unlock(&cs->mutex);
  757. return 1;
  758. error:
  759. mutex_unlock(&cs->mutex);
  760. return 0;
  761. }
  762. EXPORT_SYMBOL_GPL(gigaset_start);
  763. void gigaset_shutdown(struct cardstate *cs)
  764. {
  765. mutex_lock(&cs->mutex);
  766. cs->waiting = 1;
  767. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
  768. //FIXME what should we do?
  769. goto exit;
  770. }
  771. gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
  772. gigaset_schedule_event(cs);
  773. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  774. warn("%s: aborted", __func__);
  775. //FIXME
  776. }
  777. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  778. //FIXME?
  779. //gigaset_baud_rate(cs, B115200);
  780. //gigaset_set_line_ctrl(cs, CS8);
  781. //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
  782. //cs->control_state = 0;
  783. } else {
  784. //FIXME use some saved values?
  785. }
  786. cleanup_cs(cs);
  787. exit:
  788. mutex_unlock(&cs->mutex);
  789. }
  790. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  791. void gigaset_stop(struct cardstate *cs)
  792. {
  793. mutex_lock(&cs->mutex);
  794. cs->waiting = 1;
  795. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  796. //FIXME what should we do?
  797. goto exit;
  798. }
  799. gig_dbg(DEBUG_CMD, "scheduling STOP");
  800. gigaset_schedule_event(cs);
  801. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  802. warn("%s: aborted", __func__);
  803. //FIXME
  804. }
  805. /* clear device sysfs */
  806. gigaset_free_dev_sysfs(cs);
  807. cleanup_cs(cs);
  808. exit:
  809. mutex_unlock(&cs->mutex);
  810. }
  811. EXPORT_SYMBOL_GPL(gigaset_stop);
  812. static LIST_HEAD(drivers);
  813. static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
  814. struct cardstate *gigaset_get_cs_by_id(int id)
  815. {
  816. unsigned long flags;
  817. static struct cardstate *ret = NULL;
  818. static struct cardstate *cs;
  819. struct gigaset_driver *drv;
  820. unsigned i;
  821. spin_lock_irqsave(&driver_lock, flags);
  822. list_for_each_entry(drv, &drivers, list) {
  823. spin_lock(&drv->lock);
  824. for (i = 0; i < drv->minors; ++i) {
  825. if (drv->flags[i] & VALID_ID) {
  826. cs = drv->cs + i;
  827. if (cs->myid == id)
  828. ret = cs;
  829. }
  830. if (ret)
  831. break;
  832. }
  833. spin_unlock(&drv->lock);
  834. if (ret)
  835. break;
  836. }
  837. spin_unlock_irqrestore(&driver_lock, flags);
  838. return ret;
  839. }
  840. void gigaset_debugdrivers(void)
  841. {
  842. unsigned long flags;
  843. static struct cardstate *cs;
  844. struct gigaset_driver *drv;
  845. unsigned i;
  846. spin_lock_irqsave(&driver_lock, flags);
  847. list_for_each_entry(drv, &drivers, list) {
  848. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  849. spin_lock(&drv->lock);
  850. for (i = 0; i < drv->minors; ++i) {
  851. gig_dbg(DEBUG_DRIVER, " index %u", i);
  852. gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
  853. drv->flags[i]);
  854. cs = drv->cs + i;
  855. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  856. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  857. cs->minor_index);
  858. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  859. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  860. }
  861. spin_unlock(&drv->lock);
  862. }
  863. spin_unlock_irqrestore(&driver_lock, flags);
  864. }
  865. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  866. {
  867. unsigned long flags;
  868. static struct cardstate *ret = NULL;
  869. struct gigaset_driver *drv;
  870. unsigned index;
  871. spin_lock_irqsave(&driver_lock, flags);
  872. list_for_each_entry(drv, &drivers, list) {
  873. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  874. continue;
  875. index = minor - drv->minor;
  876. spin_lock(&drv->lock);
  877. if (drv->flags[index] & VALID_MINOR)
  878. ret = drv->cs + index;
  879. spin_unlock(&drv->lock);
  880. if (ret)
  881. break;
  882. }
  883. spin_unlock_irqrestore(&driver_lock, flags);
  884. return ret;
  885. }
  886. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  887. {
  888. if (tty->index < 0 || tty->index >= tty->driver->num)
  889. return NULL;
  890. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  891. }
  892. void gigaset_freedriver(struct gigaset_driver *drv)
  893. {
  894. unsigned long flags;
  895. spin_lock_irqsave(&driver_lock, flags);
  896. list_del(&drv->list);
  897. spin_unlock_irqrestore(&driver_lock, flags);
  898. gigaset_if_freedriver(drv);
  899. module_put(drv->owner);
  900. kfree(drv->cs);
  901. kfree(drv->flags);
  902. kfree(drv);
  903. }
  904. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  905. /* gigaset_initdriver
  906. * Allocate and initialize gigaset_driver structure. Initialize interface.
  907. * parameters:
  908. * minor First minor number
  909. * minors Number of minors this driver can handle
  910. * procname Name of the driver
  911. * devname Name of the device files (prefix without minor number)
  912. * devfsname Devfs name of the device files without %d
  913. * return value:
  914. * Pointer to the gigaset_driver structure on success, NULL on failure.
  915. */
  916. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  917. const char *procname,
  918. const char *devname,
  919. const char *devfsname,
  920. const struct gigaset_ops *ops,
  921. struct module *owner)
  922. {
  923. struct gigaset_driver *drv;
  924. unsigned long flags;
  925. unsigned i;
  926. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  927. if (!drv)
  928. return NULL;
  929. if (!try_module_get(owner))
  930. goto out1;
  931. drv->cs = NULL;
  932. drv->have_tty = 0;
  933. drv->minor = minor;
  934. drv->minors = minors;
  935. spin_lock_init(&drv->lock);
  936. drv->blocked = 0;
  937. drv->ops = ops;
  938. drv->owner = owner;
  939. INIT_LIST_HEAD(&drv->list);
  940. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  941. if (!drv->cs)
  942. goto out2;
  943. drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
  944. if (!drv->flags)
  945. goto out3;
  946. for (i = 0; i < minors; ++i) {
  947. drv->flags[i] = 0;
  948. drv->cs[i].driver = drv;
  949. drv->cs[i].ops = drv->ops;
  950. drv->cs[i].minor_index = i;
  951. }
  952. gigaset_if_initdriver(drv, procname, devname, devfsname);
  953. spin_lock_irqsave(&driver_lock, flags);
  954. list_add(&drv->list, &drivers);
  955. spin_unlock_irqrestore(&driver_lock, flags);
  956. return drv;
  957. out3:
  958. kfree(drv->cs);
  959. out2:
  960. module_put(owner);
  961. out1:
  962. kfree(drv);
  963. return NULL;
  964. }
  965. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  966. /* For drivers without fixed assignment device<->cardstate (usb) */
  967. struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
  968. {
  969. unsigned long flags;
  970. struct cardstate *cs = NULL;
  971. unsigned i;
  972. spin_lock_irqsave(&drv->lock, flags);
  973. if (drv->blocked)
  974. goto exit;
  975. for (i = 0; i < drv->minors; ++i) {
  976. if ((drv->flags[i] & VALID_MINOR) &&
  977. !(drv->flags[i] & ASSIGNED)) {
  978. drv->flags[i] |= ASSIGNED;
  979. cs = drv->cs + i;
  980. break;
  981. }
  982. }
  983. exit:
  984. spin_unlock_irqrestore(&drv->lock, flags);
  985. return cs;
  986. }
  987. EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
  988. void gigaset_unassign(struct cardstate *cs)
  989. {
  990. unsigned long flags;
  991. unsigned *minor_flags;
  992. struct gigaset_driver *drv;
  993. if (!cs)
  994. return;
  995. drv = cs->driver;
  996. spin_lock_irqsave(&drv->lock, flags);
  997. minor_flags = drv->flags + cs->minor_index;
  998. if (*minor_flags & VALID_MINOR)
  999. *minor_flags &= ~ASSIGNED;
  1000. spin_unlock_irqrestore(&drv->lock, flags);
  1001. }
  1002. EXPORT_SYMBOL_GPL(gigaset_unassign);
  1003. void gigaset_blockdriver(struct gigaset_driver *drv)
  1004. {
  1005. unsigned long flags;
  1006. spin_lock_irqsave(&drv->lock, flags);
  1007. drv->blocked = 1;
  1008. spin_unlock_irqrestore(&drv->lock, flags);
  1009. }
  1010. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  1011. static int __init gigaset_init_module(void)
  1012. {
  1013. /* in accordance with the principle of least astonishment,
  1014. * setting the 'debug' parameter to 1 activates a sensible
  1015. * set of default debug levels
  1016. */
  1017. if (gigaset_debuglevel == 1)
  1018. gigaset_debuglevel = DEBUG_DEFAULT;
  1019. info(DRIVER_AUTHOR);
  1020. info(DRIVER_DESC);
  1021. return 0;
  1022. }
  1023. static void __exit gigaset_exit_module(void)
  1024. {
  1025. }
  1026. module_init(gigaset_init_module);
  1027. module_exit(gigaset_exit_module);
  1028. MODULE_AUTHOR(DRIVER_AUTHOR);
  1029. MODULE_DESCRIPTION(DRIVER_DESC);
  1030. MODULE_LICENSE("GPL");