cdc-acm.c 47 KB

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