cdc-acm.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. /*
  2. * cdc-acm.c
  3. *
  4. * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
  5. * Copyright (c) 1999 Pavel Machek <pavel@ucw.cz>
  6. * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com>
  7. * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
  8. * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
  9. * Copyright (c) 2005 David Kubicek <dave@awk.cz>
  10. * Copyright (c) 2011 Johan Hovold <jhovold@gmail.com>
  11. *
  12. * USB Abstract Control Model driver for USB modems and ISDN adapters
  13. *
  14. * Sponsored by SuSE
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #undef DEBUG
  31. #undef VERBOSE_DEBUG
  32. #include <linux/kernel.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/tty.h>
  37. #include <linux/serial.h>
  38. #include <linux/tty_driver.h>
  39. #include <linux/tty_flip.h>
  40. #include <linux/serial.h>
  41. #include <linux/module.h>
  42. #include <linux/mutex.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/usb.h>
  45. #include <linux/usb/cdc.h>
  46. #include <asm/byteorder.h>
  47. #include <asm/unaligned.h>
  48. #include <linux/list.h>
  49. #include "cdc-acm.h"
  50. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
  51. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  52. static struct usb_driver acm_driver;
  53. static struct tty_driver *acm_tty_driver;
  54. static struct acm *acm_table[ACM_TTY_MINORS];
  55. static DEFINE_MUTEX(acm_table_lock);
  56. /*
  57. * acm_table accessors
  58. */
  59. /*
  60. * Look up an ACM structure by index. If found and not disconnected, increment
  61. * its refcount and return it with its mutex held.
  62. */
  63. static struct acm *acm_get_by_index(unsigned index)
  64. {
  65. struct acm *acm;
  66. mutex_lock(&acm_table_lock);
  67. acm = acm_table[index];
  68. if (acm) {
  69. mutex_lock(&acm->mutex);
  70. if (acm->disconnected) {
  71. mutex_unlock(&acm->mutex);
  72. acm = NULL;
  73. } else {
  74. tty_port_get(&acm->port);
  75. mutex_unlock(&acm->mutex);
  76. }
  77. }
  78. mutex_unlock(&acm_table_lock);
  79. return acm;
  80. }
  81. /*
  82. * Try to find an available minor number and if found, associate it with 'acm'.
  83. */
  84. static int acm_alloc_minor(struct acm *acm)
  85. {
  86. int minor;
  87. mutex_lock(&acm_table_lock);
  88. for (minor = 0; minor < ACM_TTY_MINORS; minor++) {
  89. if (!acm_table[minor]) {
  90. acm_table[minor] = acm;
  91. break;
  92. }
  93. }
  94. mutex_unlock(&acm_table_lock);
  95. return minor;
  96. }
  97. /* Release the minor number associated with 'acm'. */
  98. static void acm_release_minor(struct acm *acm)
  99. {
  100. mutex_lock(&acm_table_lock);
  101. acm_table[acm->minor] = NULL;
  102. mutex_unlock(&acm_table_lock);
  103. }
  104. /*
  105. * Functions for ACM control messages.
  106. */
  107. static int acm_ctrl_msg(struct acm *acm, int request, int value,
  108. void *buf, int len)
  109. {
  110. int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  111. request, USB_RT_ACM, value,
  112. acm->control->altsetting[0].desc.bInterfaceNumber,
  113. buf, len, 5000);
  114. dev_dbg(&acm->control->dev,
  115. "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
  116. __func__, request, value, len, retval);
  117. return retval < 0 ? retval : 0;
  118. }
  119. /* devices aren't required to support these requests.
  120. * the cdc acm descriptor tells whether they do...
  121. */
  122. #define acm_set_control(acm, control) \
  123. acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
  124. #define acm_set_line(acm, line) \
  125. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  126. #define acm_send_break(acm, ms) \
  127. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  128. /*
  129. * Write buffer management.
  130. * All of these assume proper locks taken by the caller.
  131. */
  132. static int acm_wb_alloc(struct acm *acm)
  133. {
  134. int i, wbn;
  135. struct acm_wb *wb;
  136. wbn = 0;
  137. i = 0;
  138. for (;;) {
  139. wb = &acm->wb[wbn];
  140. if (!wb->use) {
  141. wb->use = 1;
  142. return wbn;
  143. }
  144. wbn = (wbn + 1) % ACM_NW;
  145. if (++i >= ACM_NW)
  146. return -1;
  147. }
  148. }
  149. static int acm_wb_is_avail(struct acm *acm)
  150. {
  151. int i, n;
  152. unsigned long flags;
  153. n = ACM_NW;
  154. spin_lock_irqsave(&acm->write_lock, flags);
  155. for (i = 0; i < ACM_NW; i++)
  156. n -= acm->wb[i].use;
  157. spin_unlock_irqrestore(&acm->write_lock, flags);
  158. return n;
  159. }
  160. /*
  161. * Finish write. Caller must hold acm->write_lock
  162. */
  163. static void acm_write_done(struct acm *acm, struct acm_wb *wb)
  164. {
  165. wb->use = 0;
  166. acm->transmitting--;
  167. usb_autopm_put_interface_async(acm->control);
  168. }
  169. /*
  170. * Poke write.
  171. *
  172. * the caller is responsible for locking
  173. */
  174. static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
  175. {
  176. int rc;
  177. acm->transmitting++;
  178. wb->urb->transfer_buffer = wb->buf;
  179. wb->urb->transfer_dma = wb->dmah;
  180. wb->urb->transfer_buffer_length = wb->len;
  181. wb->urb->dev = acm->dev;
  182. rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
  183. if (rc < 0) {
  184. dev_err(&acm->data->dev,
  185. "%s - usb_submit_urb(write bulk) failed: %d\n",
  186. __func__, rc);
  187. acm_write_done(acm, wb);
  188. }
  189. return rc;
  190. }
  191. static int acm_write_start(struct acm *acm, int wbn)
  192. {
  193. unsigned long flags;
  194. struct acm_wb *wb = &acm->wb[wbn];
  195. int rc;
  196. spin_lock_irqsave(&acm->write_lock, flags);
  197. if (!acm->dev) {
  198. wb->use = 0;
  199. spin_unlock_irqrestore(&acm->write_lock, flags);
  200. return -ENODEV;
  201. }
  202. dev_vdbg(&acm->data->dev, "%s - susp_count %d\n", __func__,
  203. acm->susp_count);
  204. usb_autopm_get_interface_async(acm->control);
  205. if (acm->susp_count) {
  206. if (!acm->delayed_wb)
  207. acm->delayed_wb = wb;
  208. else
  209. usb_autopm_put_interface_async(acm->control);
  210. spin_unlock_irqrestore(&acm->write_lock, flags);
  211. return 0; /* A white lie */
  212. }
  213. usb_mark_last_busy(acm->dev);
  214. rc = acm_start_wb(acm, wb);
  215. spin_unlock_irqrestore(&acm->write_lock, flags);
  216. return rc;
  217. }
  218. /*
  219. * attributes exported through sysfs
  220. */
  221. static ssize_t show_caps
  222. (struct device *dev, struct device_attribute *attr, char *buf)
  223. {
  224. struct usb_interface *intf = to_usb_interface(dev);
  225. struct acm *acm = usb_get_intfdata(intf);
  226. return sprintf(buf, "%d", acm->ctrl_caps);
  227. }
  228. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  229. static ssize_t show_country_codes
  230. (struct device *dev, struct device_attribute *attr, char *buf)
  231. {
  232. struct usb_interface *intf = to_usb_interface(dev);
  233. struct acm *acm = usb_get_intfdata(intf);
  234. memcpy(buf, acm->country_codes, acm->country_code_size);
  235. return acm->country_code_size;
  236. }
  237. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  238. static ssize_t show_country_rel_date
  239. (struct device *dev, struct device_attribute *attr, char *buf)
  240. {
  241. struct usb_interface *intf = to_usb_interface(dev);
  242. struct acm *acm = usb_get_intfdata(intf);
  243. return sprintf(buf, "%d", acm->country_rel_date);
  244. }
  245. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  246. /*
  247. * Interrupt handlers for various ACM device responses
  248. */
  249. /* control interface reports status changes with "interrupt" transfers */
  250. static void acm_ctrl_irq(struct urb *urb)
  251. {
  252. struct acm *acm = urb->context;
  253. struct usb_cdc_notification *dr = urb->transfer_buffer;
  254. struct tty_struct *tty;
  255. unsigned char *data;
  256. int newctrl;
  257. int retval;
  258. int status = urb->status;
  259. switch (status) {
  260. case 0:
  261. /* success */
  262. break;
  263. case -ECONNRESET:
  264. case -ENOENT:
  265. case -ESHUTDOWN:
  266. /* this urb is terminated, clean up */
  267. dev_dbg(&acm->control->dev,
  268. "%s - urb shutting down with status: %d\n",
  269. __func__, status);
  270. return;
  271. default:
  272. dev_dbg(&acm->control->dev,
  273. "%s - nonzero urb status received: %d\n",
  274. __func__, status);
  275. goto exit;
  276. }
  277. usb_mark_last_busy(acm->dev);
  278. data = (unsigned char *)(dr + 1);
  279. switch (dr->bNotificationType) {
  280. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  281. dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
  282. __func__, dr->wValue);
  283. break;
  284. case USB_CDC_NOTIFY_SERIAL_STATE:
  285. tty = tty_port_tty_get(&acm->port);
  286. newctrl = get_unaligned_le16(data);
  287. if (tty) {
  288. if (!acm->clocal &&
  289. (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  290. dev_dbg(&acm->control->dev,
  291. "%s - calling hangup\n", __func__);
  292. tty_hangup(tty);
  293. }
  294. tty_kref_put(tty);
  295. }
  296. acm->ctrlin = newctrl;
  297. dev_dbg(&acm->control->dev,
  298. "%s - input control lines: dcd%c dsr%c break%c "
  299. "ring%c framing%c parity%c overrun%c\n",
  300. __func__,
  301. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
  302. acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  303. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
  304. acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  305. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
  306. acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  307. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  308. break;
  309. default:
  310. dev_dbg(&acm->control->dev,
  311. "%s - unknown notification %d received: index %d "
  312. "len %d data0 %d data1 %d\n",
  313. __func__,
  314. dr->bNotificationType, dr->wIndex,
  315. dr->wLength, data[0], data[1]);
  316. break;
  317. }
  318. exit:
  319. retval = usb_submit_urb(urb, GFP_ATOMIC);
  320. if (retval)
  321. dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
  322. __func__, retval);
  323. }
  324. static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
  325. {
  326. int res;
  327. if (!test_and_clear_bit(index, &acm->read_urbs_free))
  328. return 0;
  329. dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index);
  330. res = usb_submit_urb(acm->read_urbs[index], mem_flags);
  331. if (res) {
  332. if (res != -EPERM) {
  333. dev_err(&acm->data->dev,
  334. "%s - usb_submit_urb failed: %d\n",
  335. __func__, res);
  336. }
  337. set_bit(index, &acm->read_urbs_free);
  338. return res;
  339. }
  340. return 0;
  341. }
  342. static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
  343. {
  344. int res;
  345. int i;
  346. for (i = 0; i < acm->rx_buflimit; ++i) {
  347. res = acm_submit_read_urb(acm, i, mem_flags);
  348. if (res)
  349. return res;
  350. }
  351. return 0;
  352. }
  353. static void acm_process_read_urb(struct acm *acm, struct urb *urb)
  354. {
  355. struct tty_struct *tty;
  356. if (!urb->actual_length)
  357. return;
  358. tty = tty_port_tty_get(&acm->port);
  359. if (!tty)
  360. return;
  361. tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
  362. tty_flip_buffer_push(tty);
  363. tty_kref_put(tty);
  364. }
  365. static void acm_read_bulk_callback(struct urb *urb)
  366. {
  367. struct acm_rb *rb = urb->context;
  368. struct acm *acm = rb->instance;
  369. unsigned long flags;
  370. dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
  371. rb->index, urb->actual_length);
  372. set_bit(rb->index, &acm->read_urbs_free);
  373. if (!acm->dev) {
  374. dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
  375. return;
  376. }
  377. usb_mark_last_busy(acm->dev);
  378. if (urb->status) {
  379. dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
  380. __func__, urb->status);
  381. return;
  382. }
  383. acm_process_read_urb(acm, urb);
  384. /* throttle device if requested by tty */
  385. spin_lock_irqsave(&acm->read_lock, flags);
  386. acm->throttled = acm->throttle_req;
  387. if (!acm->throttled && !acm->susp_count) {
  388. spin_unlock_irqrestore(&acm->read_lock, flags);
  389. acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
  390. } else {
  391. spin_unlock_irqrestore(&acm->read_lock, flags);
  392. }
  393. }
  394. /* data interface wrote those outgoing bytes */
  395. static void acm_write_bulk(struct urb *urb)
  396. {
  397. struct acm_wb *wb = urb->context;
  398. struct acm *acm = wb->instance;
  399. unsigned long flags;
  400. if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
  401. dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
  402. __func__,
  403. urb->actual_length,
  404. urb->transfer_buffer_length,
  405. urb->status);
  406. spin_lock_irqsave(&acm->write_lock, flags);
  407. acm_write_done(acm, wb);
  408. spin_unlock_irqrestore(&acm->write_lock, flags);
  409. schedule_work(&acm->work);
  410. }
  411. static void acm_softint(struct work_struct *work)
  412. {
  413. struct acm *acm = container_of(work, struct acm, work);
  414. struct tty_struct *tty;
  415. dev_vdbg(&acm->data->dev, "%s\n", __func__);
  416. tty = tty_port_tty_get(&acm->port);
  417. if (!tty)
  418. return;
  419. tty_wakeup(tty);
  420. tty_kref_put(tty);
  421. }
  422. /*
  423. * TTY handlers
  424. */
  425. static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  426. {
  427. struct acm *acm;
  428. int retval;
  429. dev_dbg(tty->dev, "%s\n", __func__);
  430. acm = acm_get_by_index(tty->index);
  431. if (!acm)
  432. return -ENODEV;
  433. retval = tty_init_termios(tty);
  434. if (retval)
  435. goto error_init_termios;
  436. tty->driver_data = acm;
  437. /* Final install (we use the default method) */
  438. tty_driver_kref_get(driver);
  439. tty->count++;
  440. driver->ttys[tty->index] = tty;
  441. return 0;
  442. error_init_termios:
  443. tty_port_put(&acm->port);
  444. return retval;
  445. }
  446. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  447. {
  448. struct acm *acm = tty->driver_data;
  449. dev_dbg(tty->dev, "%s\n", __func__);
  450. return tty_port_open(&acm->port, tty, filp);
  451. }
  452. static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
  453. {
  454. struct acm *acm = container_of(port, struct acm, port);
  455. int retval = -ENODEV;
  456. dev_dbg(&acm->control->dev, "%s\n", __func__);
  457. mutex_lock(&acm->mutex);
  458. if (acm->disconnected)
  459. goto disconnected;
  460. retval = usb_autopm_get_interface(acm->control);
  461. if (retval)
  462. goto error_get_interface;
  463. /*
  464. * FIXME: Why do we need this? Allocating 64K of physically contiguous
  465. * memory is really nasty...
  466. */
  467. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  468. acm->control->needs_remote_wakeup = 1;
  469. acm->ctrlurb->dev = acm->dev;
  470. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  471. dev_err(&acm->control->dev,
  472. "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
  473. goto error_submit_urb;
  474. }
  475. acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS;
  476. if (acm_set_control(acm, acm->ctrlout) < 0 &&
  477. (acm->ctrl_caps & USB_CDC_CAP_LINE))
  478. goto error_set_control;
  479. usb_autopm_put_interface(acm->control);
  480. if (acm_submit_read_urbs(acm, GFP_KERNEL))
  481. goto error_submit_read_urbs;
  482. mutex_unlock(&acm->mutex);
  483. return 0;
  484. error_submit_read_urbs:
  485. acm->ctrlout = 0;
  486. acm_set_control(acm, acm->ctrlout);
  487. error_set_control:
  488. usb_kill_urb(acm->ctrlurb);
  489. error_submit_urb:
  490. usb_autopm_put_interface(acm->control);
  491. error_get_interface:
  492. disconnected:
  493. mutex_unlock(&acm->mutex);
  494. return retval;
  495. }
  496. static void acm_port_destruct(struct tty_port *port)
  497. {
  498. struct acm *acm = container_of(port, struct acm, port);
  499. dev_dbg(&acm->control->dev, "%s\n", __func__);
  500. tty_unregister_device(acm_tty_driver, acm->minor);
  501. acm_release_minor(acm);
  502. usb_put_intf(acm->control);
  503. kfree(acm->country_codes);
  504. kfree(acm);
  505. }
  506. static void acm_port_shutdown(struct tty_port *port)
  507. {
  508. struct acm *acm = container_of(port, struct acm, port);
  509. int i;
  510. dev_dbg(&acm->control->dev, "%s\n", __func__);
  511. mutex_lock(&acm->mutex);
  512. if (!acm->disconnected) {
  513. usb_autopm_get_interface(acm->control);
  514. acm_set_control(acm, acm->ctrlout = 0);
  515. usb_kill_urb(acm->ctrlurb);
  516. for (i = 0; i < ACM_NW; i++)
  517. usb_kill_urb(acm->wb[i].urb);
  518. for (i = 0; i < acm->rx_buflimit; i++)
  519. usb_kill_urb(acm->read_urbs[i]);
  520. acm->control->needs_remote_wakeup = 0;
  521. usb_autopm_put_interface(acm->control);
  522. }
  523. mutex_unlock(&acm->mutex);
  524. }
  525. static void acm_tty_cleanup(struct tty_struct *tty)
  526. {
  527. struct acm *acm = tty->driver_data;
  528. dev_dbg(&acm->control->dev, "%s\n", __func__);
  529. tty_port_put(&acm->port);
  530. }
  531. static void acm_tty_hangup(struct tty_struct *tty)
  532. {
  533. struct acm *acm = tty->driver_data;
  534. dev_dbg(&acm->control->dev, "%s\n", __func__);
  535. tty_port_hangup(&acm->port);
  536. }
  537. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  538. {
  539. struct acm *acm = tty->driver_data;
  540. dev_dbg(&acm->control->dev, "%s\n", __func__);
  541. tty_port_close(&acm->port, tty, filp);
  542. }
  543. static int acm_tty_write(struct tty_struct *tty,
  544. const unsigned char *buf, int count)
  545. {
  546. struct acm *acm = tty->driver_data;
  547. int stat;
  548. unsigned long flags;
  549. int wbn;
  550. struct acm_wb *wb;
  551. if (!count)
  552. return 0;
  553. dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count);
  554. spin_lock_irqsave(&acm->write_lock, flags);
  555. wbn = acm_wb_alloc(acm);
  556. if (wbn < 0) {
  557. spin_unlock_irqrestore(&acm->write_lock, flags);
  558. return 0;
  559. }
  560. wb = &acm->wb[wbn];
  561. count = (count > acm->writesize) ? acm->writesize : count;
  562. dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count);
  563. memcpy(wb->buf, buf, count);
  564. wb->len = count;
  565. spin_unlock_irqrestore(&acm->write_lock, flags);
  566. stat = acm_write_start(acm, wbn);
  567. if (stat < 0)
  568. return stat;
  569. return count;
  570. }
  571. static int acm_tty_write_room(struct tty_struct *tty)
  572. {
  573. struct acm *acm = tty->driver_data;
  574. /*
  575. * Do not let the line discipline to know that we have a reserve,
  576. * or it might get too enthusiastic.
  577. */
  578. return acm_wb_is_avail(acm) ? acm->writesize : 0;
  579. }
  580. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  581. {
  582. struct acm *acm = tty->driver_data;
  583. /*
  584. * if the device was unplugged then any remaining characters fell out
  585. * of the connector ;)
  586. */
  587. if (acm->disconnected)
  588. return 0;
  589. /*
  590. * This is inaccurate (overcounts), but it works.
  591. */
  592. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  593. }
  594. static void acm_tty_throttle(struct tty_struct *tty)
  595. {
  596. struct acm *acm = tty->driver_data;
  597. spin_lock_irq(&acm->read_lock);
  598. acm->throttle_req = 1;
  599. spin_unlock_irq(&acm->read_lock);
  600. }
  601. static void acm_tty_unthrottle(struct tty_struct *tty)
  602. {
  603. struct acm *acm = tty->driver_data;
  604. unsigned int was_throttled;
  605. spin_lock_irq(&acm->read_lock);
  606. was_throttled = acm->throttled;
  607. acm->throttled = 0;
  608. acm->throttle_req = 0;
  609. spin_unlock_irq(&acm->read_lock);
  610. if (was_throttled)
  611. acm_submit_read_urbs(acm, GFP_KERNEL);
  612. }
  613. static int acm_tty_break_ctl(struct tty_struct *tty, int state)
  614. {
  615. struct acm *acm = tty->driver_data;
  616. int retval;
  617. retval = acm_send_break(acm, state ? 0xffff : 0);
  618. if (retval < 0)
  619. dev_dbg(&acm->control->dev, "%s - send break failed\n",
  620. __func__);
  621. return retval;
  622. }
  623. static int acm_tty_tiocmget(struct tty_struct *tty)
  624. {
  625. struct acm *acm = tty->driver_data;
  626. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  627. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  628. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  629. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  630. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  631. TIOCM_CTS;
  632. }
  633. static int acm_tty_tiocmset(struct tty_struct *tty,
  634. unsigned int set, unsigned int clear)
  635. {
  636. struct acm *acm = tty->driver_data;
  637. unsigned int newctrl;
  638. newctrl = acm->ctrlout;
  639. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  640. (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  641. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  642. (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  643. newctrl = (newctrl & ~clear) | set;
  644. if (acm->ctrlout == newctrl)
  645. return 0;
  646. return acm_set_control(acm, acm->ctrlout = newctrl);
  647. }
  648. static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
  649. {
  650. struct serial_struct tmp;
  651. if (!info)
  652. return -EINVAL;
  653. memset(&tmp, 0, sizeof(tmp));
  654. tmp.flags = ASYNC_LOW_LATENCY;
  655. tmp.xmit_fifo_size = acm->writesize;
  656. tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
  657. if (copy_to_user(info, &tmp, sizeof(tmp)))
  658. return -EFAULT;
  659. else
  660. return 0;
  661. }
  662. static int acm_tty_ioctl(struct tty_struct *tty,
  663. unsigned int cmd, unsigned long arg)
  664. {
  665. struct acm *acm = tty->driver_data;
  666. int rv = -ENOIOCTLCMD;
  667. switch (cmd) {
  668. case TIOCGSERIAL: /* gets serial port data */
  669. rv = get_serial_info(acm, (struct serial_struct __user *) arg);
  670. break;
  671. }
  672. return rv;
  673. }
  674. static const __u32 acm_tty_speed[] = {
  675. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  676. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  677. 57600, 115200, 230400, 460800, 500000, 576000,
  678. 921600, 1000000, 1152000, 1500000, 2000000,
  679. 2500000, 3000000, 3500000, 4000000
  680. };
  681. static const __u8 acm_tty_size[] = {
  682. 5, 6, 7, 8
  683. };
  684. static void acm_tty_set_termios(struct tty_struct *tty,
  685. struct ktermios *termios_old)
  686. {
  687. struct acm *acm = tty->driver_data;
  688. struct ktermios *termios = tty->termios;
  689. struct usb_cdc_line_coding newline;
  690. int newctrl = acm->ctrlout;
  691. newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
  692. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  693. newline.bParityType = termios->c_cflag & PARENB ?
  694. (termios->c_cflag & PARODD ? 1 : 2) +
  695. (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  696. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  697. /* FIXME: Needs to clear unsupported bits in the termios */
  698. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  699. if (!newline.dwDTERate) {
  700. newline.dwDTERate = acm->line.dwDTERate;
  701. newctrl &= ~ACM_CTRL_DTR;
  702. } else
  703. newctrl |= ACM_CTRL_DTR;
  704. if (newctrl != acm->ctrlout)
  705. acm_set_control(acm, acm->ctrlout = newctrl);
  706. if (memcmp(&acm->line, &newline, sizeof newline)) {
  707. memcpy(&acm->line, &newline, sizeof newline);
  708. dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
  709. __func__,
  710. le32_to_cpu(newline.dwDTERate),
  711. newline.bCharFormat, newline.bParityType,
  712. newline.bDataBits);
  713. acm_set_line(acm, &acm->line);
  714. }
  715. }
  716. static const struct tty_port_operations acm_port_ops = {
  717. .shutdown = acm_port_shutdown,
  718. .activate = acm_port_activate,
  719. .destruct = acm_port_destruct,
  720. };
  721. /*
  722. * USB probe and disconnect routines.
  723. */
  724. /* Little helpers: write/read buffers free */
  725. static void acm_write_buffers_free(struct acm *acm)
  726. {
  727. int i;
  728. struct acm_wb *wb;
  729. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  730. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
  731. usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
  732. }
  733. static void acm_read_buffers_free(struct acm *acm)
  734. {
  735. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  736. int i;
  737. for (i = 0; i < acm->rx_buflimit; i++)
  738. usb_free_coherent(usb_dev, acm->readsize,
  739. acm->read_buffers[i].base, acm->read_buffers[i].dma);
  740. }
  741. /* Little helper: write buffers allocate */
  742. static int acm_write_buffers_alloc(struct acm *acm)
  743. {
  744. int i;
  745. struct acm_wb *wb;
  746. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  747. wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
  748. &wb->dmah);
  749. if (!wb->buf) {
  750. while (i != 0) {
  751. --i;
  752. --wb;
  753. usb_free_coherent(acm->dev, acm->writesize,
  754. wb->buf, wb->dmah);
  755. }
  756. return -ENOMEM;
  757. }
  758. }
  759. return 0;
  760. }
  761. static int acm_probe(struct usb_interface *intf,
  762. const struct usb_device_id *id)
  763. {
  764. struct usb_cdc_union_desc *union_header = NULL;
  765. struct usb_cdc_country_functional_desc *cfd = NULL;
  766. unsigned char *buffer = intf->altsetting->extra;
  767. int buflen = intf->altsetting->extralen;
  768. struct usb_interface *control_interface;
  769. struct usb_interface *data_interface;
  770. struct usb_endpoint_descriptor *epctrl = NULL;
  771. struct usb_endpoint_descriptor *epread = NULL;
  772. struct usb_endpoint_descriptor *epwrite = NULL;
  773. struct usb_device *usb_dev = interface_to_usbdev(intf);
  774. struct acm *acm;
  775. int minor;
  776. int ctrlsize, readsize;
  777. u8 *buf;
  778. u8 ac_management_function = 0;
  779. u8 call_management_function = 0;
  780. int call_interface_num = -1;
  781. int data_interface_num = -1;
  782. unsigned long quirks;
  783. int num_rx_buf;
  784. int i;
  785. int combined_interfaces = 0;
  786. /* normal quirks */
  787. quirks = (unsigned long)id->driver_info;
  788. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  789. /* handle quirks deadly to normal probing*/
  790. if (quirks == NO_UNION_NORMAL) {
  791. data_interface = usb_ifnum_to_if(usb_dev, 1);
  792. control_interface = usb_ifnum_to_if(usb_dev, 0);
  793. goto skip_normal_probe;
  794. }
  795. /* normal probing*/
  796. if (!buffer) {
  797. dev_err(&intf->dev, "Weird descriptor references\n");
  798. return -EINVAL;
  799. }
  800. if (!buflen) {
  801. if (intf->cur_altsetting->endpoint &&
  802. intf->cur_altsetting->endpoint->extralen &&
  803. intf->cur_altsetting->endpoint->extra) {
  804. dev_dbg(&intf->dev,
  805. "Seeking extra descriptors on endpoint\n");
  806. buflen = intf->cur_altsetting->endpoint->extralen;
  807. buffer = intf->cur_altsetting->endpoint->extra;
  808. } else {
  809. dev_err(&intf->dev,
  810. "Zero length descriptor references\n");
  811. return -EINVAL;
  812. }
  813. }
  814. while (buflen > 0) {
  815. if (buffer[1] != USB_DT_CS_INTERFACE) {
  816. dev_err(&intf->dev, "skipping garbage\n");
  817. goto next_desc;
  818. }
  819. switch (buffer[2]) {
  820. case USB_CDC_UNION_TYPE: /* we've found it */
  821. if (union_header) {
  822. dev_err(&intf->dev, "More than one "
  823. "union descriptor, skipping ...\n");
  824. goto next_desc;
  825. }
  826. union_header = (struct usb_cdc_union_desc *)buffer;
  827. break;
  828. case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
  829. cfd = (struct usb_cdc_country_functional_desc *)buffer;
  830. break;
  831. case USB_CDC_HEADER_TYPE: /* maybe check version */
  832. break; /* for now we ignore it */
  833. case USB_CDC_ACM_TYPE:
  834. ac_management_function = buffer[3];
  835. break;
  836. case USB_CDC_CALL_MANAGEMENT_TYPE:
  837. call_management_function = buffer[3];
  838. call_interface_num = buffer[4];
  839. if ( (quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
  840. dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
  841. break;
  842. default:
  843. /* there are LOTS more CDC descriptors that
  844. * could legitimately be found here.
  845. */
  846. dev_dbg(&intf->dev, "Ignoring descriptor: "
  847. "type %02x, length %d\n",
  848. buffer[2], buffer[0]);
  849. break;
  850. }
  851. next_desc:
  852. buflen -= buffer[0];
  853. buffer += buffer[0];
  854. }
  855. if (!union_header) {
  856. if (call_interface_num > 0) {
  857. dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
  858. /* quirks for Droids MuIn LCD */
  859. if (quirks & NO_DATA_INTERFACE)
  860. data_interface = usb_ifnum_to_if(usb_dev, 0);
  861. else
  862. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  863. control_interface = intf;
  864. } else {
  865. if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
  866. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  867. return -ENODEV;
  868. } else {
  869. dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
  870. combined_interfaces = 1;
  871. control_interface = data_interface = intf;
  872. goto look_for_collapsed_interface;
  873. }
  874. }
  875. } else {
  876. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  877. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  878. if (!control_interface || !data_interface) {
  879. dev_dbg(&intf->dev, "no interfaces\n");
  880. return -ENODEV;
  881. }
  882. }
  883. if (data_interface_num != call_interface_num)
  884. dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
  885. if (control_interface == data_interface) {
  886. /* some broken devices designed for windows work this way */
  887. dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
  888. combined_interfaces = 1;
  889. /* a popular other OS doesn't use it */
  890. quirks |= NO_CAP_LINE;
  891. if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
  892. dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
  893. return -EINVAL;
  894. }
  895. look_for_collapsed_interface:
  896. for (i = 0; i < 3; i++) {
  897. struct usb_endpoint_descriptor *ep;
  898. ep = &data_interface->cur_altsetting->endpoint[i].desc;
  899. if (usb_endpoint_is_int_in(ep))
  900. epctrl = ep;
  901. else if (usb_endpoint_is_bulk_out(ep))
  902. epwrite = ep;
  903. else if (usb_endpoint_is_bulk_in(ep))
  904. epread = ep;
  905. else
  906. return -EINVAL;
  907. }
  908. if (!epctrl || !epread || !epwrite)
  909. return -ENODEV;
  910. else
  911. goto made_compressed_probe;
  912. }
  913. skip_normal_probe:
  914. /*workaround for switched interfaces */
  915. if (data_interface->cur_altsetting->desc.bInterfaceClass
  916. != CDC_DATA_INTERFACE_TYPE) {
  917. if (control_interface->cur_altsetting->desc.bInterfaceClass
  918. == CDC_DATA_INTERFACE_TYPE) {
  919. struct usb_interface *t;
  920. dev_dbg(&intf->dev,
  921. "Your device has switched interfaces.\n");
  922. t = control_interface;
  923. control_interface = data_interface;
  924. data_interface = t;
  925. } else {
  926. return -EINVAL;
  927. }
  928. }
  929. /* Accept probe requests only for the control interface */
  930. if (!combined_interfaces && intf != control_interface)
  931. return -ENODEV;
  932. if (!combined_interfaces && usb_interface_claimed(data_interface)) {
  933. /* valid in this context */
  934. dev_dbg(&intf->dev, "The data interface isn't available\n");
  935. return -EBUSY;
  936. }
  937. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  938. return -EINVAL;
  939. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  940. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  941. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  942. /* workaround for switched endpoints */
  943. if (!usb_endpoint_dir_in(epread)) {
  944. /* descriptors are swapped */
  945. struct usb_endpoint_descriptor *t;
  946. dev_dbg(&intf->dev,
  947. "The data interface has switched endpoints\n");
  948. t = epread;
  949. epread = epwrite;
  950. epwrite = t;
  951. }
  952. made_compressed_probe:
  953. dev_dbg(&intf->dev, "interfaces are valid\n");
  954. acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
  955. if (acm == NULL) {
  956. dev_err(&intf->dev, "out of memory (acm kzalloc)\n");
  957. goto alloc_fail;
  958. }
  959. minor = acm_alloc_minor(acm);
  960. if (minor == ACM_TTY_MINORS) {
  961. dev_err(&intf->dev, "no more free acm devices\n");
  962. kfree(acm);
  963. return -ENODEV;
  964. }
  965. ctrlsize = usb_endpoint_maxp(epctrl);
  966. readsize = usb_endpoint_maxp(epread) *
  967. (quirks == SINGLE_RX_URB ? 1 : 2);
  968. acm->combined_interfaces = combined_interfaces;
  969. acm->writesize = usb_endpoint_maxp(epwrite) * 20;
  970. acm->control = control_interface;
  971. acm->data = data_interface;
  972. acm->minor = minor;
  973. acm->dev = usb_dev;
  974. acm->ctrl_caps = ac_management_function;
  975. if (quirks & NO_CAP_LINE)
  976. acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
  977. acm->ctrlsize = ctrlsize;
  978. acm->readsize = readsize;
  979. acm->rx_buflimit = num_rx_buf;
  980. INIT_WORK(&acm->work, acm_softint);
  981. spin_lock_init(&acm->write_lock);
  982. spin_lock_init(&acm->read_lock);
  983. mutex_init(&acm->mutex);
  984. acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  985. acm->is_int_ep = usb_endpoint_xfer_int(epread);
  986. if (acm->is_int_ep)
  987. acm->bInterval = epread->bInterval;
  988. tty_port_init(&acm->port);
  989. acm->port.ops = &acm_port_ops;
  990. buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  991. if (!buf) {
  992. dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  993. goto alloc_fail2;
  994. }
  995. acm->ctrl_buffer = buf;
  996. if (acm_write_buffers_alloc(acm) < 0) {
  997. dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
  998. goto alloc_fail4;
  999. }
  1000. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  1001. if (!acm->ctrlurb) {
  1002. dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  1003. goto alloc_fail5;
  1004. }
  1005. for (i = 0; i < num_rx_buf; i++) {
  1006. struct acm_rb *rb = &(acm->read_buffers[i]);
  1007. struct urb *urb;
  1008. rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
  1009. &rb->dma);
  1010. if (!rb->base) {
  1011. dev_err(&intf->dev, "out of memory "
  1012. "(read bufs usb_alloc_coherent)\n");
  1013. goto alloc_fail6;
  1014. }
  1015. rb->index = i;
  1016. rb->instance = acm;
  1017. urb = usb_alloc_urb(0, GFP_KERNEL);
  1018. if (!urb) {
  1019. dev_err(&intf->dev,
  1020. "out of memory (read urbs usb_alloc_urb)\n");
  1021. goto alloc_fail6;
  1022. }
  1023. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1024. urb->transfer_dma = rb->dma;
  1025. if (acm->is_int_ep) {
  1026. usb_fill_int_urb(urb, acm->dev,
  1027. acm->rx_endpoint,
  1028. rb->base,
  1029. acm->readsize,
  1030. acm_read_bulk_callback, rb,
  1031. acm->bInterval);
  1032. } else {
  1033. usb_fill_bulk_urb(urb, acm->dev,
  1034. acm->rx_endpoint,
  1035. rb->base,
  1036. acm->readsize,
  1037. acm_read_bulk_callback, rb);
  1038. }
  1039. acm->read_urbs[i] = urb;
  1040. __set_bit(i, &acm->read_urbs_free);
  1041. }
  1042. for (i = 0; i < ACM_NW; i++) {
  1043. struct acm_wb *snd = &(acm->wb[i]);
  1044. snd->urb = usb_alloc_urb(0, GFP_KERNEL);
  1045. if (snd->urb == NULL) {
  1046. dev_err(&intf->dev,
  1047. "out of memory (write urbs usb_alloc_urb)\n");
  1048. goto alloc_fail7;
  1049. }
  1050. if (usb_endpoint_xfer_int(epwrite))
  1051. usb_fill_int_urb(snd->urb, usb_dev,
  1052. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  1053. NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
  1054. else
  1055. usb_fill_bulk_urb(snd->urb, usb_dev,
  1056. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  1057. NULL, acm->writesize, acm_write_bulk, snd);
  1058. snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1059. snd->instance = acm;
  1060. }
  1061. usb_set_intfdata(intf, acm);
  1062. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  1063. if (i < 0)
  1064. goto alloc_fail7;
  1065. if (cfd) { /* export the country data */
  1066. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  1067. if (!acm->country_codes)
  1068. goto skip_countries;
  1069. acm->country_code_size = cfd->bLength - 4;
  1070. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
  1071. cfd->bLength - 4);
  1072. acm->country_rel_date = cfd->iCountryCodeRelDate;
  1073. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  1074. if (i < 0) {
  1075. kfree(acm->country_codes);
  1076. acm->country_codes = NULL;
  1077. acm->country_code_size = 0;
  1078. goto skip_countries;
  1079. }
  1080. i = device_create_file(&intf->dev,
  1081. &dev_attr_iCountryCodeRelDate);
  1082. if (i < 0) {
  1083. device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
  1084. kfree(acm->country_codes);
  1085. acm->country_codes = NULL;
  1086. acm->country_code_size = 0;
  1087. goto skip_countries;
  1088. }
  1089. }
  1090. skip_countries:
  1091. usb_fill_int_urb(acm->ctrlurb, usb_dev,
  1092. usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  1093. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
  1094. /* works around buggy devices */
  1095. epctrl->bInterval ? epctrl->bInterval : 0xff);
  1096. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1097. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  1098. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  1099. acm_set_control(acm, acm->ctrlout);
  1100. acm->line.dwDTERate = cpu_to_le32(9600);
  1101. acm->line.bDataBits = 8;
  1102. acm_set_line(acm, &acm->line);
  1103. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  1104. usb_set_intfdata(data_interface, acm);
  1105. usb_get_intf(control_interface);
  1106. tty_register_device(acm_tty_driver, minor, &control_interface->dev);
  1107. return 0;
  1108. alloc_fail7:
  1109. for (i = 0; i < ACM_NW; i++)
  1110. usb_free_urb(acm->wb[i].urb);
  1111. alloc_fail6:
  1112. for (i = 0; i < num_rx_buf; i++)
  1113. usb_free_urb(acm->read_urbs[i]);
  1114. acm_read_buffers_free(acm);
  1115. usb_free_urb(acm->ctrlurb);
  1116. alloc_fail5:
  1117. acm_write_buffers_free(acm);
  1118. alloc_fail4:
  1119. usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1120. alloc_fail2:
  1121. acm_release_minor(acm);
  1122. kfree(acm);
  1123. alloc_fail:
  1124. return -ENOMEM;
  1125. }
  1126. static void stop_data_traffic(struct acm *acm)
  1127. {
  1128. int i;
  1129. dev_dbg(&acm->control->dev, "%s\n", __func__);
  1130. usb_kill_urb(acm->ctrlurb);
  1131. for (i = 0; i < ACM_NW; i++)
  1132. usb_kill_urb(acm->wb[i].urb);
  1133. for (i = 0; i < acm->rx_buflimit; i++)
  1134. usb_kill_urb(acm->read_urbs[i]);
  1135. cancel_work_sync(&acm->work);
  1136. }
  1137. static void acm_disconnect(struct usb_interface *intf)
  1138. {
  1139. struct acm *acm = usb_get_intfdata(intf);
  1140. struct usb_device *usb_dev = interface_to_usbdev(intf);
  1141. struct tty_struct *tty;
  1142. int i;
  1143. dev_dbg(&intf->dev, "%s\n", __func__);
  1144. /* sibling interface is already cleaning up */
  1145. if (!acm)
  1146. return;
  1147. mutex_lock(&acm->mutex);
  1148. acm->disconnected = true;
  1149. if (acm->country_codes) {
  1150. device_remove_file(&acm->control->dev,
  1151. &dev_attr_wCountryCodes);
  1152. device_remove_file(&acm->control->dev,
  1153. &dev_attr_iCountryCodeRelDate);
  1154. }
  1155. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1156. usb_set_intfdata(acm->control, NULL);
  1157. usb_set_intfdata(acm->data, NULL);
  1158. mutex_unlock(&acm->mutex);
  1159. tty = tty_port_tty_get(&acm->port);
  1160. if (tty) {
  1161. tty_vhangup(tty);
  1162. tty_kref_put(tty);
  1163. }
  1164. stop_data_traffic(acm);
  1165. usb_free_urb(acm->ctrlurb);
  1166. for (i = 0; i < ACM_NW; i++)
  1167. usb_free_urb(acm->wb[i].urb);
  1168. for (i = 0; i < acm->rx_buflimit; i++)
  1169. usb_free_urb(acm->read_urbs[i]);
  1170. acm_write_buffers_free(acm);
  1171. usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1172. acm_read_buffers_free(acm);
  1173. if (!acm->combined_interfaces)
  1174. usb_driver_release_interface(&acm_driver, intf == acm->control ?
  1175. acm->data : acm->control);
  1176. tty_port_put(&acm->port);
  1177. }
  1178. #ifdef CONFIG_PM
  1179. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  1180. {
  1181. struct acm *acm = usb_get_intfdata(intf);
  1182. int cnt;
  1183. if (PMSG_IS_AUTO(message)) {
  1184. int b;
  1185. spin_lock_irq(&acm->write_lock);
  1186. b = acm->transmitting;
  1187. spin_unlock_irq(&acm->write_lock);
  1188. if (b)
  1189. return -EBUSY;
  1190. }
  1191. spin_lock_irq(&acm->read_lock);
  1192. spin_lock(&acm->write_lock);
  1193. cnt = acm->susp_count++;
  1194. spin_unlock(&acm->write_lock);
  1195. spin_unlock_irq(&acm->read_lock);
  1196. if (cnt)
  1197. return 0;
  1198. if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
  1199. stop_data_traffic(acm);
  1200. return 0;
  1201. }
  1202. static int acm_resume(struct usb_interface *intf)
  1203. {
  1204. struct acm *acm = usb_get_intfdata(intf);
  1205. struct acm_wb *wb;
  1206. int rv = 0;
  1207. int cnt;
  1208. spin_lock_irq(&acm->read_lock);
  1209. acm->susp_count -= 1;
  1210. cnt = acm->susp_count;
  1211. spin_unlock_irq(&acm->read_lock);
  1212. if (cnt)
  1213. return 0;
  1214. if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
  1215. rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
  1216. spin_lock_irq(&acm->write_lock);
  1217. if (acm->delayed_wb) {
  1218. wb = acm->delayed_wb;
  1219. acm->delayed_wb = NULL;
  1220. spin_unlock_irq(&acm->write_lock);
  1221. acm_start_wb(acm, wb);
  1222. } else {
  1223. spin_unlock_irq(&acm->write_lock);
  1224. }
  1225. /*
  1226. * delayed error checking because we must
  1227. * do the write path at all cost
  1228. */
  1229. if (rv < 0)
  1230. goto err_out;
  1231. rv = acm_submit_read_urbs(acm, GFP_NOIO);
  1232. }
  1233. err_out:
  1234. return rv;
  1235. }
  1236. static int acm_reset_resume(struct usb_interface *intf)
  1237. {
  1238. struct acm *acm = usb_get_intfdata(intf);
  1239. struct tty_struct *tty;
  1240. if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
  1241. tty = tty_port_tty_get(&acm->port);
  1242. if (tty) {
  1243. tty_hangup(tty);
  1244. tty_kref_put(tty);
  1245. }
  1246. }
  1247. return acm_resume(intf);
  1248. }
  1249. #endif /* CONFIG_PM */
  1250. #define NOKIA_PCSUITE_ACM_INFO(x) \
  1251. USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
  1252. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1253. USB_CDC_ACM_PROTO_VENDOR)
  1254. #define SAMSUNG_PCSUITE_ACM_INFO(x) \
  1255. USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
  1256. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1257. USB_CDC_ACM_PROTO_VENDOR)
  1258. /*
  1259. * USB driver structure.
  1260. */
  1261. static const struct usb_device_id acm_ids[] = {
  1262. /* quirky and broken devices */
  1263. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1264. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1265. },
  1266. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1267. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1268. },
  1269. { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
  1270. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1271. },
  1272. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1273. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1274. },
  1275. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1276. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1277. },
  1278. { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
  1279. .driver_info = SINGLE_RX_URB,
  1280. },
  1281. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1282. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1283. },
  1284. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1285. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1286. },
  1287. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1288. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1289. },
  1290. { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
  1291. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1292. },
  1293. { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
  1294. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1295. },
  1296. { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
  1297. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1298. },
  1299. { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
  1300. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1301. },
  1302. { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
  1303. },
  1304. /* Motorola H24 HSPA module: */
  1305. { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
  1306. { USB_DEVICE(0x22b8, 0x2d92) }, /* modem + diagnostics */
  1307. { USB_DEVICE(0x22b8, 0x2d93) }, /* modem + AT port */
  1308. { USB_DEVICE(0x22b8, 0x2d95) }, /* modem + AT port + diagnostics */
  1309. { USB_DEVICE(0x22b8, 0x2d96) }, /* modem + NMEA */
  1310. { USB_DEVICE(0x22b8, 0x2d97) }, /* modem + diagnostics + NMEA */
  1311. { USB_DEVICE(0x22b8, 0x2d99) }, /* modem + AT port + NMEA */
  1312. { USB_DEVICE(0x22b8, 0x2d9a) }, /* modem + AT port + diagnostics + NMEA */
  1313. { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
  1314. .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
  1315. data interface instead of
  1316. communications interface.
  1317. Maybe we should define a new
  1318. quirk for this. */
  1319. },
  1320. { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
  1321. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1322. },
  1323. { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
  1324. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1325. },
  1326. /* Nokia S60 phones expose two ACM channels. The first is
  1327. * a modem and is picked up by the standard AT-command
  1328. * information below. The second is 'vendor-specific' but
  1329. * is treated as a serial device at the S60 end, so we want
  1330. * to expose it on Linux too. */
  1331. { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
  1332. { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
  1333. { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
  1334. { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
  1335. { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
  1336. { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
  1337. { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
  1338. { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
  1339. { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
  1340. { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
  1341. { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
  1342. { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
  1343. { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
  1344. { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
  1345. { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
  1346. { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
  1347. { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
  1348. { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
  1349. { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
  1350. { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
  1351. { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
  1352. { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
  1353. { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
  1354. { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
  1355. { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
  1356. { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
  1357. { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
  1358. { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
  1359. { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
  1360. { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
  1361. { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
  1362. { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
  1363. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1364. { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
  1365. { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
  1366. { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
  1367. { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
  1368. { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
  1369. { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
  1370. { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
  1371. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1372. { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
  1373. { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
  1374. { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
  1375. { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
  1376. { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
  1377. { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
  1378. { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
  1379. { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
  1380. { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
  1381. { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
  1382. { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
  1383. { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
  1384. { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
  1385. { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
  1386. { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
  1387. { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
  1388. { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
  1389. /* Support for Owen devices */
  1390. { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
  1391. /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
  1392. /* Support Lego NXT using pbLua firmware */
  1393. { USB_DEVICE(0x0694, 0xff00),
  1394. .driver_info = NOT_A_MODEM,
  1395. },
  1396. /* Support for Droids MuIn LCD */
  1397. { USB_DEVICE(0x04d8, 0x000b),
  1398. .driver_info = NO_DATA_INTERFACE,
  1399. },
  1400. /* control interfaces without any protocol set */
  1401. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1402. USB_CDC_PROTO_NONE) },
  1403. /* control interfaces with various AT-command sets */
  1404. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1405. USB_CDC_ACM_PROTO_AT_V25TER) },
  1406. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1407. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1408. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1409. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1410. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1411. USB_CDC_ACM_PROTO_AT_GSM) },
  1412. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1413. USB_CDC_ACM_PROTO_AT_3G) },
  1414. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1415. USB_CDC_ACM_PROTO_AT_CDMA) },
  1416. { }
  1417. };
  1418. MODULE_DEVICE_TABLE(usb, acm_ids);
  1419. static struct usb_driver acm_driver = {
  1420. .name = "cdc_acm",
  1421. .probe = acm_probe,
  1422. .disconnect = acm_disconnect,
  1423. #ifdef CONFIG_PM
  1424. .suspend = acm_suspend,
  1425. .resume = acm_resume,
  1426. .reset_resume = acm_reset_resume,
  1427. #endif
  1428. .id_table = acm_ids,
  1429. #ifdef CONFIG_PM
  1430. .supports_autosuspend = 1,
  1431. #endif
  1432. };
  1433. /*
  1434. * TTY driver structures.
  1435. */
  1436. static const struct tty_operations acm_ops = {
  1437. .install = acm_tty_install,
  1438. .open = acm_tty_open,
  1439. .close = acm_tty_close,
  1440. .cleanup = acm_tty_cleanup,
  1441. .hangup = acm_tty_hangup,
  1442. .write = acm_tty_write,
  1443. .write_room = acm_tty_write_room,
  1444. .ioctl = acm_tty_ioctl,
  1445. .throttle = acm_tty_throttle,
  1446. .unthrottle = acm_tty_unthrottle,
  1447. .chars_in_buffer = acm_tty_chars_in_buffer,
  1448. .break_ctl = acm_tty_break_ctl,
  1449. .set_termios = acm_tty_set_termios,
  1450. .tiocmget = acm_tty_tiocmget,
  1451. .tiocmset = acm_tty_tiocmset,
  1452. };
  1453. /*
  1454. * Init / exit.
  1455. */
  1456. static int __init acm_init(void)
  1457. {
  1458. int retval;
  1459. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1460. if (!acm_tty_driver)
  1461. return -ENOMEM;
  1462. acm_tty_driver->owner = THIS_MODULE,
  1463. acm_tty_driver->driver_name = "acm",
  1464. acm_tty_driver->name = "ttyACM",
  1465. acm_tty_driver->major = ACM_TTY_MAJOR,
  1466. acm_tty_driver->minor_start = 0,
  1467. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1468. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1469. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1470. acm_tty_driver->init_termios = tty_std_termios;
  1471. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
  1472. HUPCL | CLOCAL;
  1473. tty_set_operations(acm_tty_driver, &acm_ops);
  1474. retval = tty_register_driver(acm_tty_driver);
  1475. if (retval) {
  1476. put_tty_driver(acm_tty_driver);
  1477. return retval;
  1478. }
  1479. retval = usb_register(&acm_driver);
  1480. if (retval) {
  1481. tty_unregister_driver(acm_tty_driver);
  1482. put_tty_driver(acm_tty_driver);
  1483. return retval;
  1484. }
  1485. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1486. return 0;
  1487. }
  1488. static void __exit acm_exit(void)
  1489. {
  1490. usb_deregister(&acm_driver);
  1491. tty_unregister_driver(acm_tty_driver);
  1492. put_tty_driver(acm_tty_driver);
  1493. }
  1494. module_init(acm_init);
  1495. module_exit(acm_exit);
  1496. MODULE_AUTHOR(DRIVER_AUTHOR);
  1497. MODULE_DESCRIPTION(DRIVER_DESC);
  1498. MODULE_LICENSE("GPL");
  1499. MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);