hso.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  7. * <ajb@spheresystems.co.uk>
  8. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  9. * Copyright (C) 2008 Novell, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  23. * USA
  24. *
  25. *
  26. *****************************************************************************/
  27. /******************************************************************************
  28. *
  29. * Description of the device:
  30. *
  31. * Interface 0: Contains the IP network interface on the bulk end points.
  32. * The multiplexed serial ports are using the interrupt and
  33. * control endpoints.
  34. * Interrupt contains a bitmap telling which multiplexed
  35. * serialport needs servicing.
  36. *
  37. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  38. * port is opened, as this have a huge impact on the network port
  39. * throughput.
  40. *
  41. * Interface 2: Standard modem interface - circuit switched interface, this
  42. * can be used to make a standard ppp connection however it
  43. * should not be used in conjunction with the IP network interface
  44. * enabled for USB performance reasons i.e. if using this set
  45. * ideally disable_net=1.
  46. *
  47. *****************************************************************************/
  48. #include <linux/sched.h>
  49. #include <linux/slab.h>
  50. #include <linux/init.h>
  51. #include <linux/delay.h>
  52. #include <linux/netdevice.h>
  53. #include <linux/module.h>
  54. #include <linux/ethtool.h>
  55. #include <linux/usb.h>
  56. #include <linux/timer.h>
  57. #include <linux/tty.h>
  58. #include <linux/tty_driver.h>
  59. #include <linux/tty_flip.h>
  60. #include <linux/kmod.h>
  61. #include <linux/rfkill.h>
  62. #include <linux/ip.h>
  63. #include <linux/uaccess.h>
  64. #include <linux/usb/cdc.h>
  65. #include <net/arp.h>
  66. #include <asm/byteorder.h>
  67. #include <linux/serial_core.h>
  68. #include <linux/serial.h>
  69. #define DRIVER_VERSION "1.2"
  70. #define MOD_AUTHOR "Option Wireless"
  71. #define MOD_DESCRIPTION "USB High Speed Option driver"
  72. #define MOD_LICENSE "GPL"
  73. #define HSO_MAX_NET_DEVICES 10
  74. #define HSO__MAX_MTU 2048
  75. #define DEFAULT_MTU 1500
  76. #define DEFAULT_MRU 1500
  77. #define CTRL_URB_RX_SIZE 1024
  78. #define CTRL_URB_TX_SIZE 64
  79. #define BULK_URB_RX_SIZE 4096
  80. #define BULK_URB_TX_SIZE 8192
  81. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  82. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  83. #define MUX_BULK_RX_BUF_COUNT 4
  84. #define USB_TYPE_OPTION_VENDOR 0x20
  85. /* These definitions are used with the struct hso_net flags element */
  86. /* - use *_bit operations on it. (bit indices not values.) */
  87. #define HSO_NET_RUNNING 0
  88. #define HSO_NET_TX_TIMEOUT (HZ*10)
  89. #define HSO_SERIAL_MAGIC 0x48534f31
  90. /* Number of ttys to handle */
  91. #define HSO_SERIAL_TTY_MINORS 256
  92. #define MAX_RX_URBS 2
  93. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  94. {
  95. if (tty)
  96. return tty->driver_data;
  97. return NULL;
  98. }
  99. /*****************************************************************************/
  100. /* Debugging functions */
  101. /*****************************************************************************/
  102. #define D__(lvl_, fmt, arg...) \
  103. do { \
  104. printk(lvl_ "[%d:%s]: " fmt "\n", \
  105. __LINE__, __func__, ## arg); \
  106. } while (0)
  107. #define D_(lvl, args...) \
  108. do { \
  109. if (lvl & debug) \
  110. D__(KERN_INFO, args); \
  111. } while (0)
  112. #define D1(args...) D_(0x01, ##args)
  113. #define D2(args...) D_(0x02, ##args)
  114. #define D3(args...) D_(0x04, ##args)
  115. #define D4(args...) D_(0x08, ##args)
  116. #define D5(args...) D_(0x10, ##args)
  117. /*****************************************************************************/
  118. /* Enumerators */
  119. /*****************************************************************************/
  120. enum pkt_parse_state {
  121. WAIT_IP,
  122. WAIT_DATA,
  123. WAIT_SYNC
  124. };
  125. /*****************************************************************************/
  126. /* Structs */
  127. /*****************************************************************************/
  128. struct hso_shared_int {
  129. struct usb_endpoint_descriptor *intr_endp;
  130. void *shared_intr_buf;
  131. struct urb *shared_intr_urb;
  132. struct usb_device *usb;
  133. int use_count;
  134. int ref_count;
  135. struct mutex shared_int_lock;
  136. };
  137. struct hso_net {
  138. struct hso_device *parent;
  139. struct net_device *net;
  140. struct rfkill *rfkill;
  141. struct usb_endpoint_descriptor *in_endp;
  142. struct usb_endpoint_descriptor *out_endp;
  143. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  144. struct urb *mux_bulk_tx_urb;
  145. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  146. void *mux_bulk_tx_buf;
  147. struct sk_buff *skb_rx_buf;
  148. struct sk_buff *skb_tx_buf;
  149. enum pkt_parse_state rx_parse_state;
  150. spinlock_t net_lock;
  151. unsigned short rx_buf_size;
  152. unsigned short rx_buf_missing;
  153. struct iphdr rx_ip_hdr;
  154. unsigned long flags;
  155. };
  156. enum rx_ctrl_state{
  157. RX_IDLE,
  158. RX_SENT,
  159. RX_PENDING
  160. };
  161. #define BM_REQUEST_TYPE (0xa1)
  162. #define B_NOTIFICATION (0x20)
  163. #define W_VALUE (0x0)
  164. #define W_INDEX (0x2)
  165. #define W_LENGTH (0x2)
  166. #define B_OVERRUN (0x1<<6)
  167. #define B_PARITY (0x1<<5)
  168. #define B_FRAMING (0x1<<4)
  169. #define B_RING_SIGNAL (0x1<<3)
  170. #define B_BREAK (0x1<<2)
  171. #define B_TX_CARRIER (0x1<<1)
  172. #define B_RX_CARRIER (0x1<<0)
  173. struct hso_serial_state_notification {
  174. u8 bmRequestType;
  175. u8 bNotification;
  176. u16 wValue;
  177. u16 wIndex;
  178. u16 wLength;
  179. u16 UART_state_bitmap;
  180. } __attribute__((packed));
  181. struct hso_tiocmget {
  182. struct mutex mutex;
  183. wait_queue_head_t waitq;
  184. int intr_completed;
  185. struct usb_endpoint_descriptor *endp;
  186. struct urb *urb;
  187. struct hso_serial_state_notification serial_state_notification;
  188. u16 prev_UART_state_bitmap;
  189. struct uart_icount icount;
  190. };
  191. struct hso_serial {
  192. struct hso_device *parent;
  193. int magic;
  194. u8 minor;
  195. struct hso_shared_int *shared_int;
  196. /* rx/tx urb could be either a bulk urb or a control urb depending
  197. on which serial port it is used on. */
  198. struct urb *rx_urb[MAX_RX_URBS];
  199. u8 num_rx_urbs;
  200. u8 *rx_data[MAX_RX_URBS];
  201. u16 rx_data_length; /* should contain allocated length */
  202. struct urb *tx_urb;
  203. u8 *tx_data;
  204. u8 *tx_buffer;
  205. u16 tx_data_length; /* should contain allocated length */
  206. u16 tx_data_count;
  207. u16 tx_buffer_count;
  208. struct usb_ctrlrequest ctrl_req_tx;
  209. struct usb_ctrlrequest ctrl_req_rx;
  210. struct usb_endpoint_descriptor *in_endp;
  211. struct usb_endpoint_descriptor *out_endp;
  212. enum rx_ctrl_state rx_state;
  213. u8 rts_state;
  214. u8 dtr_state;
  215. unsigned tx_urb_used:1;
  216. /* from usb_serial_port */
  217. struct tty_struct *tty;
  218. int open_count;
  219. spinlock_t serial_lock;
  220. int (*write_data) (struct hso_serial *serial);
  221. struct hso_tiocmget *tiocmget;
  222. /* Hacks required to get flow control
  223. * working on the serial receive buffers
  224. * so as not to drop characters on the floor.
  225. */
  226. int curr_rx_urb_idx;
  227. u16 curr_rx_urb_offset;
  228. u8 rx_urb_filled[MAX_RX_URBS];
  229. struct tasklet_struct unthrottle_tasklet;
  230. struct work_struct retry_unthrottle_workqueue;
  231. };
  232. struct hso_mutex_t {
  233. struct mutex mutex;
  234. u8 allocated;
  235. };
  236. struct hso_device {
  237. union {
  238. struct hso_serial *dev_serial;
  239. struct hso_net *dev_net;
  240. } port_data;
  241. u32 port_spec;
  242. u8 is_active;
  243. u8 usb_gone;
  244. struct work_struct async_get_intf;
  245. struct work_struct async_put_intf;
  246. struct usb_device *usb;
  247. struct usb_interface *interface;
  248. struct device *dev;
  249. struct kref ref;
  250. struct hso_mutex_t *mutex;
  251. };
  252. /* Type of interface */
  253. #define HSO_INTF_MASK 0xFF00
  254. #define HSO_INTF_MUX 0x0100
  255. #define HSO_INTF_BULK 0x0200
  256. /* Type of port */
  257. #define HSO_PORT_MASK 0xFF
  258. #define HSO_PORT_NO_PORT 0x0
  259. #define HSO_PORT_CONTROL 0x1
  260. #define HSO_PORT_APP 0x2
  261. #define HSO_PORT_GPS 0x3
  262. #define HSO_PORT_PCSC 0x4
  263. #define HSO_PORT_APP2 0x5
  264. #define HSO_PORT_GPS_CONTROL 0x6
  265. #define HSO_PORT_MSD 0x7
  266. #define HSO_PORT_VOICE 0x8
  267. #define HSO_PORT_DIAG2 0x9
  268. #define HSO_PORT_DIAG 0x10
  269. #define HSO_PORT_MODEM 0x11
  270. #define HSO_PORT_NETWORK 0x12
  271. /* Additional device info */
  272. #define HSO_INFO_MASK 0xFF000000
  273. #define HSO_INFO_CRC_BUG 0x01000000
  274. /*****************************************************************************/
  275. /* Prototypes */
  276. /*****************************************************************************/
  277. /* Serial driver functions */
  278. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  279. unsigned int set, unsigned int clear);
  280. static void ctrl_callback(struct urb *urb);
  281. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  282. static void hso_kick_transmit(struct hso_serial *serial);
  283. /* Helper functions */
  284. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  285. struct usb_device *usb, gfp_t gfp);
  286. static void log_usb_status(int status, const char *function);
  287. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  288. int type, int dir);
  289. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  290. static void hso_free_interface(struct usb_interface *intf);
  291. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  292. static int hso_stop_serial_device(struct hso_device *hso_dev);
  293. static int hso_start_net_device(struct hso_device *hso_dev);
  294. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  295. static int hso_stop_net_device(struct hso_device *hso_dev);
  296. static void hso_serial_ref_free(struct kref *ref);
  297. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  298. static int hso_mux_serial_read(struct hso_serial *serial);
  299. static void async_get_intf(struct work_struct *data);
  300. static void async_put_intf(struct work_struct *data);
  301. static int hso_put_activity(struct hso_device *hso_dev);
  302. static int hso_get_activity(struct hso_device *hso_dev);
  303. static void tiocmget_intr_callback(struct urb *urb);
  304. /*****************************************************************************/
  305. /* Helping functions */
  306. /*****************************************************************************/
  307. /* #define DEBUG */
  308. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  309. {
  310. return hso_dev->port_data.dev_net;
  311. }
  312. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  313. {
  314. return hso_dev->port_data.dev_serial;
  315. }
  316. /* Debugging functions */
  317. #ifdef DEBUG
  318. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  319. unsigned int len)
  320. {
  321. static char name[255];
  322. sprintf(name, "hso[%d:%s]", line_count, func_name);
  323. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  324. }
  325. #define DUMP(buf_, len_) \
  326. dbg_dump(__LINE__, __func__, buf_, len_)
  327. #define DUMP1(buf_, len_) \
  328. do { \
  329. if (0x01 & debug) \
  330. DUMP(buf_, len_); \
  331. } while (0)
  332. #else
  333. #define DUMP(buf_, len_)
  334. #define DUMP1(buf_, len_)
  335. #endif
  336. /* module parameters */
  337. static int debug;
  338. static int tty_major;
  339. static int disable_net;
  340. /* driver info */
  341. static const char driver_name[] = "hso";
  342. static const char tty_filename[] = "ttyHS";
  343. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  344. /* the usb driver itself (registered in hso_init) */
  345. static struct usb_driver hso_driver;
  346. /* serial structures */
  347. static struct tty_driver *tty_drv;
  348. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  349. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  350. static spinlock_t serial_table_lock;
  351. static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
  352. static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
  353. /* hso_mutex_table has to be declared statically as hso_device
  354. * is freed in hso_serial_open & hso_serial_close while
  355. * the mutex is still in use.
  356. */
  357. #define HSO_NUM_MUTEXES (HSO_SERIAL_TTY_MINORS+HSO_MAX_NET_DEVICES)
  358. static struct hso_mutex_t hso_mutex_table[HSO_NUM_MUTEXES];
  359. static spinlock_t hso_mutex_lock;
  360. static const s32 default_port_spec[] = {
  361. HSO_INTF_MUX | HSO_PORT_NETWORK,
  362. HSO_INTF_BULK | HSO_PORT_DIAG,
  363. HSO_INTF_BULK | HSO_PORT_MODEM,
  364. 0
  365. };
  366. static const s32 icon321_port_spec[] = {
  367. HSO_INTF_MUX | HSO_PORT_NETWORK,
  368. HSO_INTF_BULK | HSO_PORT_DIAG2,
  369. HSO_INTF_BULK | HSO_PORT_MODEM,
  370. HSO_INTF_BULK | HSO_PORT_DIAG,
  371. 0
  372. };
  373. #define default_port_device(vendor, product) \
  374. USB_DEVICE(vendor, product), \
  375. .driver_info = (kernel_ulong_t)default_port_spec
  376. #define icon321_port_device(vendor, product) \
  377. USB_DEVICE(vendor, product), \
  378. .driver_info = (kernel_ulong_t)icon321_port_spec
  379. /* list of devices we support */
  380. static const struct usb_device_id hso_ids[] = {
  381. {default_port_device(0x0af0, 0x6711)},
  382. {default_port_device(0x0af0, 0x6731)},
  383. {default_port_device(0x0af0, 0x6751)},
  384. {default_port_device(0x0af0, 0x6771)},
  385. {default_port_device(0x0af0, 0x6791)},
  386. {default_port_device(0x0af0, 0x6811)},
  387. {default_port_device(0x0af0, 0x6911)},
  388. {default_port_device(0x0af0, 0x6951)},
  389. {default_port_device(0x0af0, 0x6971)},
  390. {default_port_device(0x0af0, 0x7011)},
  391. {default_port_device(0x0af0, 0x7031)},
  392. {default_port_device(0x0af0, 0x7051)},
  393. {default_port_device(0x0af0, 0x7071)},
  394. {default_port_device(0x0af0, 0x7111)},
  395. {default_port_device(0x0af0, 0x7211)},
  396. {default_port_device(0x0af0, 0x7251)},
  397. {default_port_device(0x0af0, 0x7271)},
  398. {default_port_device(0x0af0, 0x7311)},
  399. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  400. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  401. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  402. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  403. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  404. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  405. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  406. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  407. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  408. {USB_DEVICE(0x0af0, 0x7701)},
  409. {USB_DEVICE(0x0af0, 0x7801)},
  410. {USB_DEVICE(0x0af0, 0x7901)},
  411. {USB_DEVICE(0x0af0, 0x7361)},
  412. {icon321_port_device(0x0af0, 0xd051)},
  413. {}
  414. };
  415. MODULE_DEVICE_TABLE(usb, hso_ids);
  416. /* Sysfs attribute */
  417. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  418. struct device_attribute *attr,
  419. char *buf)
  420. {
  421. struct hso_device *hso_dev = dev->driver_data;
  422. char *port_name;
  423. if (!hso_dev)
  424. return 0;
  425. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  426. case HSO_PORT_CONTROL:
  427. port_name = "Control";
  428. break;
  429. case HSO_PORT_APP:
  430. port_name = "Application";
  431. break;
  432. case HSO_PORT_APP2:
  433. port_name = "Application2";
  434. break;
  435. case HSO_PORT_GPS:
  436. port_name = "GPS";
  437. break;
  438. case HSO_PORT_GPS_CONTROL:
  439. port_name = "GPS Control";
  440. break;
  441. case HSO_PORT_PCSC:
  442. port_name = "PCSC";
  443. break;
  444. case HSO_PORT_DIAG:
  445. port_name = "Diagnostic";
  446. break;
  447. case HSO_PORT_DIAG2:
  448. port_name = "Diagnostic2";
  449. break;
  450. case HSO_PORT_MODEM:
  451. port_name = "Modem";
  452. break;
  453. case HSO_PORT_NETWORK:
  454. port_name = "Network";
  455. break;
  456. default:
  457. port_name = "Unknown";
  458. break;
  459. }
  460. return sprintf(buf, "%s\n", port_name);
  461. }
  462. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  463. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  464. {
  465. int idx;
  466. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  467. if (serial->rx_urb[idx] == urb)
  468. return idx;
  469. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  470. return -1;
  471. }
  472. /* converts mux value to a port spec value */
  473. static u32 hso_mux_to_port(int mux)
  474. {
  475. u32 result;
  476. switch (mux) {
  477. case 0x1:
  478. result = HSO_PORT_CONTROL;
  479. break;
  480. case 0x2:
  481. result = HSO_PORT_APP;
  482. break;
  483. case 0x4:
  484. result = HSO_PORT_PCSC;
  485. break;
  486. case 0x8:
  487. result = HSO_PORT_GPS;
  488. break;
  489. case 0x10:
  490. result = HSO_PORT_APP2;
  491. break;
  492. default:
  493. result = HSO_PORT_NO_PORT;
  494. }
  495. return result;
  496. }
  497. /* converts port spec value to a mux value */
  498. static u32 hso_port_to_mux(int port)
  499. {
  500. u32 result;
  501. switch (port & HSO_PORT_MASK) {
  502. case HSO_PORT_CONTROL:
  503. result = 0x0;
  504. break;
  505. case HSO_PORT_APP:
  506. result = 0x1;
  507. break;
  508. case HSO_PORT_PCSC:
  509. result = 0x2;
  510. break;
  511. case HSO_PORT_GPS:
  512. result = 0x3;
  513. break;
  514. case HSO_PORT_APP2:
  515. result = 0x4;
  516. break;
  517. default:
  518. result = 0x0;
  519. }
  520. return result;
  521. }
  522. static struct hso_serial *get_serial_by_shared_int_and_type(
  523. struct hso_shared_int *shared_int,
  524. int mux)
  525. {
  526. int i, port;
  527. port = hso_mux_to_port(mux);
  528. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  529. if (serial_table[i]
  530. && (dev2ser(serial_table[i])->shared_int == shared_int)
  531. && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  532. return dev2ser(serial_table[i]);
  533. }
  534. }
  535. return NULL;
  536. }
  537. static struct hso_serial *get_serial_by_index(unsigned index)
  538. {
  539. struct hso_serial *serial = NULL;
  540. unsigned long flags;
  541. spin_lock_irqsave(&serial_table_lock, flags);
  542. if (serial_table[index])
  543. serial = dev2ser(serial_table[index]);
  544. spin_unlock_irqrestore(&serial_table_lock, flags);
  545. return serial;
  546. }
  547. static int get_free_serial_index(void)
  548. {
  549. int index;
  550. unsigned long flags;
  551. spin_lock_irqsave(&serial_table_lock, flags);
  552. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  553. if (serial_table[index] == NULL) {
  554. spin_unlock_irqrestore(&serial_table_lock, flags);
  555. return index;
  556. }
  557. }
  558. spin_unlock_irqrestore(&serial_table_lock, flags);
  559. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  560. return -1;
  561. }
  562. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  563. {
  564. unsigned long flags;
  565. spin_lock_irqsave(&serial_table_lock, flags);
  566. if (serial)
  567. serial_table[index] = serial->parent;
  568. else
  569. serial_table[index] = NULL;
  570. spin_unlock_irqrestore(&serial_table_lock, flags);
  571. }
  572. static struct hso_mutex_t *hso_get_free_mutex(void)
  573. {
  574. int index;
  575. struct hso_mutex_t *curr_hso_mutex;
  576. spin_lock(&hso_mutex_lock);
  577. for (index = 0; index < HSO_NUM_MUTEXES; index++) {
  578. curr_hso_mutex = &hso_mutex_table[index];
  579. if (!curr_hso_mutex->allocated) {
  580. curr_hso_mutex->allocated = 1;
  581. spin_unlock(&hso_mutex_lock);
  582. return curr_hso_mutex;
  583. }
  584. }
  585. printk(KERN_ERR "BUG %s: no free hso_mutexs devices in table\n"
  586. , __func__);
  587. spin_unlock(&hso_mutex_lock);
  588. return NULL;
  589. }
  590. static void hso_free_mutex(struct hso_mutex_t *mutex)
  591. {
  592. spin_lock(&hso_mutex_lock);
  593. mutex->allocated = 0;
  594. spin_unlock(&hso_mutex_lock);
  595. }
  596. /* log a meaningful explanation of an USB status */
  597. static void log_usb_status(int status, const char *function)
  598. {
  599. char *explanation;
  600. switch (status) {
  601. case -ENODEV:
  602. explanation = "no device";
  603. break;
  604. case -ENOENT:
  605. explanation = "endpoint not enabled";
  606. break;
  607. case -EPIPE:
  608. explanation = "endpoint stalled";
  609. break;
  610. case -ENOSPC:
  611. explanation = "not enough bandwidth";
  612. break;
  613. case -ESHUTDOWN:
  614. explanation = "device disabled";
  615. break;
  616. case -EHOSTUNREACH:
  617. explanation = "device suspended";
  618. break;
  619. case -EINVAL:
  620. case -EAGAIN:
  621. case -EFBIG:
  622. case -EMSGSIZE:
  623. explanation = "internal error";
  624. break;
  625. default:
  626. explanation = "unknown status";
  627. break;
  628. }
  629. D1("%s: received USB status - %s (%d)", function, explanation, status);
  630. }
  631. /* Network interface functions */
  632. /* called when net interface is brought up by ifconfig */
  633. static int hso_net_open(struct net_device *net)
  634. {
  635. struct hso_net *odev = netdev_priv(net);
  636. unsigned long flags = 0;
  637. if (!odev) {
  638. dev_err(&net->dev, "No net device !\n");
  639. return -ENODEV;
  640. }
  641. odev->skb_tx_buf = NULL;
  642. /* setup environment */
  643. spin_lock_irqsave(&odev->net_lock, flags);
  644. odev->rx_parse_state = WAIT_IP;
  645. odev->rx_buf_size = 0;
  646. odev->rx_buf_missing = sizeof(struct iphdr);
  647. spin_unlock_irqrestore(&odev->net_lock, flags);
  648. hso_start_net_device(odev->parent);
  649. /* We are up and running. */
  650. set_bit(HSO_NET_RUNNING, &odev->flags);
  651. /* Tell the kernel we are ready to start receiving from it */
  652. netif_start_queue(net);
  653. return 0;
  654. }
  655. /* called when interface is brought down by ifconfig */
  656. static int hso_net_close(struct net_device *net)
  657. {
  658. struct hso_net *odev = netdev_priv(net);
  659. /* we don't need the queue anymore */
  660. netif_stop_queue(net);
  661. /* no longer running */
  662. clear_bit(HSO_NET_RUNNING, &odev->flags);
  663. hso_stop_net_device(odev->parent);
  664. /* done */
  665. return 0;
  666. }
  667. /* USB tells is xmit done, we should start the netqueue again */
  668. static void write_bulk_callback(struct urb *urb)
  669. {
  670. struct hso_net *odev = urb->context;
  671. int status = urb->status;
  672. /* Sanity check */
  673. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  674. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  675. return;
  676. }
  677. /* Do we still have a valid kernel network device? */
  678. if (!netif_device_present(odev->net)) {
  679. dev_err(&urb->dev->dev, "%s: net device not present\n",
  680. __func__);
  681. return;
  682. }
  683. /* log status, but don't act on it, we don't need to resubmit anything
  684. * anyhow */
  685. if (status)
  686. log_usb_status(status, __func__);
  687. hso_put_activity(odev->parent);
  688. /* Tell the network interface we are ready for another frame */
  689. netif_wake_queue(odev->net);
  690. }
  691. /* called by kernel when we need to transmit a packet */
  692. static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
  693. {
  694. struct hso_net *odev = netdev_priv(net);
  695. int result;
  696. /* Tell the kernel, "No more frames 'til we are done with this one." */
  697. netif_stop_queue(net);
  698. if (hso_get_activity(odev->parent) == -EAGAIN) {
  699. odev->skb_tx_buf = skb;
  700. return 0;
  701. }
  702. /* log if asked */
  703. DUMP1(skb->data, skb->len);
  704. /* Copy it from kernel memory to OUR memory */
  705. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  706. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  707. /* Fill in the URB for shipping it out. */
  708. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  709. odev->parent->usb,
  710. usb_sndbulkpipe(odev->parent->usb,
  711. odev->out_endp->
  712. bEndpointAddress & 0x7F),
  713. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  714. odev);
  715. /* Deal with the Zero Length packet problem, I hope */
  716. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  717. /* Send the URB on its merry way. */
  718. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  719. if (result) {
  720. dev_warn(&odev->parent->interface->dev,
  721. "failed mux_bulk_tx_urb %d", result);
  722. net->stats.tx_errors++;
  723. netif_start_queue(net);
  724. } else {
  725. net->stats.tx_packets++;
  726. net->stats.tx_bytes += skb->len;
  727. /* And tell the kernel when the last transmit started. */
  728. net->trans_start = jiffies;
  729. }
  730. dev_kfree_skb(skb);
  731. /* we're done */
  732. return result;
  733. }
  734. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  735. {
  736. struct hso_net *odev = netdev_priv(net);
  737. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  738. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  739. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  740. }
  741. static struct ethtool_ops ops = {
  742. .get_drvinfo = hso_get_drvinfo,
  743. .get_link = ethtool_op_get_link
  744. };
  745. /* called when a packet did not ack after watchdogtimeout */
  746. static void hso_net_tx_timeout(struct net_device *net)
  747. {
  748. struct hso_net *odev = netdev_priv(net);
  749. if (!odev)
  750. return;
  751. /* Tell syslog we are hosed. */
  752. dev_warn(&net->dev, "Tx timed out.\n");
  753. /* Tear the waiting frame off the list */
  754. if (odev->mux_bulk_tx_urb
  755. && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  756. usb_unlink_urb(odev->mux_bulk_tx_urb);
  757. /* Update statistics */
  758. net->stats.tx_errors++;
  759. }
  760. /* make a real packet from the received USB buffer */
  761. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  762. unsigned int count, unsigned char is_eop)
  763. {
  764. unsigned short temp_bytes;
  765. unsigned short buffer_offset = 0;
  766. unsigned short frame_len;
  767. unsigned char *tmp_rx_buf;
  768. /* log if needed */
  769. D1("Rx %d bytes", count);
  770. DUMP(ip_pkt, min(128, (int)count));
  771. while (count) {
  772. switch (odev->rx_parse_state) {
  773. case WAIT_IP:
  774. /* waiting for IP header. */
  775. /* wanted bytes - size of ip header */
  776. temp_bytes =
  777. (count <
  778. odev->rx_buf_missing) ? count : odev->
  779. rx_buf_missing;
  780. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  781. odev->rx_buf_size, ip_pkt + buffer_offset,
  782. temp_bytes);
  783. odev->rx_buf_size += temp_bytes;
  784. buffer_offset += temp_bytes;
  785. odev->rx_buf_missing -= temp_bytes;
  786. count -= temp_bytes;
  787. if (!odev->rx_buf_missing) {
  788. /* header is complete allocate an sk_buffer and
  789. * continue to WAIT_DATA */
  790. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  791. if ((frame_len > DEFAULT_MRU) ||
  792. (frame_len < sizeof(struct iphdr))) {
  793. dev_err(&odev->net->dev,
  794. "Invalid frame (%d) length\n",
  795. frame_len);
  796. odev->rx_parse_state = WAIT_SYNC;
  797. continue;
  798. }
  799. /* Allocate an sk_buff */
  800. odev->skb_rx_buf = dev_alloc_skb(frame_len);
  801. if (!odev->skb_rx_buf) {
  802. /* We got no receive buffer. */
  803. D1("could not allocate memory");
  804. odev->rx_parse_state = WAIT_SYNC;
  805. return;
  806. }
  807. /* Here's where it came from */
  808. odev->skb_rx_buf->dev = odev->net;
  809. /* Copy what we got so far. make room for iphdr
  810. * after tail. */
  811. tmp_rx_buf =
  812. skb_put(odev->skb_rx_buf,
  813. sizeof(struct iphdr));
  814. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  815. sizeof(struct iphdr));
  816. /* ETH_HLEN */
  817. odev->rx_buf_size = sizeof(struct iphdr);
  818. /* Filip actually use .tot_len */
  819. odev->rx_buf_missing =
  820. frame_len - sizeof(struct iphdr);
  821. odev->rx_parse_state = WAIT_DATA;
  822. }
  823. break;
  824. case WAIT_DATA:
  825. temp_bytes = (count < odev->rx_buf_missing)
  826. ? count : odev->rx_buf_missing;
  827. /* Copy the rest of the bytes that are left in the
  828. * buffer into the waiting sk_buf. */
  829. /* Make room for temp_bytes after tail. */
  830. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  831. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  832. odev->rx_buf_missing -= temp_bytes;
  833. count -= temp_bytes;
  834. buffer_offset += temp_bytes;
  835. odev->rx_buf_size += temp_bytes;
  836. if (!odev->rx_buf_missing) {
  837. /* Packet is complete. Inject into stack. */
  838. /* We have IP packet here */
  839. odev->skb_rx_buf->protocol =
  840. __constant_htons(ETH_P_IP);
  841. /* don't check it */
  842. odev->skb_rx_buf->ip_summed =
  843. CHECKSUM_UNNECESSARY;
  844. skb_reset_mac_header(odev->skb_rx_buf);
  845. /* Ship it off to the kernel */
  846. netif_rx(odev->skb_rx_buf);
  847. /* No longer our buffer. */
  848. odev->skb_rx_buf = NULL;
  849. /* update out statistics */
  850. odev->net->stats.rx_packets++;
  851. odev->net->stats.rx_bytes += odev->rx_buf_size;
  852. odev->rx_buf_size = 0;
  853. odev->rx_buf_missing = sizeof(struct iphdr);
  854. odev->rx_parse_state = WAIT_IP;
  855. }
  856. break;
  857. case WAIT_SYNC:
  858. D1(" W_S");
  859. count = 0;
  860. break;
  861. default:
  862. D1(" ");
  863. count--;
  864. break;
  865. }
  866. }
  867. /* Recovery mechanism for WAIT_SYNC state. */
  868. if (is_eop) {
  869. if (odev->rx_parse_state == WAIT_SYNC) {
  870. odev->rx_parse_state = WAIT_IP;
  871. odev->rx_buf_size = 0;
  872. odev->rx_buf_missing = sizeof(struct iphdr);
  873. }
  874. }
  875. }
  876. /* Moving data from usb to kernel (in interrupt state) */
  877. static void read_bulk_callback(struct urb *urb)
  878. {
  879. struct hso_net *odev = urb->context;
  880. struct net_device *net;
  881. int result;
  882. int status = urb->status;
  883. /* is al ok? (Filip: Who's Al ?) */
  884. if (status) {
  885. log_usb_status(status, __func__);
  886. return;
  887. }
  888. /* Sanity check */
  889. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  890. D1("BULK IN callback but driver is not active!");
  891. return;
  892. }
  893. usb_mark_last_busy(urb->dev);
  894. net = odev->net;
  895. if (!netif_device_present(net)) {
  896. /* Somebody killed our network interface... */
  897. return;
  898. }
  899. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  900. u32 rest;
  901. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  902. rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
  903. if (((rest == 5) || (rest == 6))
  904. && !memcmp(((u8 *) urb->transfer_buffer) +
  905. urb->actual_length - 4, crc_check, 4)) {
  906. urb->actual_length -= 4;
  907. }
  908. }
  909. /* do we even have a packet? */
  910. if (urb->actual_length) {
  911. /* Handle the IP stream, add header and push it onto network
  912. * stack if the packet is complete. */
  913. spin_lock(&odev->net_lock);
  914. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  915. (urb->transfer_buffer_length >
  916. urb->actual_length) ? 1 : 0);
  917. spin_unlock(&odev->net_lock);
  918. }
  919. /* We are done with this URB, resubmit it. Prep the USB to wait for
  920. * another frame. Reuse same as received. */
  921. usb_fill_bulk_urb(urb,
  922. odev->parent->usb,
  923. usb_rcvbulkpipe(odev->parent->usb,
  924. odev->in_endp->
  925. bEndpointAddress & 0x7F),
  926. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  927. read_bulk_callback, odev);
  928. /* Give this to the USB subsystem so it can tell us when more data
  929. * arrives. */
  930. result = usb_submit_urb(urb, GFP_ATOMIC);
  931. if (result)
  932. dev_warn(&odev->parent->interface->dev,
  933. "%s failed submit mux_bulk_rx_urb %d", __func__,
  934. result);
  935. }
  936. /* Serial driver functions */
  937. static void _hso_serial_set_termios(struct tty_struct *tty,
  938. struct ktermios *old)
  939. {
  940. struct hso_serial *serial = get_serial_by_tty(tty);
  941. struct ktermios *termios;
  942. if ((!tty) || (!tty->termios) || (!serial)) {
  943. printk(KERN_ERR "%s: no tty structures", __func__);
  944. return;
  945. }
  946. D4("port %d", serial->minor);
  947. /*
  948. * The default requirements for this device are:
  949. */
  950. termios = tty->termios;
  951. termios->c_iflag &=
  952. ~(IGNBRK /* disable ignore break */
  953. | BRKINT /* disable break causes interrupt */
  954. | PARMRK /* disable mark parity errors */
  955. | ISTRIP /* disable clear high bit of input characters */
  956. | INLCR /* disable translate NL to CR */
  957. | IGNCR /* disable ignore CR */
  958. | ICRNL /* disable translate CR to NL */
  959. | IXON); /* disable enable XON/XOFF flow control */
  960. /* disable postprocess output characters */
  961. termios->c_oflag &= ~OPOST;
  962. termios->c_lflag &=
  963. ~(ECHO /* disable echo input characters */
  964. | ECHONL /* disable echo new line */
  965. | ICANON /* disable erase, kill, werase, and rprnt
  966. special characters */
  967. | ISIG /* disable interrupt, quit, and suspend special
  968. characters */
  969. | IEXTEN); /* disable non-POSIX special characters */
  970. termios->c_cflag &=
  971. ~(CSIZE /* no size */
  972. | PARENB /* disable parity bit */
  973. | CBAUD /* clear current baud rate */
  974. | CBAUDEX); /* clear current buad rate */
  975. termios->c_cflag |= CS8; /* character size 8 bits */
  976. /* baud rate 115200 */
  977. tty_encode_baud_rate(serial->tty, 115200, 115200);
  978. /*
  979. * Force low_latency on; otherwise the pushes are scheduled;
  980. * this is bad as it opens up the possibility of dropping bytes
  981. * on the floor. We don't want to drop bytes on the floor. :)
  982. */
  983. serial->tty->low_latency = 1;
  984. return;
  985. }
  986. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  987. {
  988. int result;
  989. #ifdef CONFIG_HSO_AUTOPM
  990. usb_mark_last_busy(urb->dev);
  991. #endif
  992. /* We are done with this URB, resubmit it. Prep the USB to wait for
  993. * another frame */
  994. usb_fill_bulk_urb(urb, serial->parent->usb,
  995. usb_rcvbulkpipe(serial->parent->usb,
  996. serial->in_endp->
  997. bEndpointAddress & 0x7F),
  998. urb->transfer_buffer, serial->rx_data_length,
  999. hso_std_serial_read_bulk_callback, serial);
  1000. /* Give this to the USB subsystem so it can tell us when more data
  1001. * arrives. */
  1002. result = usb_submit_urb(urb, GFP_ATOMIC);
  1003. if (result) {
  1004. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  1005. __func__, result);
  1006. }
  1007. }
  1008. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  1009. {
  1010. int count;
  1011. struct urb *curr_urb;
  1012. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  1013. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  1014. count = put_rxbuf_data(curr_urb, serial);
  1015. if (count == -1)
  1016. return;
  1017. if (count == 0) {
  1018. serial->curr_rx_urb_idx++;
  1019. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  1020. serial->curr_rx_urb_idx = 0;
  1021. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  1022. }
  1023. }
  1024. }
  1025. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1026. {
  1027. int count = 0;
  1028. struct urb *urb;
  1029. urb = serial->rx_urb[0];
  1030. if (serial->open_count > 0) {
  1031. count = put_rxbuf_data(urb, serial);
  1032. if (count == -1)
  1033. return;
  1034. }
  1035. /* Re issue a read as long as we receive data. */
  1036. if (count == 0 && ((urb->actual_length != 0) ||
  1037. (serial->rx_state == RX_PENDING))) {
  1038. serial->rx_state = RX_SENT;
  1039. hso_mux_serial_read(serial);
  1040. } else
  1041. serial->rx_state = RX_IDLE;
  1042. }
  1043. /* read callback for Diag and CS port */
  1044. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1045. {
  1046. struct hso_serial *serial = urb->context;
  1047. int status = urb->status;
  1048. /* sanity check */
  1049. if (!serial) {
  1050. D1("serial == NULL");
  1051. return;
  1052. } else if (status) {
  1053. log_usb_status(status, __func__);
  1054. return;
  1055. }
  1056. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1057. D1("Actual length = %d\n", urb->actual_length);
  1058. DUMP1(urb->transfer_buffer, urb->actual_length);
  1059. /* Anyone listening? */
  1060. if (serial->open_count == 0)
  1061. return;
  1062. if (status == 0) {
  1063. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1064. u32 rest;
  1065. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1066. rest =
  1067. urb->actual_length %
  1068. serial->in_endp->wMaxPacketSize;
  1069. if (((rest == 5) || (rest == 6))
  1070. && !memcmp(((u8 *) urb->transfer_buffer) +
  1071. urb->actual_length - 4, crc_check, 4)) {
  1072. urb->actual_length -= 4;
  1073. }
  1074. }
  1075. /* Valid data, handle RX data */
  1076. spin_lock(&serial->serial_lock);
  1077. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1078. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1079. spin_unlock(&serial->serial_lock);
  1080. } else if (status == -ENOENT || status == -ECONNRESET) {
  1081. /* Unlinked - check for throttled port. */
  1082. D2("Port %d, successfully unlinked urb", serial->minor);
  1083. spin_lock(&serial->serial_lock);
  1084. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1085. hso_resubmit_rx_bulk_urb(serial, urb);
  1086. spin_unlock(&serial->serial_lock);
  1087. } else {
  1088. D2("Port %d, status = %d for read urb", serial->minor, status);
  1089. return;
  1090. }
  1091. }
  1092. /*
  1093. * This needs to be a tasklet otherwise we will
  1094. * end up recursively calling this function.
  1095. */
  1096. void hso_unthrottle_tasklet(struct hso_serial *serial)
  1097. {
  1098. unsigned long flags;
  1099. spin_lock_irqsave(&serial->serial_lock, flags);
  1100. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1101. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1102. else
  1103. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1104. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1105. }
  1106. static void hso_unthrottle(struct tty_struct *tty)
  1107. {
  1108. struct hso_serial *serial = get_serial_by_tty(tty);
  1109. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1110. }
  1111. void hso_unthrottle_workfunc(struct work_struct *work)
  1112. {
  1113. struct hso_serial *serial =
  1114. container_of(work, struct hso_serial,
  1115. retry_unthrottle_workqueue);
  1116. hso_unthrottle_tasklet(serial);
  1117. }
  1118. /* open the requested serial port */
  1119. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1120. {
  1121. struct hso_serial *serial = get_serial_by_index(tty->index);
  1122. int result1 = 0, result2 = 0;
  1123. struct mutex *hso_mutex = NULL;
  1124. int refcnt = 1;
  1125. /* sanity check */
  1126. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1127. tty->driver_data = NULL;
  1128. D1("Failed to open port");
  1129. return -ENODEV;
  1130. }
  1131. hso_mutex = &serial->parent->mutex->mutex;
  1132. mutex_lock(hso_mutex);
  1133. /* check for port already opened, if not set the termios */
  1134. /* The serial->open count needs to be here as hso_serial_close
  1135. * will be called even if hso_serial_open returns -ENODEV.
  1136. */
  1137. serial->open_count++;
  1138. result1 = usb_autopm_get_interface(serial->parent->interface);
  1139. if (result1 < 0)
  1140. goto err_out;
  1141. D1("Opening %d", serial->minor);
  1142. kref_get(&serial->parent->ref);
  1143. /* setup */
  1144. tty->driver_data = serial;
  1145. serial->tty = tty;
  1146. if (serial->open_count == 1) {
  1147. tty->low_latency = 1;
  1148. serial->rx_state = RX_IDLE;
  1149. /* Force default termio settings */
  1150. _hso_serial_set_termios(tty, NULL);
  1151. tasklet_init(&serial->unthrottle_tasklet,
  1152. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1153. (unsigned long)serial);
  1154. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1155. hso_unthrottle_workfunc);
  1156. result2 = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1157. if (result2) {
  1158. hso_stop_serial_device(serial->parent);
  1159. serial->open_count--;
  1160. }
  1161. } else {
  1162. D1("Port was already open");
  1163. }
  1164. usb_autopm_put_interface(serial->parent->interface);
  1165. /* done */
  1166. if (result1)
  1167. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1168. err_out:
  1169. if (result2)
  1170. refcnt = kref_put(&serial->parent->ref, hso_serial_ref_free);
  1171. mutex_unlock(hso_mutex);
  1172. if (refcnt == 0)
  1173. hso_free_mutex(container_of(hso_mutex,
  1174. struct hso_mutex_t, mutex));
  1175. return result1 == 0 ? result2 : result1;
  1176. }
  1177. /* close the requested serial port */
  1178. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1179. {
  1180. struct hso_serial *serial = tty->driver_data;
  1181. u8 usb_gone;
  1182. struct mutex *hso_mutex;
  1183. int refcnt;
  1184. D1("Closing serial port");
  1185. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1186. D1("invalid serial structure bailing out.\n");
  1187. return;
  1188. }
  1189. usb_gone = serial->parent->usb_gone;
  1190. hso_mutex = &serial->parent->mutex->mutex;
  1191. mutex_lock(hso_mutex);
  1192. if (!usb_gone)
  1193. usb_autopm_get_interface(serial->parent->interface);
  1194. /* reset the rts and dtr */
  1195. /* do the actual close */
  1196. serial->open_count--;
  1197. if (serial->open_count <= 0) {
  1198. serial->open_count = 0;
  1199. if (serial->tty) {
  1200. serial->tty->driver_data = NULL;
  1201. serial->tty = NULL;
  1202. }
  1203. if (!usb_gone)
  1204. hso_stop_serial_device(serial->parent);
  1205. tasklet_kill(&serial->unthrottle_tasklet);
  1206. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1207. }
  1208. if (!usb_gone)
  1209. usb_autopm_put_interface(serial->parent->interface);
  1210. refcnt = kref_put(&serial->parent->ref, hso_serial_ref_free);
  1211. mutex_unlock(hso_mutex);
  1212. if (refcnt == 0)
  1213. hso_free_mutex(container_of(hso_mutex,
  1214. struct hso_mutex_t, mutex));
  1215. }
  1216. /* close the requested serial port */
  1217. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1218. int count)
  1219. {
  1220. struct hso_serial *serial = get_serial_by_tty(tty);
  1221. int space, tx_bytes;
  1222. unsigned long flags;
  1223. /* sanity check */
  1224. if (serial == NULL) {
  1225. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1226. return -ENODEV;
  1227. }
  1228. spin_lock_irqsave(&serial->serial_lock, flags);
  1229. space = serial->tx_data_length - serial->tx_buffer_count;
  1230. tx_bytes = (count < space) ? count : space;
  1231. if (!tx_bytes)
  1232. goto out;
  1233. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1234. serial->tx_buffer_count += tx_bytes;
  1235. out:
  1236. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1237. hso_kick_transmit(serial);
  1238. /* done */
  1239. return tx_bytes;
  1240. }
  1241. /* how much room is there for writing */
  1242. static int hso_serial_write_room(struct tty_struct *tty)
  1243. {
  1244. struct hso_serial *serial = get_serial_by_tty(tty);
  1245. int room;
  1246. unsigned long flags;
  1247. spin_lock_irqsave(&serial->serial_lock, flags);
  1248. room = serial->tx_data_length - serial->tx_buffer_count;
  1249. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1250. /* return free room */
  1251. return room;
  1252. }
  1253. /* setup the term */
  1254. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1255. {
  1256. struct hso_serial *serial = get_serial_by_tty(tty);
  1257. unsigned long flags;
  1258. if (old)
  1259. D5("Termios called with: cflags new[%d] - old[%d]",
  1260. tty->termios->c_cflag, old->c_cflag);
  1261. /* the actual setup */
  1262. spin_lock_irqsave(&serial->serial_lock, flags);
  1263. if (serial->open_count)
  1264. _hso_serial_set_termios(tty, old);
  1265. else
  1266. tty->termios = old;
  1267. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1268. /* done */
  1269. return;
  1270. }
  1271. /* how many characters in the buffer */
  1272. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1273. {
  1274. struct hso_serial *serial = get_serial_by_tty(tty);
  1275. int chars;
  1276. unsigned long flags;
  1277. /* sanity check */
  1278. if (serial == NULL)
  1279. return 0;
  1280. spin_lock_irqsave(&serial->serial_lock, flags);
  1281. chars = serial->tx_buffer_count;
  1282. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1283. return chars;
  1284. }
  1285. int tiocmget_submit_urb(struct hso_serial *serial,
  1286. struct hso_tiocmget *tiocmget,
  1287. struct usb_device *usb)
  1288. {
  1289. int result;
  1290. if (serial->parent->usb_gone)
  1291. return -ENODEV;
  1292. usb_fill_int_urb(tiocmget->urb, usb,
  1293. usb_rcvintpipe(usb,
  1294. tiocmget->endp->
  1295. bEndpointAddress & 0x7F),
  1296. &tiocmget->serial_state_notification,
  1297. sizeof(struct hso_serial_state_notification),
  1298. tiocmget_intr_callback, serial,
  1299. tiocmget->endp->bInterval);
  1300. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1301. if (result) {
  1302. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1303. result);
  1304. }
  1305. return result;
  1306. }
  1307. static void tiocmget_intr_callback(struct urb *urb)
  1308. {
  1309. struct hso_serial *serial = urb->context;
  1310. struct hso_tiocmget *tiocmget;
  1311. int status = urb->status;
  1312. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1313. struct uart_icount *icount;
  1314. struct hso_serial_state_notification *serial_state_notification;
  1315. struct usb_device *usb;
  1316. /* Sanity checks */
  1317. if (!serial)
  1318. return;
  1319. if (status) {
  1320. log_usb_status(status, __func__);
  1321. return;
  1322. }
  1323. tiocmget = serial->tiocmget;
  1324. if (!tiocmget)
  1325. return;
  1326. usb = serial->parent->usb;
  1327. serial_state_notification = &tiocmget->serial_state_notification;
  1328. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1329. serial_state_notification->bNotification != B_NOTIFICATION ||
  1330. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1331. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1332. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1333. dev_warn(&usb->dev,
  1334. "hso received invalid serial state notification\n");
  1335. DUMP(serial_state_notification,
  1336. sizeof(hso_serial_state_notifation))
  1337. } else {
  1338. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1339. UART_state_bitmap);
  1340. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1341. icount = &tiocmget->icount;
  1342. spin_lock(&serial->serial_lock);
  1343. if ((UART_state_bitmap & B_OVERRUN) !=
  1344. (prev_UART_state_bitmap & B_OVERRUN))
  1345. icount->parity++;
  1346. if ((UART_state_bitmap & B_PARITY) !=
  1347. (prev_UART_state_bitmap & B_PARITY))
  1348. icount->parity++;
  1349. if ((UART_state_bitmap & B_FRAMING) !=
  1350. (prev_UART_state_bitmap & B_FRAMING))
  1351. icount->frame++;
  1352. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1353. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1354. icount->rng++;
  1355. if ((UART_state_bitmap & B_BREAK) !=
  1356. (prev_UART_state_bitmap & B_BREAK))
  1357. icount->brk++;
  1358. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1359. (prev_UART_state_bitmap & B_TX_CARRIER))
  1360. icount->dsr++;
  1361. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1362. (prev_UART_state_bitmap & B_RX_CARRIER))
  1363. icount->dcd++;
  1364. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1365. spin_unlock(&serial->serial_lock);
  1366. tiocmget->intr_completed = 1;
  1367. wake_up_interruptible(&tiocmget->waitq);
  1368. }
  1369. memset(serial_state_notification, 0,
  1370. sizeof(struct hso_serial_state_notification));
  1371. tiocmget_submit_urb(serial,
  1372. tiocmget,
  1373. serial->parent->usb);
  1374. }
  1375. /*
  1376. * next few functions largely stolen from drivers/serial/serial_core.c
  1377. */
  1378. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1379. * - mask passed in arg for lines of interest
  1380. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1381. * Caller should use TIOCGICOUNT to see which one it was
  1382. */
  1383. static int
  1384. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1385. {
  1386. DECLARE_WAITQUEUE(wait, current);
  1387. struct uart_icount cprev, cnow;
  1388. struct hso_tiocmget *tiocmget;
  1389. int ret;
  1390. tiocmget = serial->tiocmget;
  1391. if (!tiocmget)
  1392. return -ENOENT;
  1393. /*
  1394. * note the counters on entry
  1395. */
  1396. spin_lock_irq(&serial->serial_lock);
  1397. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1398. spin_unlock_irq(&serial->serial_lock);
  1399. add_wait_queue(&tiocmget->waitq, &wait);
  1400. for (;;) {
  1401. spin_lock_irq(&serial->serial_lock);
  1402. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1403. spin_unlock_irq(&serial->serial_lock);
  1404. set_current_state(TASK_INTERRUPTIBLE);
  1405. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1406. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1407. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1408. ret = 0;
  1409. break;
  1410. }
  1411. schedule();
  1412. /* see if a signal did it */
  1413. if (signal_pending(current)) {
  1414. ret = -ERESTARTSYS;
  1415. break;
  1416. }
  1417. cprev = cnow;
  1418. }
  1419. current->state = TASK_RUNNING;
  1420. remove_wait_queue(&tiocmget->waitq, &wait);
  1421. return ret;
  1422. }
  1423. /*
  1424. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1425. * Return: write counters to the user passed counter struct
  1426. * NB: both 1->0 and 0->1 transitions are counted except for
  1427. * RI where only 0->1 is counted.
  1428. */
  1429. static int hso_get_count(struct hso_serial *serial,
  1430. struct serial_icounter_struct __user *icnt)
  1431. {
  1432. struct serial_icounter_struct icount;
  1433. struct uart_icount cnow;
  1434. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1435. if (!tiocmget)
  1436. return -ENOENT;
  1437. spin_lock_irq(&serial->serial_lock);
  1438. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1439. spin_unlock_irq(&serial->serial_lock);
  1440. icount.cts = cnow.cts;
  1441. icount.dsr = cnow.dsr;
  1442. icount.rng = cnow.rng;
  1443. icount.dcd = cnow.dcd;
  1444. icount.rx = cnow.rx;
  1445. icount.tx = cnow.tx;
  1446. icount.frame = cnow.frame;
  1447. icount.overrun = cnow.overrun;
  1448. icount.parity = cnow.parity;
  1449. icount.brk = cnow.brk;
  1450. icount.buf_overrun = cnow.buf_overrun;
  1451. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  1452. }
  1453. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1454. {
  1455. int retval;
  1456. struct hso_serial *serial = get_serial_by_tty(tty);
  1457. struct hso_tiocmget *tiocmget;
  1458. u16 UART_state_bitmap;
  1459. /* sanity check */
  1460. if (!serial) {
  1461. D1("no tty structures");
  1462. return -EINVAL;
  1463. }
  1464. spin_lock_irq(&serial->serial_lock);
  1465. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1466. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1467. tiocmget = serial->tiocmget;
  1468. if (tiocmget) {
  1469. UART_state_bitmap = le16_to_cpu(
  1470. tiocmget->prev_UART_state_bitmap);
  1471. if (UART_state_bitmap & B_RING_SIGNAL)
  1472. retval |= TIOCM_RNG;
  1473. if (UART_state_bitmap & B_RX_CARRIER)
  1474. retval |= TIOCM_CD;
  1475. if (UART_state_bitmap & B_TX_CARRIER)
  1476. retval |= TIOCM_DSR;
  1477. }
  1478. spin_unlock_irq(&serial->serial_lock);
  1479. return retval;
  1480. }
  1481. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1482. unsigned int set, unsigned int clear)
  1483. {
  1484. int val = 0;
  1485. unsigned long flags;
  1486. int if_num;
  1487. struct hso_serial *serial = get_serial_by_tty(tty);
  1488. /* sanity check */
  1489. if (!serial) {
  1490. D1("no tty structures");
  1491. return -EINVAL;
  1492. }
  1493. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1494. spin_lock_irqsave(&serial->serial_lock, flags);
  1495. if (set & TIOCM_RTS)
  1496. serial->rts_state = 1;
  1497. if (set & TIOCM_DTR)
  1498. serial->dtr_state = 1;
  1499. if (clear & TIOCM_RTS)
  1500. serial->rts_state = 0;
  1501. if (clear & TIOCM_DTR)
  1502. serial->dtr_state = 0;
  1503. if (serial->dtr_state)
  1504. val |= 0x01;
  1505. if (serial->rts_state)
  1506. val |= 0x02;
  1507. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1508. return usb_control_msg(serial->parent->usb,
  1509. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1510. 0x21, val, if_num, NULL, 0,
  1511. USB_CTRL_SET_TIMEOUT);
  1512. }
  1513. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1514. unsigned int cmd, unsigned long arg)
  1515. {
  1516. struct hso_serial *serial = get_serial_by_tty(tty);
  1517. void __user *uarg = (void __user *)arg;
  1518. int ret = 0;
  1519. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1520. if (!serial)
  1521. return -ENODEV;
  1522. switch (cmd) {
  1523. case TIOCMIWAIT:
  1524. ret = hso_wait_modem_status(serial, arg);
  1525. break;
  1526. case TIOCGICOUNT:
  1527. ret = hso_get_count(serial, uarg);
  1528. break;
  1529. default:
  1530. ret = -ENOIOCTLCMD;
  1531. break;
  1532. }
  1533. return ret;
  1534. }
  1535. /* starts a transmit */
  1536. static void hso_kick_transmit(struct hso_serial *serial)
  1537. {
  1538. u8 *temp;
  1539. unsigned long flags;
  1540. int res;
  1541. spin_lock_irqsave(&serial->serial_lock, flags);
  1542. if (!serial->tx_buffer_count)
  1543. goto out;
  1544. if (serial->tx_urb_used)
  1545. goto out;
  1546. /* Wakeup USB interface if necessary */
  1547. if (hso_get_activity(serial->parent) == -EAGAIN)
  1548. goto out;
  1549. /* Switch pointers around to avoid memcpy */
  1550. temp = serial->tx_buffer;
  1551. serial->tx_buffer = serial->tx_data;
  1552. serial->tx_data = temp;
  1553. serial->tx_data_count = serial->tx_buffer_count;
  1554. serial->tx_buffer_count = 0;
  1555. /* If temp is set, it means we switched buffers */
  1556. if (temp && serial->write_data) {
  1557. res = serial->write_data(serial);
  1558. if (res >= 0)
  1559. serial->tx_urb_used = 1;
  1560. }
  1561. out:
  1562. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1563. }
  1564. /* make a request (for reading and writing data to muxed serial port) */
  1565. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1566. struct urb *ctrl_urb,
  1567. struct usb_ctrlrequest *ctrl_req,
  1568. u8 *ctrl_urb_data, u32 size)
  1569. {
  1570. int result;
  1571. int pipe;
  1572. /* Sanity check */
  1573. if (!serial || !ctrl_urb || !ctrl_req) {
  1574. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1575. return -EINVAL;
  1576. }
  1577. /* initialize */
  1578. ctrl_req->wValue = 0;
  1579. ctrl_req->wIndex = hso_port_to_mux(port);
  1580. ctrl_req->wLength = size;
  1581. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1582. /* Reading command */
  1583. ctrl_req->bRequestType = USB_DIR_IN |
  1584. USB_TYPE_OPTION_VENDOR |
  1585. USB_RECIP_INTERFACE;
  1586. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1587. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1588. } else {
  1589. /* Writing command */
  1590. ctrl_req->bRequestType = USB_DIR_OUT |
  1591. USB_TYPE_OPTION_VENDOR |
  1592. USB_RECIP_INTERFACE;
  1593. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1594. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1595. }
  1596. /* syslog */
  1597. D2("%s command (%02x) len: %d, port: %d",
  1598. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1599. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1600. /* Load ctrl urb */
  1601. ctrl_urb->transfer_flags = 0;
  1602. usb_fill_control_urb(ctrl_urb,
  1603. serial->parent->usb,
  1604. pipe,
  1605. (u8 *) ctrl_req,
  1606. ctrl_urb_data, size, ctrl_callback, serial);
  1607. /* Send it on merry way */
  1608. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1609. if (result) {
  1610. dev_err(&ctrl_urb->dev->dev,
  1611. "%s failed submit ctrl_urb %d type %d", __func__,
  1612. result, type);
  1613. return result;
  1614. }
  1615. /* done */
  1616. return size;
  1617. }
  1618. /* called by intr_callback when read occurs */
  1619. static int hso_mux_serial_read(struct hso_serial *serial)
  1620. {
  1621. if (!serial)
  1622. return -EINVAL;
  1623. /* clean data */
  1624. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1625. /* make the request */
  1626. if (serial->num_rx_urbs != 1) {
  1627. dev_err(&serial->parent->interface->dev,
  1628. "ERROR: mux'd reads with multiple buffers "
  1629. "not possible\n");
  1630. return 0;
  1631. }
  1632. return mux_device_request(serial,
  1633. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1634. serial->parent->port_spec & HSO_PORT_MASK,
  1635. serial->rx_urb[0],
  1636. &serial->ctrl_req_rx,
  1637. serial->rx_data[0], serial->rx_data_length);
  1638. }
  1639. /* used for muxed serial port callback (muxed serial read) */
  1640. static void intr_callback(struct urb *urb)
  1641. {
  1642. struct hso_shared_int *shared_int = urb->context;
  1643. struct hso_serial *serial;
  1644. unsigned char *port_req;
  1645. int status = urb->status;
  1646. int i;
  1647. usb_mark_last_busy(urb->dev);
  1648. /* sanity check */
  1649. if (!shared_int)
  1650. return;
  1651. /* status check */
  1652. if (status) {
  1653. log_usb_status(status, __func__);
  1654. return;
  1655. }
  1656. D4("\n--- Got intr callback 0x%02X ---", status);
  1657. /* what request? */
  1658. port_req = urb->transfer_buffer;
  1659. D4(" port_req = 0x%.2X\n", *port_req);
  1660. /* loop over all muxed ports to find the one sending this */
  1661. for (i = 0; i < 8; i++) {
  1662. /* max 8 channels on MUX */
  1663. if (*port_req & (1 << i)) {
  1664. serial = get_serial_by_shared_int_and_type(shared_int,
  1665. (1 << i));
  1666. if (serial != NULL) {
  1667. D1("Pending read interrupt on port %d\n", i);
  1668. spin_lock(&serial->serial_lock);
  1669. if (serial->rx_state == RX_IDLE) {
  1670. /* Setup and send a ctrl req read on
  1671. * port i */
  1672. if (!serial->rx_urb_filled[0]) {
  1673. serial->rx_state = RX_SENT;
  1674. hso_mux_serial_read(serial);
  1675. } else
  1676. serial->rx_state = RX_PENDING;
  1677. } else {
  1678. D1("Already pending a read on "
  1679. "port %d\n", i);
  1680. }
  1681. spin_unlock(&serial->serial_lock);
  1682. }
  1683. }
  1684. }
  1685. /* Resubmit interrupt urb */
  1686. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1687. }
  1688. /* called for writing to muxed serial port */
  1689. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1690. {
  1691. if (NULL == serial)
  1692. return -EINVAL;
  1693. return mux_device_request(serial,
  1694. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1695. serial->parent->port_spec & HSO_PORT_MASK,
  1696. serial->tx_urb,
  1697. &serial->ctrl_req_tx,
  1698. serial->tx_data, serial->tx_data_count);
  1699. }
  1700. /* write callback for Diag and CS port */
  1701. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1702. {
  1703. struct hso_serial *serial = urb->context;
  1704. int status = urb->status;
  1705. /* sanity check */
  1706. if (!serial) {
  1707. D1("serial == NULL");
  1708. return;
  1709. }
  1710. spin_lock(&serial->serial_lock);
  1711. serial->tx_urb_used = 0;
  1712. spin_unlock(&serial->serial_lock);
  1713. if (status) {
  1714. log_usb_status(status, __func__);
  1715. return;
  1716. }
  1717. hso_put_activity(serial->parent);
  1718. if (serial->tty)
  1719. tty_wakeup(serial->tty);
  1720. hso_kick_transmit(serial);
  1721. D1(" ");
  1722. return;
  1723. }
  1724. /* called for writing diag or CS serial port */
  1725. static int hso_std_serial_write_data(struct hso_serial *serial)
  1726. {
  1727. int count = serial->tx_data_count;
  1728. int result;
  1729. usb_fill_bulk_urb(serial->tx_urb,
  1730. serial->parent->usb,
  1731. usb_sndbulkpipe(serial->parent->usb,
  1732. serial->out_endp->
  1733. bEndpointAddress & 0x7F),
  1734. serial->tx_data, serial->tx_data_count,
  1735. hso_std_serial_write_bulk_callback, serial);
  1736. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1737. if (result) {
  1738. dev_warn(&serial->parent->usb->dev,
  1739. "Failed to submit urb - res %d\n", result);
  1740. return result;
  1741. }
  1742. return count;
  1743. }
  1744. /* callback after read or write on muxed serial port */
  1745. static void ctrl_callback(struct urb *urb)
  1746. {
  1747. struct hso_serial *serial = urb->context;
  1748. struct usb_ctrlrequest *req;
  1749. int status = urb->status;
  1750. /* sanity check */
  1751. if (!serial)
  1752. return;
  1753. spin_lock(&serial->serial_lock);
  1754. serial->tx_urb_used = 0;
  1755. spin_unlock(&serial->serial_lock);
  1756. if (status) {
  1757. log_usb_status(status, __func__);
  1758. return;
  1759. }
  1760. /* what request? */
  1761. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1762. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1763. D4("Actual length of urb = %d\n", urb->actual_length);
  1764. DUMP1(urb->transfer_buffer, urb->actual_length);
  1765. if (req->bRequestType ==
  1766. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1767. /* response to a read command */
  1768. serial->rx_urb_filled[0] = 1;
  1769. spin_lock(&serial->serial_lock);
  1770. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1771. spin_unlock(&serial->serial_lock);
  1772. } else {
  1773. hso_put_activity(serial->parent);
  1774. if (serial->tty)
  1775. tty_wakeup(serial->tty);
  1776. /* response to a write command */
  1777. hso_kick_transmit(serial);
  1778. }
  1779. }
  1780. /* handle RX data for serial port */
  1781. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1782. {
  1783. struct tty_struct *tty = serial->tty;
  1784. int write_length_remaining = 0;
  1785. int curr_write_len;
  1786. /* Sanity check */
  1787. if (urb == NULL || serial == NULL) {
  1788. D1("serial = NULL");
  1789. return -2;
  1790. }
  1791. /* Push data to tty */
  1792. if (tty) {
  1793. write_length_remaining = urb->actual_length -
  1794. serial->curr_rx_urb_offset;
  1795. D1("data to push to tty");
  1796. while (write_length_remaining) {
  1797. if (test_bit(TTY_THROTTLED, &tty->flags))
  1798. return -1;
  1799. curr_write_len = tty_insert_flip_string
  1800. (tty, urb->transfer_buffer +
  1801. serial->curr_rx_urb_offset,
  1802. write_length_remaining);
  1803. serial->curr_rx_urb_offset += curr_write_len;
  1804. write_length_remaining -= curr_write_len;
  1805. tty_flip_buffer_push(tty);
  1806. }
  1807. }
  1808. if (write_length_remaining == 0) {
  1809. serial->curr_rx_urb_offset = 0;
  1810. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1811. }
  1812. return write_length_remaining;
  1813. }
  1814. /* Base driver functions */
  1815. static void hso_log_port(struct hso_device *hso_dev)
  1816. {
  1817. char *port_type;
  1818. char port_dev[20];
  1819. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1820. case HSO_PORT_CONTROL:
  1821. port_type = "Control";
  1822. break;
  1823. case HSO_PORT_APP:
  1824. port_type = "Application";
  1825. break;
  1826. case HSO_PORT_GPS:
  1827. port_type = "GPS";
  1828. break;
  1829. case HSO_PORT_GPS_CONTROL:
  1830. port_type = "GPS control";
  1831. break;
  1832. case HSO_PORT_APP2:
  1833. port_type = "Application2";
  1834. break;
  1835. case HSO_PORT_PCSC:
  1836. port_type = "PCSC";
  1837. break;
  1838. case HSO_PORT_DIAG:
  1839. port_type = "Diagnostic";
  1840. break;
  1841. case HSO_PORT_DIAG2:
  1842. port_type = "Diagnostic2";
  1843. break;
  1844. case HSO_PORT_MODEM:
  1845. port_type = "Modem";
  1846. break;
  1847. case HSO_PORT_NETWORK:
  1848. port_type = "Network";
  1849. break;
  1850. default:
  1851. port_type = "Unknown";
  1852. break;
  1853. }
  1854. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1855. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1856. } else
  1857. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1858. dev2ser(hso_dev)->minor);
  1859. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1860. port_type, port_dev);
  1861. }
  1862. static int hso_start_net_device(struct hso_device *hso_dev)
  1863. {
  1864. int i, result = 0;
  1865. struct hso_net *hso_net = dev2net(hso_dev);
  1866. if (!hso_net)
  1867. return -ENODEV;
  1868. /* send URBs for all read buffers */
  1869. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1870. /* Prep a receive URB */
  1871. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1872. hso_dev->usb,
  1873. usb_rcvbulkpipe(hso_dev->usb,
  1874. hso_net->in_endp->
  1875. bEndpointAddress & 0x7F),
  1876. hso_net->mux_bulk_rx_buf_pool[i],
  1877. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1878. hso_net);
  1879. /* Put it out there so the device can send us stuff */
  1880. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1881. GFP_NOIO);
  1882. if (result)
  1883. dev_warn(&hso_dev->usb->dev,
  1884. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1885. i, result);
  1886. }
  1887. return result;
  1888. }
  1889. static int hso_stop_net_device(struct hso_device *hso_dev)
  1890. {
  1891. int i;
  1892. struct hso_net *hso_net = dev2net(hso_dev);
  1893. if (!hso_net)
  1894. return -ENODEV;
  1895. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1896. if (hso_net->mux_bulk_rx_urb_pool[i])
  1897. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1898. }
  1899. if (hso_net->mux_bulk_tx_urb)
  1900. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1901. return 0;
  1902. }
  1903. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1904. {
  1905. int i, result = 0;
  1906. struct hso_serial *serial = dev2ser(hso_dev);
  1907. if (!serial)
  1908. return -ENODEV;
  1909. /* If it is not the MUX port fill in and submit a bulk urb (already
  1910. * allocated in hso_serial_start) */
  1911. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1912. for (i = 0; i < serial->num_rx_urbs; i++) {
  1913. usb_fill_bulk_urb(serial->rx_urb[i],
  1914. serial->parent->usb,
  1915. usb_rcvbulkpipe(serial->parent->usb,
  1916. serial->in_endp->
  1917. bEndpointAddress &
  1918. 0x7F),
  1919. serial->rx_data[i],
  1920. serial->rx_data_length,
  1921. hso_std_serial_read_bulk_callback,
  1922. serial);
  1923. result = usb_submit_urb(serial->rx_urb[i], flags);
  1924. if (result) {
  1925. dev_warn(&serial->parent->usb->dev,
  1926. "Failed to submit urb - res %d\n",
  1927. result);
  1928. break;
  1929. }
  1930. }
  1931. } else {
  1932. mutex_lock(&serial->shared_int->shared_int_lock);
  1933. if (!serial->shared_int->use_count) {
  1934. result =
  1935. hso_mux_submit_intr_urb(serial->shared_int,
  1936. hso_dev->usb, flags);
  1937. }
  1938. serial->shared_int->use_count++;
  1939. mutex_unlock(&serial->shared_int->shared_int_lock);
  1940. }
  1941. if (serial->tiocmget)
  1942. tiocmget_submit_urb(serial,
  1943. serial->tiocmget,
  1944. serial->parent->usb);
  1945. return result;
  1946. }
  1947. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1948. {
  1949. int i;
  1950. struct hso_serial *serial = dev2ser(hso_dev);
  1951. struct hso_tiocmget *tiocmget;
  1952. if (!serial)
  1953. return -ENODEV;
  1954. for (i = 0; i < serial->num_rx_urbs; i++) {
  1955. if (serial->rx_urb[i]) {
  1956. usb_kill_urb(serial->rx_urb[i]);
  1957. serial->rx_urb_filled[i] = 0;
  1958. }
  1959. }
  1960. serial->curr_rx_urb_idx = 0;
  1961. serial->curr_rx_urb_offset = 0;
  1962. if (serial->tx_urb)
  1963. usb_kill_urb(serial->tx_urb);
  1964. if (serial->shared_int) {
  1965. mutex_lock(&serial->shared_int->shared_int_lock);
  1966. if (serial->shared_int->use_count &&
  1967. (--serial->shared_int->use_count == 0)) {
  1968. struct urb *urb;
  1969. urb = serial->shared_int->shared_intr_urb;
  1970. if (urb)
  1971. usb_kill_urb(urb);
  1972. }
  1973. mutex_unlock(&serial->shared_int->shared_int_lock);
  1974. }
  1975. tiocmget = serial->tiocmget;
  1976. if (tiocmget) {
  1977. wake_up_interruptible(&tiocmget->waitq);
  1978. usb_kill_urb(tiocmget->urb);
  1979. }
  1980. return 0;
  1981. }
  1982. static void hso_serial_common_free(struct hso_serial *serial)
  1983. {
  1984. int i;
  1985. if (serial->parent->dev)
  1986. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1987. tty_unregister_device(tty_drv, serial->minor);
  1988. for (i = 0; i < serial->num_rx_urbs; i++) {
  1989. /* unlink and free RX URB */
  1990. usb_free_urb(serial->rx_urb[i]);
  1991. /* free the RX buffer */
  1992. kfree(serial->rx_data[i]);
  1993. }
  1994. /* unlink and free TX URB */
  1995. usb_free_urb(serial->tx_urb);
  1996. kfree(serial->tx_data);
  1997. }
  1998. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1999. int rx_size, int tx_size)
  2000. {
  2001. struct device *dev;
  2002. int minor;
  2003. int i;
  2004. minor = get_free_serial_index();
  2005. if (minor < 0)
  2006. goto exit;
  2007. /* register our minor number */
  2008. serial->parent->dev = tty_register_device(tty_drv, minor,
  2009. &serial->parent->interface->dev);
  2010. dev = serial->parent->dev;
  2011. dev->driver_data = serial->parent;
  2012. i = device_create_file(dev, &dev_attr_hsotype);
  2013. /* fill in specific data for later use */
  2014. serial->minor = minor;
  2015. serial->magic = HSO_SERIAL_MAGIC;
  2016. spin_lock_init(&serial->serial_lock);
  2017. serial->num_rx_urbs = num_urbs;
  2018. /* RX, allocate urb and initialize */
  2019. /* prepare our RX buffer */
  2020. serial->rx_data_length = rx_size;
  2021. for (i = 0; i < serial->num_rx_urbs; i++) {
  2022. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2023. if (!serial->rx_urb[i]) {
  2024. dev_err(dev, "Could not allocate urb?\n");
  2025. goto exit;
  2026. }
  2027. serial->rx_urb[i]->transfer_buffer = NULL;
  2028. serial->rx_urb[i]->transfer_buffer_length = 0;
  2029. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2030. GFP_KERNEL);
  2031. if (!serial->rx_data[i]) {
  2032. dev_err(dev, "%s - Out of memory\n", __func__);
  2033. goto exit;
  2034. }
  2035. }
  2036. /* TX, allocate urb and initialize */
  2037. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2038. if (!serial->tx_urb) {
  2039. dev_err(dev, "Could not allocate urb?\n");
  2040. goto exit;
  2041. }
  2042. serial->tx_urb->transfer_buffer = NULL;
  2043. serial->tx_urb->transfer_buffer_length = 0;
  2044. /* prepare our TX buffer */
  2045. serial->tx_data_count = 0;
  2046. serial->tx_buffer_count = 0;
  2047. serial->tx_data_length = tx_size;
  2048. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2049. if (!serial->tx_data) {
  2050. dev_err(dev, "%s - Out of memory", __func__);
  2051. goto exit;
  2052. }
  2053. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2054. if (!serial->tx_buffer) {
  2055. dev_err(dev, "%s - Out of memory", __func__);
  2056. goto exit;
  2057. }
  2058. return 0;
  2059. exit:
  2060. hso_serial_common_free(serial);
  2061. return -1;
  2062. }
  2063. /* Frees a general hso device */
  2064. static void hso_free_device(struct hso_device *hso_dev)
  2065. {
  2066. kfree(hso_dev);
  2067. }
  2068. /* Creates a general hso device */
  2069. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2070. int port_spec)
  2071. {
  2072. struct hso_device *hso_dev;
  2073. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2074. if (!hso_dev)
  2075. return NULL;
  2076. hso_dev->port_spec = port_spec;
  2077. hso_dev->usb = interface_to_usbdev(intf);
  2078. hso_dev->interface = intf;
  2079. kref_init(&hso_dev->ref);
  2080. hso_dev->mutex = hso_get_free_mutex();
  2081. if (!hso_dev->mutex) {
  2082. kfree(hso_dev);
  2083. return NULL;
  2084. }
  2085. mutex_init(&hso_dev->mutex->mutex);
  2086. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2087. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2088. return hso_dev;
  2089. }
  2090. /* Removes a network device in the network device table */
  2091. static int remove_net_device(struct hso_device *hso_dev)
  2092. {
  2093. int i;
  2094. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2095. if (network_table[i] == hso_dev) {
  2096. network_table[i] = NULL;
  2097. break;
  2098. }
  2099. }
  2100. if (i == HSO_MAX_NET_DEVICES)
  2101. return -1;
  2102. return 0;
  2103. }
  2104. /* Frees our network device */
  2105. static void hso_free_net_device(struct hso_device *hso_dev)
  2106. {
  2107. int i;
  2108. struct hso_net *hso_net = dev2net(hso_dev);
  2109. if (!hso_net)
  2110. return;
  2111. /* start freeing */
  2112. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2113. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2114. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2115. }
  2116. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2117. kfree(hso_net->mux_bulk_tx_buf);
  2118. remove_net_device(hso_net->parent);
  2119. if (hso_net->net) {
  2120. unregister_netdev(hso_net->net);
  2121. free_netdev(hso_net->net);
  2122. }
  2123. hso_free_mutex(hso_dev->mutex);
  2124. hso_free_device(hso_dev);
  2125. }
  2126. /* initialize the network interface */
  2127. static void hso_net_init(struct net_device *net)
  2128. {
  2129. struct hso_net *hso_net = netdev_priv(net);
  2130. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2131. /* fill in the other fields */
  2132. net->open = hso_net_open;
  2133. net->stop = hso_net_close;
  2134. net->hard_start_xmit = hso_net_start_xmit;
  2135. net->tx_timeout = hso_net_tx_timeout;
  2136. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2137. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2138. net->type = ARPHRD_NONE;
  2139. net->mtu = DEFAULT_MTU - 14;
  2140. net->tx_queue_len = 10;
  2141. SET_ETHTOOL_OPS(net, &ops);
  2142. /* and initialize the semaphore */
  2143. spin_lock_init(&hso_net->net_lock);
  2144. }
  2145. /* Adds a network device in the network device table */
  2146. static int add_net_device(struct hso_device *hso_dev)
  2147. {
  2148. int i;
  2149. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2150. if (network_table[i] == NULL) {
  2151. network_table[i] = hso_dev;
  2152. break;
  2153. }
  2154. }
  2155. if (i == HSO_MAX_NET_DEVICES)
  2156. return -1;
  2157. return 0;
  2158. }
  2159. static int hso_radio_toggle(void *data, enum rfkill_state state)
  2160. {
  2161. struct hso_device *hso_dev = data;
  2162. int enabled = (state == RFKILL_STATE_ON);
  2163. int rv;
  2164. mutex_lock(&hso_dev->mutex->mutex);
  2165. if (hso_dev->usb_gone)
  2166. rv = 0;
  2167. else
  2168. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2169. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2170. USB_CTRL_SET_TIMEOUT);
  2171. mutex_unlock(&hso_dev->mutex->mutex);
  2172. return rv;
  2173. }
  2174. /* Creates and sets up everything for rfkill */
  2175. static void hso_create_rfkill(struct hso_device *hso_dev,
  2176. struct usb_interface *interface)
  2177. {
  2178. struct hso_net *hso_net = dev2net(hso_dev);
  2179. struct device *dev = &hso_net->net->dev;
  2180. char *rfkn;
  2181. hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
  2182. RFKILL_TYPE_WWAN);
  2183. if (!hso_net->rfkill) {
  2184. dev_err(dev, "%s - Out of memory\n", __func__);
  2185. return;
  2186. }
  2187. rfkn = kzalloc(20, GFP_KERNEL);
  2188. if (!rfkn) {
  2189. rfkill_free(hso_net->rfkill);
  2190. hso_net->rfkill = NULL;
  2191. dev_err(dev, "%s - Out of memory\n", __func__);
  2192. return;
  2193. }
  2194. snprintf(rfkn, 20, "hso-%d",
  2195. interface->altsetting->desc.bInterfaceNumber);
  2196. hso_net->rfkill->name = rfkn;
  2197. hso_net->rfkill->state = RFKILL_STATE_ON;
  2198. hso_net->rfkill->data = hso_dev;
  2199. hso_net->rfkill->toggle_radio = hso_radio_toggle;
  2200. if (rfkill_register(hso_net->rfkill) < 0) {
  2201. kfree(rfkn);
  2202. hso_net->rfkill->name = NULL;
  2203. rfkill_free(hso_net->rfkill);
  2204. hso_net->rfkill = NULL;
  2205. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2206. return;
  2207. }
  2208. }
  2209. /* Creates our network device */
  2210. static struct hso_device *hso_create_net_device(struct usb_interface *interface)
  2211. {
  2212. int result, i;
  2213. struct net_device *net;
  2214. struct hso_net *hso_net;
  2215. struct hso_device *hso_dev;
  2216. hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
  2217. if (!hso_dev)
  2218. return NULL;
  2219. /* allocate our network device, then we can put in our private data */
  2220. /* call hso_net_init to do the basic initialization */
  2221. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2222. if (!net) {
  2223. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2224. goto exit;
  2225. }
  2226. hso_net = netdev_priv(net);
  2227. hso_dev->port_data.dev_net = hso_net;
  2228. hso_net->net = net;
  2229. hso_net->parent = hso_dev;
  2230. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2231. USB_DIR_IN);
  2232. if (!hso_net->in_endp) {
  2233. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2234. goto exit;
  2235. }
  2236. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2237. USB_DIR_OUT);
  2238. if (!hso_net->out_endp) {
  2239. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2240. goto exit;
  2241. }
  2242. SET_NETDEV_DEV(net, &interface->dev);
  2243. /* registering our net device */
  2244. result = register_netdev(net);
  2245. if (result) {
  2246. dev_err(&interface->dev, "Failed to register device\n");
  2247. goto exit;
  2248. }
  2249. /* start allocating */
  2250. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2251. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2252. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2253. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2254. goto exit;
  2255. }
  2256. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2257. GFP_KERNEL);
  2258. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2259. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2260. goto exit;
  2261. }
  2262. }
  2263. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2264. if (!hso_net->mux_bulk_tx_urb) {
  2265. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2266. goto exit;
  2267. }
  2268. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2269. if (!hso_net->mux_bulk_tx_buf) {
  2270. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2271. goto exit;
  2272. }
  2273. add_net_device(hso_dev);
  2274. hso_log_port(hso_dev);
  2275. hso_create_rfkill(hso_dev, interface);
  2276. return hso_dev;
  2277. exit:
  2278. hso_free_net_device(hso_dev);
  2279. return NULL;
  2280. }
  2281. static void hso_free_tiomget(struct hso_serial *serial)
  2282. {
  2283. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2284. if (tiocmget) {
  2285. kfree(tiocmget);
  2286. if (tiocmget->urb) {
  2287. usb_free_urb(tiocmget->urb);
  2288. tiocmget->urb = NULL;
  2289. }
  2290. serial->tiocmget = NULL;
  2291. }
  2292. }
  2293. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2294. static void hso_free_serial_device(struct hso_device *hso_dev)
  2295. {
  2296. struct hso_serial *serial = dev2ser(hso_dev);
  2297. if (!serial)
  2298. return;
  2299. set_serial_by_index(serial->minor, NULL);
  2300. hso_serial_common_free(serial);
  2301. if (serial->shared_int) {
  2302. mutex_lock(&serial->shared_int->shared_int_lock);
  2303. if (--serial->shared_int->ref_count == 0)
  2304. hso_free_shared_int(serial->shared_int);
  2305. else
  2306. mutex_unlock(&serial->shared_int->shared_int_lock);
  2307. }
  2308. hso_free_tiomget(serial);
  2309. kfree(serial);
  2310. hso_free_device(hso_dev);
  2311. }
  2312. /* Creates a bulk AT channel */
  2313. static struct hso_device *hso_create_bulk_serial_device(
  2314. struct usb_interface *interface, int port)
  2315. {
  2316. struct hso_device *hso_dev;
  2317. struct hso_serial *serial;
  2318. int num_urbs;
  2319. struct hso_tiocmget *tiocmget;
  2320. hso_dev = hso_create_device(interface, port);
  2321. if (!hso_dev)
  2322. return NULL;
  2323. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2324. if (!serial)
  2325. goto exit;
  2326. serial->parent = hso_dev;
  2327. hso_dev->port_data.dev_serial = serial;
  2328. if (port & HSO_PORT_MODEM) {
  2329. num_urbs = 2;
  2330. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2331. GFP_KERNEL);
  2332. /* it isn't going to break our heart if serial->tiocmget
  2333. * allocation fails don't bother checking this.
  2334. */
  2335. if (serial->tiocmget) {
  2336. tiocmget = serial->tiocmget;
  2337. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2338. if (tiocmget->urb) {
  2339. mutex_init(&tiocmget->mutex);
  2340. init_waitqueue_head(&tiocmget->waitq);
  2341. tiocmget->endp = hso_get_ep(
  2342. interface,
  2343. USB_ENDPOINT_XFER_INT,
  2344. USB_DIR_IN);
  2345. } else
  2346. hso_free_tiomget(serial);
  2347. }
  2348. }
  2349. else
  2350. num_urbs = 1;
  2351. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2352. BULK_URB_TX_SIZE))
  2353. goto exit;
  2354. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2355. USB_DIR_IN);
  2356. if (!serial->in_endp) {
  2357. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2358. goto exit2;
  2359. }
  2360. if (!
  2361. (serial->out_endp =
  2362. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2363. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2364. goto exit2;
  2365. }
  2366. serial->write_data = hso_std_serial_write_data;
  2367. /* and record this serial */
  2368. set_serial_by_index(serial->minor, serial);
  2369. /* setup the proc dirs and files if needed */
  2370. hso_log_port(hso_dev);
  2371. /* done, return it */
  2372. return hso_dev;
  2373. exit2:
  2374. hso_serial_common_free(serial);
  2375. exit:
  2376. hso_free_tiomget(serial);
  2377. kfree(serial);
  2378. hso_free_device(hso_dev);
  2379. return NULL;
  2380. }
  2381. /* Creates a multiplexed AT channel */
  2382. static
  2383. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2384. int port,
  2385. struct hso_shared_int *mux)
  2386. {
  2387. struct hso_device *hso_dev;
  2388. struct hso_serial *serial;
  2389. int port_spec;
  2390. port_spec = HSO_INTF_MUX;
  2391. port_spec &= ~HSO_PORT_MASK;
  2392. port_spec |= hso_mux_to_port(port);
  2393. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2394. return NULL;
  2395. hso_dev = hso_create_device(interface, port_spec);
  2396. if (!hso_dev)
  2397. return NULL;
  2398. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2399. if (!serial)
  2400. goto exit;
  2401. hso_dev->port_data.dev_serial = serial;
  2402. serial->parent = hso_dev;
  2403. if (hso_serial_common_create
  2404. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2405. goto exit;
  2406. serial->tx_data_length--;
  2407. serial->write_data = hso_mux_serial_write_data;
  2408. serial->shared_int = mux;
  2409. mutex_lock(&serial->shared_int->shared_int_lock);
  2410. serial->shared_int->ref_count++;
  2411. mutex_unlock(&serial->shared_int->shared_int_lock);
  2412. /* and record this serial */
  2413. set_serial_by_index(serial->minor, serial);
  2414. /* setup the proc dirs and files if needed */
  2415. hso_log_port(hso_dev);
  2416. /* done, return it */
  2417. return hso_dev;
  2418. exit:
  2419. if (serial) {
  2420. tty_unregister_device(tty_drv, serial->minor);
  2421. kfree(serial);
  2422. }
  2423. if (hso_dev)
  2424. hso_free_device(hso_dev);
  2425. return NULL;
  2426. }
  2427. static void hso_free_shared_int(struct hso_shared_int *mux)
  2428. {
  2429. usb_free_urb(mux->shared_intr_urb);
  2430. kfree(mux->shared_intr_buf);
  2431. mutex_unlock(&mux->shared_int_lock);
  2432. kfree(mux);
  2433. }
  2434. static
  2435. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2436. {
  2437. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2438. if (!mux)
  2439. return NULL;
  2440. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2441. USB_DIR_IN);
  2442. if (!mux->intr_endp) {
  2443. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2444. goto exit;
  2445. }
  2446. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2447. if (!mux->shared_intr_urb) {
  2448. dev_err(&interface->dev, "Could not allocate intr urb?");
  2449. goto exit;
  2450. }
  2451. mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
  2452. GFP_KERNEL);
  2453. if (!mux->shared_intr_buf) {
  2454. dev_err(&interface->dev, "Could not allocate intr buf?");
  2455. goto exit;
  2456. }
  2457. mutex_init(&mux->shared_int_lock);
  2458. return mux;
  2459. exit:
  2460. kfree(mux->shared_intr_buf);
  2461. usb_free_urb(mux->shared_intr_urb);
  2462. kfree(mux);
  2463. return NULL;
  2464. }
  2465. /* Gets the port spec for a certain interface */
  2466. static int hso_get_config_data(struct usb_interface *interface)
  2467. {
  2468. struct usb_device *usbdev = interface_to_usbdev(interface);
  2469. u8 config_data[17];
  2470. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2471. s32 result;
  2472. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2473. 0x86, 0xC0, 0, 0, config_data, 17,
  2474. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2475. return -EIO;
  2476. }
  2477. switch (config_data[if_num]) {
  2478. case 0x0:
  2479. result = 0;
  2480. break;
  2481. case 0x1:
  2482. result = HSO_PORT_DIAG;
  2483. break;
  2484. case 0x2:
  2485. result = HSO_PORT_GPS;
  2486. break;
  2487. case 0x3:
  2488. result = HSO_PORT_GPS_CONTROL;
  2489. break;
  2490. case 0x4:
  2491. result = HSO_PORT_APP;
  2492. break;
  2493. case 0x5:
  2494. result = HSO_PORT_APP2;
  2495. break;
  2496. case 0x6:
  2497. result = HSO_PORT_CONTROL;
  2498. break;
  2499. case 0x7:
  2500. result = HSO_PORT_NETWORK;
  2501. break;
  2502. case 0x8:
  2503. result = HSO_PORT_MODEM;
  2504. break;
  2505. case 0x9:
  2506. result = HSO_PORT_MSD;
  2507. break;
  2508. case 0xa:
  2509. result = HSO_PORT_PCSC;
  2510. break;
  2511. case 0xb:
  2512. result = HSO_PORT_VOICE;
  2513. break;
  2514. default:
  2515. result = 0;
  2516. }
  2517. if (result)
  2518. result |= HSO_INTF_BULK;
  2519. if (config_data[16] & 0x1)
  2520. result |= HSO_INFO_CRC_BUG;
  2521. return result;
  2522. }
  2523. /* called once for each interface upon device insertion */
  2524. static int hso_probe(struct usb_interface *interface,
  2525. const struct usb_device_id *id)
  2526. {
  2527. int mux, i, if_num, port_spec;
  2528. unsigned char port_mask;
  2529. struct hso_device *hso_dev = NULL;
  2530. struct hso_shared_int *shared_int;
  2531. struct hso_device *tmp_dev = NULL;
  2532. if_num = interface->altsetting->desc.bInterfaceNumber;
  2533. /* Get the interface/port specification from either driver_info or from
  2534. * the device itself */
  2535. if (id->driver_info)
  2536. port_spec = ((u32 *)(id->driver_info))[if_num];
  2537. else
  2538. port_spec = hso_get_config_data(interface);
  2539. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2540. dev_err(&interface->dev, "Not our interface\n");
  2541. return -ENODEV;
  2542. }
  2543. /* Check if we need to switch to alt interfaces prior to port
  2544. * configuration */
  2545. if (interface->num_altsetting > 1)
  2546. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2547. interface->needs_remote_wakeup = 1;
  2548. /* Allocate new hso device(s) */
  2549. switch (port_spec & HSO_INTF_MASK) {
  2550. case HSO_INTF_MUX:
  2551. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2552. /* Create the network device */
  2553. if (!disable_net) {
  2554. hso_dev = hso_create_net_device(interface);
  2555. if (!hso_dev)
  2556. goto exit;
  2557. tmp_dev = hso_dev;
  2558. }
  2559. }
  2560. if (hso_get_mux_ports(interface, &port_mask))
  2561. /* TODO: de-allocate everything */
  2562. goto exit;
  2563. shared_int = hso_create_shared_int(interface);
  2564. if (!shared_int)
  2565. goto exit;
  2566. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2567. if (port_mask & i) {
  2568. hso_dev = hso_create_mux_serial_device(
  2569. interface, i, shared_int);
  2570. if (!hso_dev)
  2571. goto exit;
  2572. }
  2573. }
  2574. if (tmp_dev)
  2575. hso_dev = tmp_dev;
  2576. break;
  2577. case HSO_INTF_BULK:
  2578. /* It's a regular bulk interface */
  2579. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
  2580. && !disable_net)
  2581. hso_dev = hso_create_net_device(interface);
  2582. else
  2583. hso_dev =
  2584. hso_create_bulk_serial_device(interface, port_spec);
  2585. if (!hso_dev)
  2586. goto exit;
  2587. break;
  2588. default:
  2589. goto exit;
  2590. }
  2591. usb_driver_claim_interface(&hso_driver, interface, hso_dev);
  2592. /* save our data pointer in this device */
  2593. usb_set_intfdata(interface, hso_dev);
  2594. /* done */
  2595. return 0;
  2596. exit:
  2597. hso_free_interface(interface);
  2598. return -ENODEV;
  2599. }
  2600. /* device removed, cleaning up */
  2601. static void hso_disconnect(struct usb_interface *interface)
  2602. {
  2603. hso_free_interface(interface);
  2604. /* remove reference of our private data */
  2605. usb_set_intfdata(interface, NULL);
  2606. usb_driver_release_interface(&hso_driver, interface);
  2607. }
  2608. static void async_get_intf(struct work_struct *data)
  2609. {
  2610. struct hso_device *hso_dev =
  2611. container_of(data, struct hso_device, async_get_intf);
  2612. usb_autopm_get_interface(hso_dev->interface);
  2613. }
  2614. static void async_put_intf(struct work_struct *data)
  2615. {
  2616. struct hso_device *hso_dev =
  2617. container_of(data, struct hso_device, async_put_intf);
  2618. usb_autopm_put_interface(hso_dev->interface);
  2619. }
  2620. static int hso_get_activity(struct hso_device *hso_dev)
  2621. {
  2622. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2623. if (!hso_dev->is_active) {
  2624. hso_dev->is_active = 1;
  2625. schedule_work(&hso_dev->async_get_intf);
  2626. }
  2627. }
  2628. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2629. return -EAGAIN;
  2630. usb_mark_last_busy(hso_dev->usb);
  2631. return 0;
  2632. }
  2633. static int hso_put_activity(struct hso_device *hso_dev)
  2634. {
  2635. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2636. if (hso_dev->is_active) {
  2637. hso_dev->is_active = 0;
  2638. schedule_work(&hso_dev->async_put_intf);
  2639. return -EAGAIN;
  2640. }
  2641. }
  2642. hso_dev->is_active = 0;
  2643. return 0;
  2644. }
  2645. /* called by kernel when we need to suspend device */
  2646. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2647. {
  2648. int i, result;
  2649. /* Stop all serial ports */
  2650. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2651. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2652. result = hso_stop_serial_device(serial_table[i]);
  2653. if (result)
  2654. goto out;
  2655. }
  2656. }
  2657. /* Stop all network ports */
  2658. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2659. if (network_table[i] &&
  2660. (network_table[i]->interface == iface)) {
  2661. result = hso_stop_net_device(network_table[i]);
  2662. if (result)
  2663. goto out;
  2664. }
  2665. }
  2666. out:
  2667. return 0;
  2668. }
  2669. /* called by kernel when we need to resume device */
  2670. static int hso_resume(struct usb_interface *iface)
  2671. {
  2672. int i, result = 0;
  2673. struct hso_net *hso_net;
  2674. /* Start all serial ports */
  2675. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2676. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2677. if (dev2ser(serial_table[i])->open_count) {
  2678. result =
  2679. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2680. hso_kick_transmit(dev2ser(serial_table[i]));
  2681. if (result)
  2682. goto out;
  2683. }
  2684. }
  2685. }
  2686. /* Start all network ports */
  2687. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2688. if (network_table[i] &&
  2689. (network_table[i]->interface == iface)) {
  2690. hso_net = dev2net(network_table[i]);
  2691. if (hso_net->flags & IFF_UP) {
  2692. /* First transmit any lingering data,
  2693. then restart the device. */
  2694. if (hso_net->skb_tx_buf) {
  2695. dev_dbg(&iface->dev,
  2696. "Transmitting"
  2697. " lingering data\n");
  2698. hso_net_start_xmit(hso_net->skb_tx_buf,
  2699. hso_net->net);
  2700. hso_net->skb_tx_buf = NULL;
  2701. }
  2702. result = hso_start_net_device(network_table[i]);
  2703. if (result)
  2704. goto out;
  2705. }
  2706. }
  2707. }
  2708. out:
  2709. return result;
  2710. }
  2711. static void hso_serial_ref_free(struct kref *ref)
  2712. {
  2713. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2714. hso_free_serial_device(hso_dev);
  2715. }
  2716. static void hso_free_interface(struct usb_interface *interface)
  2717. {
  2718. struct hso_serial *hso_dev;
  2719. int i;
  2720. struct mutex *hso_mutex = NULL;
  2721. int refcnt = 1;
  2722. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2723. if (serial_table[i]
  2724. && (serial_table[i]->interface == interface)) {
  2725. hso_dev = dev2ser(serial_table[i]);
  2726. if (hso_dev->tty)
  2727. tty_hangup(hso_dev->tty);
  2728. hso_mutex = &hso_dev->parent->mutex->mutex;
  2729. mutex_lock(hso_mutex);
  2730. hso_dev->parent->usb_gone = 1;
  2731. refcnt = kref_put(&serial_table[i]->ref,
  2732. hso_serial_ref_free);
  2733. mutex_unlock(hso_mutex);
  2734. }
  2735. }
  2736. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2737. if (network_table[i]
  2738. && (network_table[i]->interface == interface)) {
  2739. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2740. /* hso_stop_net_device doesn't stop the net queue since
  2741. * traffic needs to start it again when suspended */
  2742. netif_stop_queue(dev2net(network_table[i])->net);
  2743. hso_stop_net_device(network_table[i]);
  2744. cancel_work_sync(&network_table[i]->async_put_intf);
  2745. cancel_work_sync(&network_table[i]->async_get_intf);
  2746. if (rfk)
  2747. rfkill_unregister(rfk);
  2748. hso_free_net_device(network_table[i]);
  2749. }
  2750. }
  2751. if (refcnt == 0)
  2752. hso_free_mutex(container_of(hso_mutex,
  2753. struct hso_mutex_t, mutex));
  2754. }
  2755. /* Helper functions */
  2756. /* Get the endpoint ! */
  2757. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2758. int type, int dir)
  2759. {
  2760. int i;
  2761. struct usb_host_interface *iface = intf->cur_altsetting;
  2762. struct usb_endpoint_descriptor *endp;
  2763. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2764. endp = &iface->endpoint[i].desc;
  2765. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2766. ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
  2767. return endp;
  2768. }
  2769. return NULL;
  2770. }
  2771. /* Get the byte that describes which ports are enabled */
  2772. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2773. {
  2774. int i;
  2775. struct usb_host_interface *iface = intf->cur_altsetting;
  2776. if (iface->extralen == 3) {
  2777. *ports = iface->extra[2];
  2778. return 0;
  2779. }
  2780. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2781. if (iface->endpoint[i].extralen == 3) {
  2782. *ports = iface->endpoint[i].extra[2];
  2783. return 0;
  2784. }
  2785. }
  2786. return -1;
  2787. }
  2788. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2789. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2790. struct usb_device *usb, gfp_t gfp)
  2791. {
  2792. int result;
  2793. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2794. usb_rcvintpipe(usb,
  2795. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2796. shared_int->shared_intr_buf,
  2797. shared_int->intr_endp->wMaxPacketSize,
  2798. intr_callback, shared_int,
  2799. shared_int->intr_endp->bInterval);
  2800. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2801. if (result)
  2802. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2803. result);
  2804. return result;
  2805. }
  2806. /* operations setup of the serial interface */
  2807. static const struct tty_operations hso_serial_ops = {
  2808. .open = hso_serial_open,
  2809. .close = hso_serial_close,
  2810. .write = hso_serial_write,
  2811. .write_room = hso_serial_write_room,
  2812. .ioctl = hso_serial_ioctl,
  2813. .set_termios = hso_serial_set_termios,
  2814. .chars_in_buffer = hso_serial_chars_in_buffer,
  2815. .tiocmget = hso_serial_tiocmget,
  2816. .tiocmset = hso_serial_tiocmset,
  2817. .unthrottle = hso_unthrottle
  2818. };
  2819. static struct usb_driver hso_driver = {
  2820. .name = driver_name,
  2821. .probe = hso_probe,
  2822. .disconnect = hso_disconnect,
  2823. .id_table = hso_ids,
  2824. .suspend = hso_suspend,
  2825. .resume = hso_resume,
  2826. .supports_autosuspend = 1,
  2827. };
  2828. static int __init hso_init(void)
  2829. {
  2830. int i;
  2831. int result;
  2832. /* put it in the log */
  2833. printk(KERN_INFO "hso: %s\n", version);
  2834. /* Initialise the serial table semaphore and table */
  2835. spin_lock_init(&serial_table_lock);
  2836. spin_lock_init(&hso_mutex_lock);
  2837. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2838. serial_table[i] = NULL;
  2839. /* allocate our driver using the proper amount of supported minors */
  2840. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2841. if (!tty_drv)
  2842. return -ENOMEM;
  2843. /* fill in all needed values */
  2844. tty_drv->magic = TTY_DRIVER_MAGIC;
  2845. tty_drv->owner = THIS_MODULE;
  2846. tty_drv->driver_name = driver_name;
  2847. tty_drv->name = tty_filename;
  2848. /* if major number is provided as parameter, use that one */
  2849. if (tty_major)
  2850. tty_drv->major = tty_major;
  2851. tty_drv->minor_start = 0;
  2852. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2853. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2854. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2855. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2856. tty_drv->init_termios = tty_std_termios;
  2857. tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2858. tty_drv->termios = hso_serial_termios;
  2859. tty_drv->termios_locked = hso_serial_termios_locked;
  2860. tty_set_operations(tty_drv, &hso_serial_ops);
  2861. /* register the tty driver */
  2862. result = tty_register_driver(tty_drv);
  2863. if (result) {
  2864. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2865. __func__, result);
  2866. return result;
  2867. }
  2868. /* register this module as an usb driver */
  2869. result = usb_register(&hso_driver);
  2870. if (result) {
  2871. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2872. result);
  2873. /* cleanup serial interface */
  2874. tty_unregister_driver(tty_drv);
  2875. return result;
  2876. }
  2877. /* done */
  2878. return 0;
  2879. }
  2880. static void __exit hso_exit(void)
  2881. {
  2882. printk(KERN_INFO "hso: unloaded\n");
  2883. tty_unregister_driver(tty_drv);
  2884. /* deregister the usb driver */
  2885. usb_deregister(&hso_driver);
  2886. }
  2887. /* Module definitions */
  2888. module_init(hso_init);
  2889. module_exit(hso_exit);
  2890. MODULE_AUTHOR(MOD_AUTHOR);
  2891. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2892. MODULE_LICENSE(MOD_LICENSE);
  2893. MODULE_INFO(Version, DRIVER_VERSION);
  2894. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2895. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2896. module_param(debug, int, S_IRUGO | S_IWUSR);
  2897. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2898. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2899. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2900. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2901. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2902. module_param(disable_net, int, S_IRUGO | S_IWUSR);