usb-gigaset.c 26 KB

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