usb-gigaset.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * USB driver for Gigaset 307x directly or using M105 Data.
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers <Eilers.Stefan@epost.de>
  5. * and Hansjoerg Lipp <hjlipp@web.de>.
  6. *
  7. * This driver was derived from the USB skeleton driver by
  8. * Greg Kroah-Hartman <greg@kroah.com>
  9. *
  10. * =====================================================================
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. * =====================================================================
  16. */
  17. #include "gigaset.h"
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/usb.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. /* Version Information */
  25. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers <Eilers.Stefan@epost.de>"
  26. #define DRIVER_DESC "USB Driver for Gigaset 307x using M105"
  27. /* Module parameters */
  28. static int startmode = SM_ISDN;
  29. static int cidmode = 1;
  30. module_param(startmode, int, S_IRUGO);
  31. module_param(cidmode, int, S_IRUGO);
  32. MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
  33. MODULE_PARM_DESC(cidmode, "Call-ID mode");
  34. #define GIGASET_MINORS 1
  35. #define GIGASET_MINOR 8
  36. #define GIGASET_MODULENAME "usb_gigaset"
  37. #define GIGASET_DEVFSNAME "gig/usb/"
  38. #define GIGASET_DEVNAME "ttyGU"
  39. #define IF_WRITEBUF 2000 //FIXME // WAKEUP_CHARS: 256
  40. /* Values for the Gigaset M105 Data */
  41. #define USB_M105_VENDOR_ID 0x0681
  42. #define USB_M105_PRODUCT_ID 0x0009
  43. /* table of devices that work with this driver */
  44. static struct usb_device_id gigaset_table [] = {
  45. { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) },
  46. { } /* Terminating entry */
  47. };
  48. MODULE_DEVICE_TABLE(usb, gigaset_table);
  49. /*
  50. * Control requests (empty fields: 00)
  51. *
  52. * RT|RQ|VALUE|INDEX|LEN |DATA
  53. * In:
  54. * C1 08 01
  55. * Get flags (1 byte). Bits: 0=dtr,1=rts,3-7:?
  56. * C1 0F ll ll
  57. * Get device information/status (llll: 0x200 and 0x40 seen).
  58. * Real size: I only saw MIN(llll,0x64).
  59. * Contents: seems to be always the same...
  60. * offset 0x00: Length of this structure (0x64) (len: 1,2,3 bytes)
  61. * offset 0x3c: String (16 bit chars): "MCCI USB Serial V2.0"
  62. * rest: ?
  63. * Out:
  64. * 41 11
  65. * Initialize/reset device ?
  66. * 41 00 xx 00
  67. * ? (xx=00 or 01; 01 on start, 00 on close)
  68. * 41 07 vv mm
  69. * Set/clear flags vv=value, mm=mask (see RQ 08)
  70. * 41 12 xx
  71. * Used before the following configuration requests are issued
  72. * (with xx=0x0f). I've seen other values<0xf, though.
  73. * 41 01 xx xx
  74. * Set baud rate. xxxx=ceil(0x384000/rate)=trunc(0x383fff/rate)+1.
  75. * 41 03 ps bb
  76. * Set byte size and parity. p: 0x20=even,0x10=odd,0x00=no parity
  77. * [ 0x30: m, 0x40: s ]
  78. * [s: 0: 1 stop bit; 1: 1.5; 2: 2]
  79. * bb: bits/byte (seen 7 and 8)
  80. * 41 13 -- -- -- -- 10 00 ww 00 00 00 xx 00 00 00 yy 00 00 00 zz 00 00 00
  81. * ??
  82. * Initialization: 01, 40, 00, 00
  83. * Open device: 00 40, 00, 00
  84. * yy and zz seem to be equal, either 0x00 or 0x0a
  85. * (ww,xx) pairs seen: (00,00), (00,40), (01,40), (09,80), (19,80)
  86. * 41 19 -- -- -- -- 06 00 00 00 00 xx 11 13
  87. * Used after every "configuration sequence" (RQ 12, RQs 01/03/13).
  88. * xx is usually 0x00 but was 0x7e before starting data transfer
  89. * in unimodem mode. So, this might be an array of characters that need
  90. * special treatment ("commit all bufferd data"?), 11=^Q, 13=^S.
  91. *
  92. * Unimodem mode: use "modprobe ppp_async flag_time=0" as the device _needs_ two
  93. * flags per packet.
  94. */
  95. static int gigaset_probe(struct usb_interface *interface,
  96. const struct usb_device_id *id);
  97. static void gigaset_disconnect(struct usb_interface *interface);
  98. static struct gigaset_driver *driver = NULL;
  99. static struct cardstate *cardstate = NULL;
  100. /* usb specific object needed to register this driver with the usb subsystem */
  101. static struct usb_driver gigaset_usb_driver = {
  102. .name = GIGASET_MODULENAME,
  103. .probe = gigaset_probe,
  104. .disconnect = gigaset_disconnect,
  105. .id_table = gigaset_table,
  106. };
  107. struct usb_cardstate {
  108. struct usb_device *udev; /* usb device pointer */
  109. struct usb_interface *interface; /* interface for this device */
  110. atomic_t busy; /* bulk output in progress */
  111. /* Output buffer */
  112. unsigned char *bulk_out_buffer;
  113. int bulk_out_size;
  114. __u8 bulk_out_endpointAddr;
  115. struct urb *bulk_out_urb;
  116. /* Input buffer */
  117. int rcvbuf_size;
  118. struct urb *read_urb;
  119. __u8 int_in_endpointAddr;
  120. char bchars[6]; /* for request 0x19 */
  121. };
  122. struct usb_bc_state {};
  123. static inline unsigned tiocm_to_gigaset(unsigned state)
  124. {
  125. return ((state & TIOCM_DTR) ? 1 : 0) | ((state & TIOCM_RTS) ? 2 : 0);
  126. }
  127. #ifdef CONFIG_GIGASET_UNDOCREQ
  128. /* WARNING: EXPERIMENTAL! */
  129. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  130. unsigned new_state)
  131. {
  132. struct usb_device *udev = cs->hw.usb->udev;
  133. unsigned mask, val;
  134. int r;
  135. mask = tiocm_to_gigaset(old_state ^ new_state);
  136. val = tiocm_to_gigaset(new_state);
  137. gig_dbg(DEBUG_USBREQ, "set flags 0x%02x with mask 0x%02x", val, mask);
  138. // don't use this in an interrupt/BH
  139. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 7, 0x41,
  140. (val & 0xff) | ((mask & 0xff) << 8), 0,
  141. NULL, 0, 2000 /* timeout? */);
  142. if (r < 0)
  143. return r;
  144. //..
  145. return 0;
  146. }
  147. static int set_value(struct cardstate *cs, u8 req, u16 val)
  148. {
  149. struct usb_device *udev = cs->hw.usb->udev;
  150. int r, r2;
  151. gig_dbg(DEBUG_USBREQ, "request %02x (%04x)",
  152. (unsigned)req, (unsigned)val);
  153. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x12, 0x41,
  154. 0xf /*?*/, 0, NULL, 0, 2000 /*?*/);
  155. /* no idea what this does */
  156. if (r < 0) {
  157. dev_err(&udev->dev, "error %d on request 0x12\n", -r);
  158. return r;
  159. }
  160. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), req, 0x41,
  161. val, 0, NULL, 0, 2000 /*?*/);
  162. if (r < 0)
  163. dev_err(&udev->dev, "error %d on request 0x%02x\n",
  164. -r, (unsigned)req);
  165. r2 = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
  166. 0, 0, cs->hw.usb->bchars, 6, 2000 /*?*/);
  167. if (r2 < 0)
  168. dev_err(&udev->dev, "error %d on request 0x19\n", -r2);
  169. return r < 0 ? r : (r2 < 0 ? r2 : 0);
  170. }
  171. /* WARNING: HIGHLY EXPERIMENTAL! */
  172. // don't use this in an interrupt/BH
  173. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  174. {
  175. u16 val;
  176. u32 rate;
  177. cflag &= CBAUD;
  178. switch (cflag) {
  179. //FIXME more values?
  180. case B300: rate = 300; break;
  181. case B600: rate = 600; break;
  182. case B1200: rate = 1200; break;
  183. case B2400: rate = 2400; break;
  184. case B4800: rate = 4800; break;
  185. case B9600: rate = 9600; break;
  186. case B19200: rate = 19200; break;
  187. case B38400: rate = 38400; break;
  188. case B57600: rate = 57600; break;
  189. case B115200: rate = 115200; break;
  190. default:
  191. rate = 9600;
  192. dev_err(cs->dev, "unsupported baudrate request 0x%x,"
  193. " using default of B9600\n", cflag);
  194. }
  195. val = 0x383fff / rate + 1;
  196. return set_value(cs, 1, val);
  197. }
  198. /* WARNING: HIGHLY EXPERIMENTAL! */
  199. // don't use this in an interrupt/BH
  200. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  201. {
  202. u16 val = 0;
  203. /* set the parity */
  204. if (cflag & PARENB)
  205. val |= (cflag & PARODD) ? 0x10 : 0x20;
  206. /* set the number of data bits */
  207. switch (cflag & CSIZE) {
  208. case CS5:
  209. val |= 5 << 8; break;
  210. case CS6:
  211. val |= 6 << 8; break;
  212. case CS7:
  213. val |= 7 << 8; break;
  214. case CS8:
  215. val |= 8 << 8; break;
  216. default:
  217. dev_err(cs->dev, "CSIZE was not CS5-CS8, using default of 8\n");
  218. val |= 8 << 8;
  219. break;
  220. }
  221. /* set the number of stop bits */
  222. if (cflag & CSTOPB) {
  223. if ((cflag & CSIZE) == CS5)
  224. val |= 1; /* 1.5 stop bits */ //FIXME is this okay?
  225. else
  226. val |= 2; /* 2 stop bits */
  227. }
  228. return set_value(cs, 3, val);
  229. }
  230. #else
  231. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  232. unsigned new_state)
  233. {
  234. return -EINVAL;
  235. }
  236. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  237. {
  238. return -EINVAL;
  239. }
  240. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  241. {
  242. return -EINVAL;
  243. }
  244. #endif
  245. /*================================================================================================================*/
  246. static int gigaset_init_bchannel(struct bc_state *bcs)
  247. {
  248. /* nothing to do for M10x */
  249. gigaset_bchannel_up(bcs);
  250. return 0;
  251. }
  252. static int gigaset_close_bchannel(struct bc_state *bcs)
  253. {
  254. /* nothing to do for M10x */
  255. gigaset_bchannel_down(bcs);
  256. return 0;
  257. }
  258. static int write_modem(struct cardstate *cs);
  259. static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb);
  260. /* Write tasklet handler: Continue sending current skb, or send command, or
  261. * start sending an skb from the send queue.
  262. */
  263. static void gigaset_modem_fill(unsigned long data)
  264. {
  265. struct cardstate *cs = (struct cardstate *) data;
  266. struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
  267. struct cmdbuf_t *cb;
  268. unsigned long flags;
  269. int again;
  270. gig_dbg(DEBUG_OUTPUT, "modem_fill");
  271. if (atomic_read(&cs->hw.usb->busy)) {
  272. gig_dbg(DEBUG_OUTPUT, "modem_fill: busy");
  273. return;
  274. }
  275. do {
  276. again = 0;
  277. if (!bcs->tx_skb) { /* no skb is being sent */
  278. spin_lock_irqsave(&cs->cmdlock, flags);
  279. cb = cs->cmdbuf;
  280. spin_unlock_irqrestore(&cs->cmdlock, flags);
  281. if (cb) { /* commands to send? */
  282. gig_dbg(DEBUG_OUTPUT, "modem_fill: cb");
  283. if (send_cb(cs, cb) < 0) {
  284. gig_dbg(DEBUG_OUTPUT,
  285. "modem_fill: send_cb failed");
  286. again = 1; /* no callback will be
  287. called! */
  288. }
  289. } else { /* skbs to send? */
  290. bcs->tx_skb = skb_dequeue(&bcs->squeue);
  291. if (bcs->tx_skb)
  292. gig_dbg(DEBUG_INTR,
  293. "Dequeued skb (Adr: %lx)!",
  294. (unsigned long) bcs->tx_skb);
  295. }
  296. }
  297. if (bcs->tx_skb) {
  298. gig_dbg(DEBUG_OUTPUT, "modem_fill: tx_skb");
  299. if (write_modem(cs) < 0) {
  300. gig_dbg(DEBUG_OUTPUT,
  301. "modem_fill: write_modem failed");
  302. // FIXME should we tell the LL?
  303. again = 1; /* no callback will be called! */
  304. }
  305. }
  306. } while (again);
  307. }
  308. /**
  309. * gigaset_read_int_callback
  310. *
  311. * It is called if the data was received from the device.
  312. */
  313. static void gigaset_read_int_callback(struct urb *urb, struct pt_regs *regs)
  314. {
  315. struct inbuf_t *inbuf = urb->context;
  316. struct cardstate *cs = inbuf->cs;
  317. int resubmit = 0;
  318. int r;
  319. unsigned numbytes;
  320. unsigned char *src;
  321. if (!atomic_read(&cs->connected)) {
  322. err("%s: disconnected", __func__);
  323. return;
  324. }
  325. if (!urb->status) {
  326. numbytes = urb->actual_length;
  327. if (numbytes) {
  328. src = inbuf->rcvbuf;
  329. if (unlikely(*src))
  330. dev_warn(cs->dev,
  331. "%s: There was no leading 0, but 0x%02x!\n",
  332. __func__, (unsigned) *src);
  333. ++src; /* skip leading 0x00 */
  334. --numbytes;
  335. if (gigaset_fill_inbuf(inbuf, src, numbytes)) {
  336. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  337. gigaset_schedule_event(inbuf->cs);
  338. }
  339. } else
  340. gig_dbg(DEBUG_INTR, "Received zero block length");
  341. resubmit = 1;
  342. } else {
  343. /* The urb might have been killed. */
  344. gig_dbg(DEBUG_ANY, "%s - nonzero read bulk status received: %d",
  345. __func__, urb->status);
  346. if (urb->status != -ENOENT) /* not killed */
  347. resubmit = 1;
  348. }
  349. if (resubmit) {
  350. r = usb_submit_urb(urb, SLAB_ATOMIC);
  351. if (r)
  352. dev_err(cs->dev, "error %d when resubmitting urb.\n",
  353. -r);
  354. }
  355. }
  356. /* This callback routine is called when data was transmitted to the device. */
  357. static void gigaset_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
  358. {
  359. struct cardstate *cs = urb->context;
  360. #ifdef CONFIG_GIGASET_DEBUG
  361. if (!atomic_read(&cs->connected)) {
  362. err("%s: not connected", __func__);
  363. return;
  364. }
  365. #endif
  366. if (urb->status)
  367. dev_err(cs->dev, "bulk transfer failed (status %d)\n",
  368. -urb->status);
  369. /* That's all we can do. Communication problems
  370. are handled by timeouts or network protocols. */
  371. atomic_set(&cs->hw.usb->busy, 0);
  372. tasklet_schedule(&cs->write_tasklet);
  373. }
  374. static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb)
  375. {
  376. struct cmdbuf_t *tcb;
  377. unsigned long flags;
  378. int count;
  379. int status = -ENOENT; // FIXME
  380. struct usb_cardstate *ucs = cs->hw.usb;
  381. do {
  382. if (!cb->len) {
  383. tcb = cb;
  384. spin_lock_irqsave(&cs->cmdlock, flags);
  385. cs->cmdbytes -= cs->curlen;
  386. gig_dbg(DEBUG_OUTPUT, "send_cb: sent %u bytes, %u left",
  387. cs->curlen, cs->cmdbytes);
  388. cs->cmdbuf = cb = cb->next;
  389. if (cb) {
  390. cb->prev = NULL;
  391. cs->curlen = cb->len;
  392. } else {
  393. cs->lastcmdbuf = NULL;
  394. cs->curlen = 0;
  395. }
  396. spin_unlock_irqrestore(&cs->cmdlock, flags);
  397. if (tcb->wake_tasklet)
  398. tasklet_schedule(tcb->wake_tasklet);
  399. kfree(tcb);
  400. }
  401. if (cb) {
  402. count = min(cb->len, ucs->bulk_out_size);
  403. usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
  404. usb_sndbulkpipe(ucs->udev,
  405. ucs->bulk_out_endpointAddr & 0x0f),
  406. cb->buf + cb->offset, count,
  407. gigaset_write_bulk_callback, cs);
  408. cb->offset += count;
  409. cb->len -= count;
  410. atomic_set(&ucs->busy, 1);
  411. gig_dbg(DEBUG_OUTPUT, "send_cb: send %d bytes", count);
  412. status = usb_submit_urb(ucs->bulk_out_urb, SLAB_ATOMIC);
  413. if (status) {
  414. atomic_set(&ucs->busy, 0);
  415. dev_err(cs->dev,
  416. "could not submit urb (error %d)\n",
  417. -status);
  418. cb->len = 0; /* skip urb => remove cb+wakeup
  419. in next loop cycle */
  420. }
  421. }
  422. } while (cb && status); /* next command on error */
  423. return status;
  424. }
  425. /* Send command to device. */
  426. static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
  427. int len, struct tasklet_struct *wake_tasklet)
  428. {
  429. struct cmdbuf_t *cb;
  430. unsigned long flags;
  431. gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
  432. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  433. "CMD Transmit", len, buf);
  434. if (!atomic_read(&cs->connected)) {
  435. err("%s: not connected", __func__);
  436. return -ENODEV;
  437. }
  438. if (len <= 0)
  439. return 0;
  440. if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
  441. dev_err(cs->dev, "%s: out of memory\n", __func__);
  442. return -ENOMEM;
  443. }
  444. memcpy(cb->buf, buf, len);
  445. cb->len = len;
  446. cb->offset = 0;
  447. cb->next = NULL;
  448. cb->wake_tasklet = wake_tasklet;
  449. spin_lock_irqsave(&cs->cmdlock, flags);
  450. cb->prev = cs->lastcmdbuf;
  451. if (cs->lastcmdbuf)
  452. cs->lastcmdbuf->next = cb;
  453. else {
  454. cs->cmdbuf = cb;
  455. cs->curlen = len;
  456. }
  457. cs->cmdbytes += len;
  458. cs->lastcmdbuf = cb;
  459. spin_unlock_irqrestore(&cs->cmdlock, flags);
  460. tasklet_schedule(&cs->write_tasklet);
  461. return len;
  462. }
  463. static int gigaset_write_room(struct cardstate *cs)
  464. {
  465. unsigned long flags;
  466. unsigned bytes;
  467. spin_lock_irqsave(&cs->cmdlock, flags);
  468. bytes = cs->cmdbytes;
  469. spin_unlock_irqrestore(&cs->cmdlock, flags);
  470. return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
  471. }
  472. static int gigaset_chars_in_buffer(struct cardstate *cs)
  473. {
  474. return cs->cmdbytes;
  475. }
  476. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  477. {
  478. #ifdef CONFIG_GIGASET_UNDOCREQ
  479. struct usb_device *udev = cs->hw.usb->udev;
  480. gigaset_dbg_buffer(DEBUG_USBREQ, "brkchars", 6, buf);
  481. memcpy(cs->hw.usb->bchars, buf, 6);
  482. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
  483. 0, 0, &buf, 6, 2000);
  484. #else
  485. return -EINVAL;
  486. #endif
  487. }
  488. static int gigaset_freebcshw(struct bc_state *bcs)
  489. {
  490. if (!bcs->hw.usb)
  491. return 0;
  492. //FIXME
  493. kfree(bcs->hw.usb);
  494. return 1;
  495. }
  496. /* Initialize the b-channel structure */
  497. static int gigaset_initbcshw(struct bc_state *bcs)
  498. {
  499. bcs->hw.usb = kmalloc(sizeof(struct usb_bc_state), GFP_KERNEL);
  500. if (!bcs->hw.usb)
  501. return 0;
  502. return 1;
  503. }
  504. static void gigaset_reinitbcshw(struct bc_state *bcs)
  505. {
  506. }
  507. static void gigaset_freecshw(struct cardstate *cs)
  508. {
  509. tasklet_kill(&cs->write_tasklet);
  510. kfree(cs->hw.usb);
  511. }
  512. static int gigaset_initcshw(struct cardstate *cs)
  513. {
  514. struct usb_cardstate *ucs;
  515. cs->hw.usb = ucs =
  516. kmalloc(sizeof(struct usb_cardstate), GFP_KERNEL);
  517. if (!ucs)
  518. return 0;
  519. ucs->bchars[0] = 0;
  520. ucs->bchars[1] = 0;
  521. ucs->bchars[2] = 0;
  522. ucs->bchars[3] = 0;
  523. ucs->bchars[4] = 0x11;
  524. ucs->bchars[5] = 0x13;
  525. ucs->bulk_out_buffer = NULL;
  526. ucs->bulk_out_urb = NULL;
  527. //ucs->urb_cmd_out = NULL;
  528. ucs->read_urb = NULL;
  529. tasklet_init(&cs->write_tasklet,
  530. &gigaset_modem_fill, (unsigned long) cs);
  531. return 1;
  532. }
  533. /* Send data from current skb to the device. */
  534. static int write_modem(struct cardstate *cs)
  535. {
  536. int ret = 0;
  537. int count;
  538. struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
  539. struct usb_cardstate *ucs = cs->hw.usb;
  540. gig_dbg(DEBUG_WRITE, "len: %d...", bcs->tx_skb->len);
  541. if (!bcs->tx_skb->len) {
  542. dev_kfree_skb_any(bcs->tx_skb);
  543. bcs->tx_skb = NULL;
  544. return -EINVAL;
  545. }
  546. /* Copy data to bulk out buffer and // FIXME copying not necessary
  547. * transmit data
  548. */
  549. count = min(bcs->tx_skb->len, (unsigned) ucs->bulk_out_size);
  550. memcpy(ucs->bulk_out_buffer, bcs->tx_skb->data, count);
  551. skb_pull(bcs->tx_skb, count);
  552. usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
  553. usb_sndbulkpipe(ucs->udev,
  554. ucs->bulk_out_endpointAddr & 0x0f),
  555. ucs->bulk_out_buffer, count,
  556. gigaset_write_bulk_callback, cs);
  557. atomic_set(&ucs->busy, 1);
  558. gig_dbg(DEBUG_OUTPUT, "write_modem: send %d bytes", count);
  559. ret = usb_submit_urb(ucs->bulk_out_urb, SLAB_ATOMIC);
  560. if (ret) {
  561. dev_err(cs->dev, "could not submit urb (error %d)\n", -ret);
  562. atomic_set(&ucs->busy, 0);
  563. }
  564. if (!bcs->tx_skb->len) {
  565. /* skb sent completely */
  566. gigaset_skb_sent(bcs, bcs->tx_skb); //FIXME also, when ret<0?
  567. gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
  568. (unsigned long) bcs->tx_skb);
  569. dev_kfree_skb_any(bcs->tx_skb);
  570. bcs->tx_skb = NULL;
  571. }
  572. return ret;
  573. }
  574. static int gigaset_probe(struct usb_interface *interface,
  575. const struct usb_device_id *id)
  576. {
  577. int retval;
  578. struct usb_device *udev = interface_to_usbdev(interface);
  579. unsigned int ifnum;
  580. struct usb_host_interface *hostif;
  581. struct cardstate *cs = NULL;
  582. struct usb_cardstate *ucs = NULL;
  583. struct usb_endpoint_descriptor *endpoint;
  584. int buffer_size;
  585. int alt;
  586. gig_dbg(DEBUG_ANY,
  587. "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
  588. __func__, le16_to_cpu(udev->descriptor.idVendor),
  589. le16_to_cpu(udev->descriptor.idProduct));
  590. retval = -ENODEV; //FIXME
  591. /* See if the device offered us matches what we can accept */
  592. if ((le16_to_cpu(udev->descriptor.idVendor != USB_M105_VENDOR_ID)) ||
  593. (le16_to_cpu(udev->descriptor.idProduct != USB_M105_PRODUCT_ID)))
  594. return -ENODEV;
  595. /* this starts to become ascii art... */
  596. hostif = interface->cur_altsetting;
  597. alt = hostif->desc.bAlternateSetting;
  598. ifnum = hostif->desc.bInterfaceNumber; // FIXME ?
  599. if (alt != 0 || ifnum != 0) {
  600. dev_warn(&udev->dev, "ifnum %d, alt %d\n", ifnum, alt);
  601. return -ENODEV;
  602. }
  603. /* Reject application specific intefaces
  604. *
  605. */
  606. if (hostif->desc.bInterfaceClass != 255) {
  607. dev_info(&udev->dev,
  608. "%s: Device matched but iface_desc[%d]->bInterfaceClass==%d!\n",
  609. __func__, ifnum, hostif->desc.bInterfaceClass);
  610. return -ENODEV;
  611. }
  612. dev_info(&udev->dev, "%s: Device matched ... !\n", __func__);
  613. cs = gigaset_getunassignedcs(driver);
  614. if (!cs) {
  615. dev_warn(&udev->dev, "no free cardstate\n");
  616. return -ENODEV;
  617. }
  618. ucs = cs->hw.usb;
  619. /* save off device structure ptrs for later use */
  620. usb_get_dev(udev);
  621. ucs->udev = udev;
  622. ucs->interface = interface;
  623. cs->dev = &interface->dev;
  624. /* save address of controller structure */
  625. usb_set_intfdata(interface, cs); // dev_set_drvdata(&interface->dev, cs);
  626. endpoint = &hostif->endpoint[0].desc;
  627. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  628. ucs->bulk_out_size = buffer_size;
  629. ucs->bulk_out_endpointAddr = endpoint->bEndpointAddress;
  630. ucs->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
  631. if (!ucs->bulk_out_buffer) {
  632. dev_err(cs->dev, "Couldn't allocate bulk_out_buffer\n");
  633. retval = -ENOMEM;
  634. goto error;
  635. }
  636. ucs->bulk_out_urb = usb_alloc_urb(0, SLAB_KERNEL);
  637. if (!ucs->bulk_out_urb) {
  638. dev_err(cs->dev, "Couldn't allocate bulk_out_urb\n");
  639. retval = -ENOMEM;
  640. goto error;
  641. }
  642. endpoint = &hostif->endpoint[1].desc;
  643. atomic_set(&ucs->busy, 0);
  644. ucs->read_urb = usb_alloc_urb(0, SLAB_KERNEL);
  645. if (!ucs->read_urb) {
  646. dev_err(cs->dev, "No free urbs available\n");
  647. retval = -ENOMEM;
  648. goto error;
  649. }
  650. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  651. ucs->rcvbuf_size = buffer_size;
  652. ucs->int_in_endpointAddr = endpoint->bEndpointAddress;
  653. cs->inbuf[0].rcvbuf = kmalloc(buffer_size, GFP_KERNEL);
  654. if (!cs->inbuf[0].rcvbuf) {
  655. dev_err(cs->dev, "Couldn't allocate rcvbuf\n");
  656. retval = -ENOMEM;
  657. goto error;
  658. }
  659. /* Fill the interrupt urb and send it to the core */
  660. usb_fill_int_urb(ucs->read_urb, udev,
  661. usb_rcvintpipe(udev,
  662. endpoint->bEndpointAddress & 0x0f),
  663. cs->inbuf[0].rcvbuf, buffer_size,
  664. gigaset_read_int_callback,
  665. cs->inbuf + 0, endpoint->bInterval);
  666. retval = usb_submit_urb(ucs->read_urb, SLAB_KERNEL);
  667. if (retval) {
  668. dev_err(cs->dev, "Could not submit URB (error %d)\n", -retval);
  669. goto error;
  670. }
  671. /* tell common part that the device is ready */
  672. if (startmode == SM_LOCKED)
  673. atomic_set(&cs->mstate, MS_LOCKED);
  674. if (!gigaset_start(cs)) {
  675. tasklet_kill(&cs->write_tasklet);
  676. retval = -ENODEV; //FIXME
  677. goto error;
  678. }
  679. return 0;
  680. error:
  681. if (ucs->read_urb)
  682. usb_kill_urb(ucs->read_urb);
  683. kfree(ucs->bulk_out_buffer);
  684. if (ucs->bulk_out_urb != NULL)
  685. usb_free_urb(ucs->bulk_out_urb);
  686. kfree(cs->inbuf[0].rcvbuf);
  687. if (ucs->read_urb != NULL)
  688. usb_free_urb(ucs->read_urb);
  689. usb_set_intfdata(interface, NULL);
  690. ucs->read_urb = ucs->bulk_out_urb = NULL;
  691. cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
  692. usb_put_dev(ucs->udev);
  693. ucs->udev = NULL;
  694. ucs->interface = NULL;
  695. gigaset_unassign(cs);
  696. return retval;
  697. }
  698. /**
  699. * skel_disconnect
  700. */
  701. static void gigaset_disconnect(struct usb_interface *interface)
  702. {
  703. struct cardstate *cs;
  704. struct usb_cardstate *ucs;
  705. cs = usb_get_intfdata(interface);
  706. ucs = cs->hw.usb;
  707. usb_kill_urb(ucs->read_urb);
  708. gigaset_stop(cs);
  709. usb_set_intfdata(interface, NULL);
  710. tasklet_kill(&cs->write_tasklet);
  711. usb_kill_urb(ucs->bulk_out_urb); /* FIXME: only if active? */
  712. kfree(ucs->bulk_out_buffer);
  713. if (ucs->bulk_out_urb != NULL)
  714. usb_free_urb(ucs->bulk_out_urb);
  715. kfree(cs->inbuf[0].rcvbuf);
  716. if (ucs->read_urb != NULL)
  717. usb_free_urb(ucs->read_urb);
  718. ucs->read_urb = ucs->bulk_out_urb = NULL;
  719. cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
  720. usb_put_dev(ucs->udev);
  721. ucs->interface = NULL;
  722. ucs->udev = NULL;
  723. cs->dev = NULL;
  724. gigaset_unassign(cs);
  725. }
  726. static struct gigaset_ops ops = {
  727. gigaset_write_cmd,
  728. gigaset_write_room,
  729. gigaset_chars_in_buffer,
  730. gigaset_brkchars,
  731. gigaset_init_bchannel,
  732. gigaset_close_bchannel,
  733. gigaset_initbcshw,
  734. gigaset_freebcshw,
  735. gigaset_reinitbcshw,
  736. gigaset_initcshw,
  737. gigaset_freecshw,
  738. gigaset_set_modem_ctrl,
  739. gigaset_baud_rate,
  740. gigaset_set_line_ctrl,
  741. gigaset_m10x_send_skb,
  742. gigaset_m10x_input,
  743. };
  744. /**
  745. * usb_gigaset_init
  746. * This function is called while kernel-module is loaded
  747. */
  748. static int __init usb_gigaset_init(void)
  749. {
  750. int result;
  751. /* allocate memory for our driver state and intialize it */
  752. if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  753. GIGASET_MODULENAME, GIGASET_DEVNAME,
  754. GIGASET_DEVFSNAME, &ops,
  755. THIS_MODULE)) == NULL)
  756. goto error;
  757. /* allocate memory for our device state and intialize it */
  758. cardstate = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
  759. if (!cardstate)
  760. goto error;
  761. /* register this driver with the USB subsystem */
  762. result = usb_register(&gigaset_usb_driver);
  763. if (result < 0) {
  764. err("usb_gigaset: usb_register failed (error %d)",
  765. -result);
  766. goto error;
  767. }
  768. info(DRIVER_AUTHOR);
  769. info(DRIVER_DESC);
  770. return 0;
  771. error: if (cardstate)
  772. gigaset_freecs(cardstate);
  773. cardstate = NULL;
  774. if (driver)
  775. gigaset_freedriver(driver);
  776. driver = NULL;
  777. return -1;
  778. }
  779. /**
  780. * usb_gigaset_exit
  781. * This function is called while unloading the kernel-module
  782. */
  783. static void __exit usb_gigaset_exit(void)
  784. {
  785. gigaset_blockdriver(driver); /* => probe will fail
  786. * => no gigaset_start any more
  787. */
  788. gigaset_shutdown(cardstate);
  789. /* from now on, no isdn callback should be possible */
  790. /* deregister this driver with the USB subsystem */
  791. usb_deregister(&gigaset_usb_driver);
  792. /* this will call the disconnect-callback */
  793. /* from now on, no disconnect/probe callback should be running */
  794. gigaset_freecs(cardstate);
  795. cardstate = NULL;
  796. gigaset_freedriver(driver);
  797. driver = NULL;
  798. }
  799. module_init(usb_gigaset_init);
  800. module_exit(usb_gigaset_exit);
  801. MODULE_AUTHOR(DRIVER_AUTHOR);
  802. MODULE_DESCRIPTION(DRIVER_DESC);
  803. MODULE_LICENSE("GPL");